podman
79 строк · 2.3 Кб
1// 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) {
27for k, v := range s.Responses {
28FixEmptyDesc(&v) //#nosec
29s.Responses[k] = v
30}
31
32if s.Paths == nil {
33return
34}
35
36for _, v := range s.Paths.Paths {
37if v.Get != nil {
38FixEmptyDescs(v.Get.Responses)
39}
40if v.Put != nil {
41FixEmptyDescs(v.Put.Responses)
42}
43if v.Post != nil {
44FixEmptyDescs(v.Post.Responses)
45}
46if v.Delete != nil {
47FixEmptyDescs(v.Delete.Responses)
48}
49if v.Options != nil {
50FixEmptyDescs(v.Options.Responses)
51}
52if v.Head != nil {
53FixEmptyDescs(v.Head.Responses)
54}
55if v.Patch != nil {
56FixEmptyDescs(v.Patch.Responses)
57}
58}
59}
60
61// FixEmptyDescs adds "(empty)" as the description for any Response in
62// the given Responses object that doesn't already have one.
63func FixEmptyDescs(rs *spec.Responses) {
64FixEmptyDesc(rs.Default)
65for k, v := range rs.StatusCodeResponses {
66FixEmptyDesc(&v) //#nosec
67rs.StatusCodeResponses[k] = v
68}
69}
70
71// FixEmptyDesc adds "(empty)" as the description to the given
72// Response object if it doesn't already have one and isn't a
73// ref. No-op on nil input.
74func FixEmptyDesc(rs *spec.Response) {
75if rs == nil || rs.Description != "" || rs.Ref.Ref.GetURL() != nil {
76return
77}
78rs.Description = "(empty)"
79}
80