blob: bfe014ca51a8d9e7c10e4f8153d7a87a496349ec [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// Copyright 2015 go-swagger maintainers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package analysis
16
17import "github.com/go-openapi/spec"
18
19// FixEmptyResponseDescriptions replaces empty ("") response
20// descriptions in the input with "(empty)" to ensure that the
21// resulting Swagger is stays valid. The problem appears to arise
22// from reading in valid specs that have a explicit response
23// description of "" (valid, response.description is required), but
24// due to zero values being omitted upon re-serializing (omitempty) we
25// lose them unless we stick some chars in there.
26func FixEmptyResponseDescriptions(s *spec.Swagger) {
27 if s.Paths != nil {
28 for _, v := range s.Paths.Paths {
29 if v.Get != nil {
30 FixEmptyDescs(v.Get.Responses)
31 }
32 if v.Put != nil {
33 FixEmptyDescs(v.Put.Responses)
34 }
35 if v.Post != nil {
36 FixEmptyDescs(v.Post.Responses)
37 }
38 if v.Delete != nil {
39 FixEmptyDescs(v.Delete.Responses)
40 }
41 if v.Options != nil {
42 FixEmptyDescs(v.Options.Responses)
43 }
44 if v.Head != nil {
45 FixEmptyDescs(v.Head.Responses)
46 }
47 if v.Patch != nil {
48 FixEmptyDescs(v.Patch.Responses)
49 }
50 }
51 }
52 for k, v := range s.Responses {
53 FixEmptyDesc(&v)
54 s.Responses[k] = v
55 }
56}
57
58// FixEmptyDescs adds "(empty)" as the description for any Response in
59// the given Responses object that doesn't already have one.
60func FixEmptyDescs(rs *spec.Responses) {
61 FixEmptyDesc(rs.Default)
62 for k, v := range rs.StatusCodeResponses {
63 FixEmptyDesc(&v)
64 rs.StatusCodeResponses[k] = v
65 }
66}
67
68// FixEmptyDesc adds "(empty)" as the description to the given
69// Response object if it doesn't already have one and isn't a
70// ref. No-op on nil input.
71func FixEmptyDesc(rs *spec.Response) {
72 if rs == nil || rs.Description != "" || rs.Ref.Ref.GetURL() != nil {
73 return
74 }
75 rs.Description = "(empty)"
76}