response.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 spec
  15. import (
  16. "encoding/json"
  17. "github.com/go-openapi/swag"
  18. "k8s.io/kube-openapi/pkg/internal"
  19. jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
  20. )
  21. // ResponseProps properties specific to a response
  22. type ResponseProps struct {
  23. Description string `json:"description,omitempty"`
  24. Schema *Schema `json:"schema,omitempty"`
  25. Headers map[string]Header `json:"headers,omitempty"`
  26. Examples map[string]interface{} `json:"examples,omitempty"`
  27. }
  28. // Marshaling structure only, always edit along with corresponding
  29. // struct (or compilation will fail).
  30. type responsePropsOmitZero struct {
  31. Description string `json:"description,omitempty"`
  32. Schema *Schema `json:"schema,omitzero"`
  33. Headers map[string]Header `json:"headers,omitempty"`
  34. Examples map[string]interface{} `json:"examples,omitempty"`
  35. }
  36. // Response describes a single response from an API Operation.
  37. //
  38. // For more information: http://goo.gl/8us55a#responseObject
  39. type Response struct {
  40. Refable
  41. ResponseProps
  42. VendorExtensible
  43. }
  44. // UnmarshalJSON hydrates this items instance with the data from JSON
  45. func (r *Response) UnmarshalJSON(data []byte) error {
  46. if internal.UseOptimizedJSONUnmarshaling {
  47. return jsonv2.Unmarshal(data, r)
  48. }
  49. if err := json.Unmarshal(data, &r.ResponseProps); err != nil {
  50. return err
  51. }
  52. if err := json.Unmarshal(data, &r.Refable); err != nil {
  53. return err
  54. }
  55. if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. func (r *Response) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  61. var x struct {
  62. ResponseProps
  63. Extensions
  64. }
  65. if err := opts.UnmarshalNext(dec, &x); err != nil {
  66. return err
  67. }
  68. if err := r.Refable.Ref.fromMap(x.Extensions); err != nil {
  69. return err
  70. }
  71. r.Extensions = internal.SanitizeExtensions(x.Extensions)
  72. r.ResponseProps = x.ResponseProps
  73. return nil
  74. }
  75. // MarshalJSON converts this items object to JSON
  76. func (r Response) MarshalJSON() ([]byte, error) {
  77. if internal.UseOptimizedJSONMarshaling {
  78. return internal.DeterministicMarshal(r)
  79. }
  80. b1, err := json.Marshal(r.ResponseProps)
  81. if err != nil {
  82. return nil, err
  83. }
  84. b2, err := json.Marshal(r.Refable)
  85. if err != nil {
  86. return nil, err
  87. }
  88. b3, err := json.Marshal(r.VendorExtensible)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return swag.ConcatJSON(b1, b2, b3), nil
  93. }
  94. func (r Response) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  95. var x struct {
  96. Ref string `json:"$ref,omitempty"`
  97. Extensions
  98. ResponseProps responsePropsOmitZero `json:",inline"`
  99. }
  100. x.Ref = r.Refable.Ref.String()
  101. x.Extensions = internal.SanitizeExtensions(r.Extensions)
  102. x.ResponseProps = responsePropsOmitZero(r.ResponseProps)
  103. return opts.MarshalNext(enc, x)
  104. }
  105. // NewResponse creates a new response instance
  106. func NewResponse() *Response {
  107. return new(Response)
  108. }
  109. // ResponseRef creates a response as a json reference
  110. func ResponseRef(url string) *Response {
  111. resp := NewResponse()
  112. resp.Ref = MustCreateRef(url)
  113. return resp
  114. }