response.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // Response describes a single response from an API Operation.
  29. //
  30. // For more information: http://goo.gl/8us55a#responseObject
  31. type Response struct {
  32. Refable
  33. ResponseProps
  34. VendorExtensible
  35. }
  36. // UnmarshalJSON hydrates this items instance with the data from JSON
  37. func (r *Response) UnmarshalJSON(data []byte) error {
  38. if internal.UseOptimizedJSONUnmarshaling {
  39. return jsonv2.Unmarshal(data, r)
  40. }
  41. if err := json.Unmarshal(data, &r.ResponseProps); err != nil {
  42. return err
  43. }
  44. if err := json.Unmarshal(data, &r.Refable); err != nil {
  45. return err
  46. }
  47. if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. func (r *Response) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  53. var x struct {
  54. ResponseProps
  55. Extensions
  56. }
  57. if err := opts.UnmarshalNext(dec, &x); err != nil {
  58. return err
  59. }
  60. r.Extensions = x.Extensions
  61. r.ResponseProps = x.ResponseProps
  62. if err := r.Refable.Ref.fromMap(r.Extensions); err != nil {
  63. return err
  64. }
  65. r.Extensions.sanitize()
  66. if len(r.Extensions) == 0 {
  67. r.Extensions = nil
  68. }
  69. return nil
  70. }
  71. // MarshalJSON converts this items object to JSON
  72. func (r Response) MarshalJSON() ([]byte, error) {
  73. b1, err := json.Marshal(r.ResponseProps)
  74. if err != nil {
  75. return nil, err
  76. }
  77. b2, err := json.Marshal(r.Refable)
  78. if err != nil {
  79. return nil, err
  80. }
  81. b3, err := json.Marshal(r.VendorExtensible)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return swag.ConcatJSON(b1, b2, b3), nil
  86. }
  87. // NewResponse creates a new response instance
  88. func NewResponse() *Response {
  89. return new(Response)
  90. }
  91. // ResponseRef creates a response as a json reference
  92. func ResponseRef(url string) *Response {
  93. resp := NewResponse()
  94. resp.Ref = MustCreateRef(url)
  95. return resp
  96. }