@@ -2,6 +2,7 @@ package openapi3
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67
78 "github.com/getkin/kin-openapi/jsoninfo"
@@ -24,17 +25,10 @@ func (h Headers) JSONLookup(token string) (interface{}, error) {
2425 return ref .Value , nil
2526}
2627
28+ // Header is specified by OpenAPI/Swagger 3.0 standard.
29+ // See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#headerObject
2730type Header struct {
28- ExtensionProps
29-
30- // Optional description. Should use CommonMark syntax.
31- Description string `json:"description,omitempty" yaml:"description,omitempty"`
32- Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
33- Required bool `json:"required,omitempty" yaml:"required,omitempty"`
34- Schema * SchemaRef `json:"schema,omitempty" yaml:"schema,omitempty"`
35- Example interface {} `json:"example,omitempty" yaml:"example,omitempty"`
36- Examples Examples `json:"examples,omitempty" yaml:"examples,omitempty"`
37- Content Content `json:"content,omitempty" yaml:"content,omitempty"`
31+ Parameter
3832}
3933
4034var _ jsonpointer.JSONPointable = (* Header )(nil )
@@ -43,10 +37,52 @@ func (value *Header) UnmarshalJSON(data []byte) error {
4337 return jsoninfo .UnmarshalStrictStruct (data , value )
4438}
4539
40+ // SerializationMethod returns a header's serialization method.
41+ func (value * Header ) SerializationMethod () (* SerializationMethod , error ) {
42+ style := value .Style
43+ if style == "" {
44+ style = SerializationSimple
45+ }
46+ explode := false
47+ if value .Explode != nil {
48+ explode = * value .Explode
49+ }
50+ return & SerializationMethod {Style : style , Explode : explode }, nil
51+ }
52+
4653func (value * Header ) Validate (ctx context.Context ) error {
47- if v := value .Schema ; v != nil {
48- if err := v .Validate (ctx ); err != nil {
49- return err
54+ if value .Name != "" {
55+ return errors .New ("header 'name' MUST NOT be specified, it is given in the corresponding headers map" )
56+ }
57+ if value .In != "" {
58+ return errors .New ("header 'in' MUST NOT be specified, it is implicitly in header" )
59+ }
60+
61+ // Validate a parameter's serialization method.
62+ sm , err := value .SerializationMethod ()
63+ if err != nil {
64+ return err
65+ }
66+ if smSupported := false ||
67+ sm .Style == SerializationSimple && ! sm .Explode ||
68+ sm .Style == SerializationSimple && sm .Explode ; ! smSupported {
69+ e := fmt .Errorf ("serialization method with style=%q and explode=%v is not supported by a header parameter" , sm .Style , sm .Explode )
70+ return fmt .Errorf ("header schema is invalid: %v" , e )
71+ }
72+
73+ if (value .Schema == nil ) == (value .Content == nil ) {
74+ e := fmt .Errorf ("parameter must contain exactly one of content and schema: %v" , value )
75+ return fmt .Errorf ("header schema is invalid: %v" , e )
76+ }
77+ if schema := value .Schema ; schema != nil {
78+ if err := schema .Validate (ctx ); err != nil {
79+ return fmt .Errorf ("header schema is invalid: %v" , err )
80+ }
81+ }
82+
83+ if content := value .Content ; content != nil {
84+ if err := content .Validate (ctx ); err != nil {
85+ return fmt .Errorf ("header content is invalid: %v" , err )
5086 }
5187 }
5288 return nil
@@ -61,8 +97,20 @@ func (value Header) JSONLookup(token string) (interface{}, error) {
6197 }
6298 return value .Schema .Value , nil
6399 }
100+ case "name" :
101+ return value .Name , nil
102+ case "in" :
103+ return value .In , nil
64104 case "description" :
65105 return value .Description , nil
106+ case "style" :
107+ return value .Style , nil
108+ case "explode" :
109+ return value .Explode , nil
110+ case "allowEmptyValue" :
111+ return value .AllowEmptyValue , nil
112+ case "allowReserved" :
113+ return value .AllowReserved , nil
66114 case "deprecated" :
67115 return value .Deprecated , nil
68116 case "required" :
0 commit comments