...

Source file src/github.com/go-openapi/analysis/fixer.go

Documentation: github.com/go-openapi/analysis

     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  
    15  package analysis
    16  
    17  import "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.
    26  func FixEmptyResponseDescriptions(s *spec.Swagger) {
    27  	for k, v := range s.Responses {
    28  		FixEmptyDesc(&v) //#nosec
    29  		s.Responses[k] = v
    30  	}
    31  
    32  	if s.Paths == nil {
    33  		return
    34  	}
    35  
    36  	for _, v := range s.Paths.Paths {
    37  		if v.Get != nil {
    38  			FixEmptyDescs(v.Get.Responses)
    39  		}
    40  		if v.Put != nil {
    41  			FixEmptyDescs(v.Put.Responses)
    42  		}
    43  		if v.Post != nil {
    44  			FixEmptyDescs(v.Post.Responses)
    45  		}
    46  		if v.Delete != nil {
    47  			FixEmptyDescs(v.Delete.Responses)
    48  		}
    49  		if v.Options != nil {
    50  			FixEmptyDescs(v.Options.Responses)
    51  		}
    52  		if v.Head != nil {
    53  			FixEmptyDescs(v.Head.Responses)
    54  		}
    55  		if v.Patch != nil {
    56  			FixEmptyDescs(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.
    63  func FixEmptyDescs(rs *spec.Responses) {
    64  	FixEmptyDesc(rs.Default)
    65  	for k, v := range rs.StatusCodeResponses {
    66  		FixEmptyDesc(&v) //#nosec
    67  		rs.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.
    74  func FixEmptyDesc(rs *spec.Response) {
    75  	if rs == nil || rs.Description != "" || rs.Ref.Ref.GetURL() != nil {
    76  		return
    77  	}
    78  	rs.Description = "(empty)"
    79  }
    80  

View as plain text