Debian Codegraph @

pkg/validation/spec/path_item.go

raw
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 spec
16
17 import (
18 "encoding/json"
19
20 "github.com/go-openapi/swag"
21 "k8s.io/kube-openapi/pkg/internal"
22 jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
23 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext"
24 )
25
26 // PathItemProps the path item specific properties
27 type PathItemProps struct {
28 Get *Operation `json:"get,omitempty"`
29 Put *Operation `json:"put,omitempty"`
30 Post *Operation `json:"post,omitempty"`
31 Delete *Operation `json:"delete,omitempty"`
32 Options *Operation `json:"options,omitempty"`
33 Head *Operation `json:"head,omitempty"`
34 Patch *Operation `json:"patch,omitempty"`
35 Parameters []Parameter `json:"parameters,omitempty"`
36 }
37
38 // PathItem describes the operations available on a single path.
39 // A Path Item may be empty, due to [ACL constraints](http://goo.gl/8us55a#securityFiltering).
40 // The path itself is still exposed to the documentation viewer but they will
41 // not know which operations and parameters are available.
42 //
43 // For more information: http://goo.gl/8us55a#pathItemObject
44 type PathItem struct {
45 Refable
46 VendorExtensible
47 PathItemProps
48 }
49
50 // UnmarshalJSON hydrates this items instance with the data from JSON
51 func (p *PathItem) UnmarshalJSON(data []byte) error {
52 if internal.UseOptimizedJSONUnmarshaling {
53 return jsonv2.Unmarshal(data, p)
54 }
55
56 if err := json.Unmarshal(data, &p.Refable); err != nil {
57 return err
58 }
59 if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
60 return err
61 }
62 return json.Unmarshal(data, &p.PathItemProps)
63 }
64
65 func (p *PathItem) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
66 var x struct {
67 Extensions Extensions `json:",inline"`
68 PathItemProps
69 }
70
71 if err := jsonv2.UnmarshalDecode(dec, &x); err != nil {
72 return err
73 }
74 if err := p.Refable.Ref.fromMap(x.Extensions); err != nil {
75 return err
76 }
77 p.Extensions = internal.SanitizeExtensions(x.Extensions)
78 p.PathItemProps = x.PathItemProps
79
80 return nil
81 }
82
83 // MarshalJSON converts this items object to JSON
84 func (p PathItem) MarshalJSON() ([]byte, error) {
85 if internal.UseOptimizedJSONMarshaling {
86 return internal.DeterministicMarshal(p)
87 }
88 b3, err := json.Marshal(p.Refable)
89 if err != nil {
90 return nil, err
91 }
92 b4, err := json.Marshal(p.VendorExtensible)
93 if err != nil {
94 return nil, err
95 }
96 b5, err := json.Marshal(p.PathItemProps)
97 if err != nil {
98 return nil, err
99 }
100 concated := swag.ConcatJSON(b3, b4, b5)
101 return concated, nil
102 }
103
104 func (p PathItem) MarshalJSONTo(enc *jsontext.Encoder) error {
105 var x struct {
106 Ref string `json:"$ref,omitempty"`
107 Extensions Extensions `json:",inline"`
108 PathItemProps
109 }
110 x.Ref = p.Refable.Ref.String()
111 x.Extensions = internal.SanitizeExtensions(p.Extensions)
112 x.PathItemProps = p.PathItemProps
113 return jsonv2.MarshalEncode(enc, x)
114 }