You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fixer.go 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. package analysis
  15. import "github.com/go-openapi/spec"
  16. // FixEmptyResponseDescriptions replaces empty ("") response
  17. // descriptions in the input with "(empty)" to ensure that the
  18. // resulting Swagger is stays valid. The problem appears to arise
  19. // from reading in valid specs that have a explicit response
  20. // description of "" (valid, response.description is required), but
  21. // due to zero values being omitted upon re-serializing (omitempty) we
  22. // lose them unless we stick some chars in there.
  23. func FixEmptyResponseDescriptions(s *spec.Swagger) {
  24. if s.Paths != nil {
  25. for _, v := range s.Paths.Paths {
  26. if v.Get != nil {
  27. FixEmptyDescs(v.Get.Responses)
  28. }
  29. if v.Put != nil {
  30. FixEmptyDescs(v.Put.Responses)
  31. }
  32. if v.Post != nil {
  33. FixEmptyDescs(v.Post.Responses)
  34. }
  35. if v.Delete != nil {
  36. FixEmptyDescs(v.Delete.Responses)
  37. }
  38. if v.Options != nil {
  39. FixEmptyDescs(v.Options.Responses)
  40. }
  41. if v.Head != nil {
  42. FixEmptyDescs(v.Head.Responses)
  43. }
  44. if v.Patch != nil {
  45. FixEmptyDescs(v.Patch.Responses)
  46. }
  47. }
  48. }
  49. for k, v := range s.Responses {
  50. FixEmptyDesc(&v)
  51. s.Responses[k] = v
  52. }
  53. }
  54. // FixEmptyDescs adds "(empty)" as the description for any Response in
  55. // the given Responses object that doesn't already have one.
  56. func FixEmptyDescs(rs *spec.Responses) {
  57. FixEmptyDesc(rs.Default)
  58. for k, v := range rs.StatusCodeResponses {
  59. FixEmptyDesc(&v)
  60. rs.StatusCodeResponses[k] = v
  61. }
  62. }
  63. // FixEmptyDesc adds "(empty)" as the description to the given
  64. // Response object if it doesn't already have one and isn't a
  65. // ref. No-op on nil input.
  66. func FixEmptyDesc(rs *spec.Response) {
  67. if rs == nil || rs.Description != "" || rs.Ref.Ref.GetURL() != nil {
  68. return
  69. }
  70. rs.Description = "(empty)"
  71. }