|
1 | | -type Header struct{ ... } |
2 | | -type Operation struct{ ... } |
3 | | -type Parameter struct{ ... } |
| 1 | +package openapi2 // import "github.com/getkin/kin-openapi/openapi2" |
| 2 | + |
| 3 | +Package openapi2 parses and writes OpenAPIv2 specification documents. |
| 4 | + |
| 5 | +Does not cover all elements of OpenAPIv2. When OpenAPI version 3 is |
| 6 | +backwards-compatible with version 2, version 3 elements have been used. |
| 7 | + |
| 8 | +See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md |
| 9 | + |
| 10 | +TYPES |
| 11 | + |
| 12 | +type Header struct { |
| 13 | + Parameter |
| 14 | +} |
| 15 | + |
| 16 | +func (header Header) MarshalJSON() ([]byte, error) |
| 17 | + MarshalJSON returns the JSON encoding of Header. |
| 18 | + |
| 19 | +func (header *Header) UnmarshalJSON(data []byte) error |
| 20 | + UnmarshalJSON sets Header to a copy of data. |
| 21 | + |
| 22 | +type Operation struct { |
| 23 | + Extensions map[string]any `json:"-" yaml:"-"` |
| 24 | + |
| 25 | + Summary string `json:"summary,omitempty" yaml:"summary,omitempty"` |
| 26 | + Description string `json:"description,omitempty" yaml:"description,omitempty"` |
| 27 | + Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"` |
| 28 | + ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"` |
| 29 | + Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"` |
| 30 | + OperationID string `json:"operationId,omitempty" yaml:"operationId,omitempty"` |
| 31 | + Parameters Parameters `json:"parameters,omitempty" yaml:"parameters,omitempty"` |
| 32 | + Responses map[string]*Response `json:"responses" yaml:"responses"` |
| 33 | + Consumes []string `json:"consumes,omitempty" yaml:"consumes,omitempty"` |
| 34 | + Produces []string `json:"produces,omitempty" yaml:"produces,omitempty"` |
| 35 | + Schemes []string `json:"schemes,omitempty" yaml:"schemes,omitempty"` |
| 36 | + Security *SecurityRequirements `json:"security,omitempty" yaml:"security,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +func (operation Operation) MarshalJSON() ([]byte, error) |
| 40 | + MarshalJSON returns the JSON encoding of Operation. |
| 41 | + |
| 42 | +func (operation *Operation) UnmarshalJSON(data []byte) error |
| 43 | + UnmarshalJSON sets Operation to a copy of data. |
| 44 | + |
| 45 | +type Parameter struct { |
| 46 | + Extensions map[string]any `json:"-" yaml:"-"` |
| 47 | + |
| 48 | + Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"` |
| 49 | + |
| 50 | + In string `json:"in,omitempty" yaml:"in,omitempty"` |
| 51 | + Name string `json:"name,omitempty" yaml:"name,omitempty"` |
| 52 | + Description string `json:"description,omitempty" yaml:"description,omitempty"` |
| 53 | + CollectionFormat string `json:"collectionFormat,omitempty" yaml:"collectionFormat,omitempty"` |
| 54 | + Type *openapi3.Types `json:"type,omitempty" yaml:"type,omitempty"` |
| 55 | + Format string `json:"format,omitempty" yaml:"format,omitempty"` |
| 56 | + Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"` |
| 57 | + AllowEmptyValue bool `json:"allowEmptyValue,omitempty" yaml:"allowEmptyValue,omitempty"` |
| 58 | + Required bool `json:"required,omitempty" yaml:"required,omitempty"` |
| 59 | + UniqueItems bool `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"` |
| 60 | + ExclusiveMin bool `json:"exclusiveMinimum,omitempty" yaml:"exclusiveMinimum,omitempty"` |
| 61 | + ExclusiveMax bool `json:"exclusiveMaximum,omitempty" yaml:"exclusiveMaximum,omitempty"` |
| 62 | + Schema *SchemaRef `json:"schema,omitempty" yaml:"schema,omitempty"` |
| 63 | + Items *SchemaRef `json:"items,omitempty" yaml:"items,omitempty"` |
| 64 | + Enum []any `json:"enum,omitempty" yaml:"enum,omitempty"` |
| 65 | + MultipleOf *float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"` |
| 66 | + Minimum *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"` |
| 67 | + Maximum *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"` |
| 68 | + MaxLength *uint64 `json:"maxLength,omitempty" yaml:"maxLength,omitempty"` |
| 69 | + MaxItems *uint64 `json:"maxItems,omitempty" yaml:"maxItems,omitempty"` |
| 70 | + MinLength uint64 `json:"minLength,omitempty" yaml:"minLength,omitempty"` |
| 71 | + MinItems uint64 `json:"minItems,omitempty" yaml:"minItems,omitempty"` |
| 72 | + Default any `json:"default,omitempty" yaml:"default,omitempty"` |
| 73 | +} |
| 74 | + |
| 75 | +func (parameter Parameter) MarshalJSON() ([]byte, error) |
| 76 | + MarshalJSON returns the JSON encoding of Parameter. |
| 77 | + |
| 78 | +func (parameter *Parameter) UnmarshalJSON(data []byte) error |
| 79 | + UnmarshalJSON sets Parameter to a copy of data. |
| 80 | + |
4 | 81 | type Parameters []*Parameter |
5 | | -type PathItem struct{ ... } |
6 | | -type Response struct{ ... } |
| 82 | + |
| 83 | +func (ps Parameters) Len() int |
| 84 | + |
| 85 | +func (ps Parameters) Less(i, j int) bool |
| 86 | + |
| 87 | +func (ps Parameters) Swap(i, j int) |
| 88 | + |
| 89 | +type PathItem struct { |
| 90 | + Extensions map[string]any `json:"-" yaml:"-"` |
| 91 | + |
| 92 | + Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"` |
| 93 | + |
| 94 | + Delete *Operation `json:"delete,omitempty" yaml:"delete,omitempty"` |
| 95 | + Get *Operation `json:"get,omitempty" yaml:"get,omitempty"` |
| 96 | + Head *Operation `json:"head,omitempty" yaml:"head,omitempty"` |
| 97 | + Options *Operation `json:"options,omitempty" yaml:"options,omitempty"` |
| 98 | + Patch *Operation `json:"patch,omitempty" yaml:"patch,omitempty"` |
| 99 | + Post *Operation `json:"post,omitempty" yaml:"post,omitempty"` |
| 100 | + Put *Operation `json:"put,omitempty" yaml:"put,omitempty"` |
| 101 | + Parameters Parameters `json:"parameters,omitempty" yaml:"parameters,omitempty"` |
| 102 | +} |
| 103 | + |
| 104 | +func (pathItem *PathItem) GetOperation(method string) *Operation |
| 105 | + |
| 106 | +func (pathItem PathItem) MarshalJSON() ([]byte, error) |
| 107 | + MarshalJSON returns the JSON encoding of PathItem. |
| 108 | + |
| 109 | +func (pathItem *PathItem) Operations() map[string]*Operation |
| 110 | + |
| 111 | +func (pathItem *PathItem) SetOperation(method string, operation *Operation) |
| 112 | + |
| 113 | +func (pathItem *PathItem) UnmarshalJSON(data []byte) error |
| 114 | + UnmarshalJSON sets PathItem to a copy of data. |
| 115 | + |
| 116 | +type Ref struct { |
| 117 | + Ref string `json:"$ref" yaml:"$ref"` |
| 118 | +} |
| 119 | + Ref is specified by OpenAPI/Swagger 2.0 standard. See |
| 120 | + https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#reference-object |
| 121 | + |
| 122 | +type Response struct { |
| 123 | + Extensions map[string]any `json:"-" yaml:"-"` |
| 124 | + |
| 125 | + Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"` |
| 126 | + |
| 127 | + Description string `json:"description,omitempty" yaml:"description,omitempty"` |
| 128 | + Schema *SchemaRef `json:"schema,omitempty" yaml:"schema,omitempty"` |
| 129 | + Headers map[string]*Header `json:"headers,omitempty" yaml:"headers,omitempty"` |
| 130 | + Examples map[string]any `json:"examples,omitempty" yaml:"examples,omitempty"` |
| 131 | +} |
| 132 | + |
| 133 | +func (response Response) MarshalJSON() ([]byte, error) |
| 134 | + MarshalJSON returns the JSON encoding of Response. |
| 135 | + |
| 136 | +func (response *Response) UnmarshalJSON(data []byte) error |
| 137 | + UnmarshalJSON sets Response to a copy of data. |
| 138 | + |
| 139 | +type Schema struct { |
| 140 | + Extensions map[string]any `json:"-" yaml:"-"` |
| 141 | + |
| 142 | + AllOf SchemaRefs `json:"allOf,omitempty" yaml:"allOf,omitempty"` |
| 143 | + Not *SchemaRef `json:"not,omitempty" yaml:"not,omitempty"` |
| 144 | + Type *openapi3.Types `json:"type,omitempty" yaml:"type,omitempty"` |
| 145 | + Title string `json:"title,omitempty" yaml:"title,omitempty"` |
| 146 | + Format string `json:"format,omitempty" yaml:"format,omitempty"` |
| 147 | + Description string `json:"description,omitempty" yaml:"description,omitempty"` |
| 148 | + Enum []any `json:"enum,omitempty" yaml:"enum,omitempty"` |
| 149 | + Default any `json:"default,omitempty" yaml:"default,omitempty"` |
| 150 | + Example any `json:"example,omitempty" yaml:"example,omitempty"` |
| 151 | + ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"` |
| 152 | + |
| 153 | + // Array-related, here for struct compactness |
| 154 | + UniqueItems bool `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"` |
| 155 | + // Number-related, here for struct compactness |
| 156 | + ExclusiveMin bool `json:"exclusiveMinimum,omitempty" yaml:"exclusiveMinimum,omitempty"` |
| 157 | + ExclusiveMax bool `json:"exclusiveMaximum,omitempty" yaml:"exclusiveMaximum,omitempty"` |
| 158 | + // Properties |
| 159 | + ReadOnly bool `json:"readOnly,omitempty" yaml:"readOnly,omitempty"` |
| 160 | + WriteOnly bool `json:"writeOnly,omitempty" yaml:"writeOnly,omitempty"` |
| 161 | + AllowEmptyValue bool `json:"allowEmptyValue,omitempty" yaml:"allowEmptyValue,omitempty"` |
| 162 | + Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"` |
| 163 | + XML *openapi3.XML `json:"xml,omitempty" yaml:"xml,omitempty"` |
| 164 | + |
| 165 | + // Number |
| 166 | + Min *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"` |
| 167 | + Max *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"` |
| 168 | + MultipleOf *float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"` |
| 169 | + |
| 170 | + // String |
| 171 | + MinLength uint64 `json:"minLength,omitempty" yaml:"minLength,omitempty"` |
| 172 | + MaxLength *uint64 `json:"maxLength,omitempty" yaml:"maxLength,omitempty"` |
| 173 | + Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"` |
| 174 | + |
| 175 | + // Array |
| 176 | + MinItems uint64 `json:"minItems,omitempty" yaml:"minItems,omitempty"` |
| 177 | + MaxItems *uint64 `json:"maxItems,omitempty" yaml:"maxItems,omitempty"` |
| 178 | + Items *SchemaRef `json:"items,omitempty" yaml:"items,omitempty"` |
| 179 | + |
| 180 | + // Object |
| 181 | + Required []string `json:"required,omitempty" yaml:"required,omitempty"` |
| 182 | + Properties Schemas `json:"properties,omitempty" yaml:"properties,omitempty"` |
| 183 | + MinProps uint64 `json:"minProperties,omitempty" yaml:"minProperties,omitempty"` |
| 184 | + MaxProps *uint64 `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"` |
| 185 | + AdditionalProperties openapi3.AdditionalProperties `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"` |
| 186 | + Discriminator string `json:"discriminator,omitempty" yaml:"discriminator,omitempty"` |
| 187 | +} |
| 188 | + Schema is specified by OpenAPI/Swagger 2.0 standard. See |
| 189 | + https://swagger.io/specification/v2/#schema-object |
| 190 | + |
| 191 | +func (schema Schema) MarshalJSON() ([]byte, error) |
| 192 | + MarshalJSON returns the JSON encoding of Schema. |
| 193 | + |
| 194 | +func (schema Schema) MarshalYAML() (any, error) |
| 195 | + MarshalYAML returns the YAML encoding of Schema. |
| 196 | + |
| 197 | +func (schema *Schema) UnmarshalJSON(data []byte) error |
| 198 | + UnmarshalJSON sets Schema to a copy of data. |
| 199 | + |
| 200 | +type SchemaRef struct { |
| 201 | + // Extensions only captures fields starting with 'x-' as no other fields |
| 202 | + // are allowed by the openapi spec. |
| 203 | + Extensions map[string]any |
| 204 | + |
| 205 | + Ref string |
| 206 | + Value *Schema |
| 207 | + |
| 208 | + // Has unexported fields. |
| 209 | +} |
| 210 | + SchemaRef represents either a Schema or a $ref to a Schema. When serializing |
| 211 | + and both fields are set, Ref is preferred over Value. |
| 212 | + |
| 213 | +func (x *SchemaRef) CollectionName() string |
| 214 | + CollectionName returns the JSON string used for a collection of these |
| 215 | + components. |
| 216 | + |
| 217 | +func (x *SchemaRef) JSONLookup(token string) (any, error) |
| 218 | + JSONLookup implements |
| 219 | + https://pkg.go.dev/github.com/go-openapi/jsonpointer#JSONPointable |
| 220 | + |
| 221 | +func (x SchemaRef) MarshalJSON() ([]byte, error) |
| 222 | + MarshalJSON returns the JSON encoding of SchemaRef. |
| 223 | + |
| 224 | +func (x SchemaRef) MarshalYAML() (any, error) |
| 225 | + MarshalYAML returns the YAML encoding of SchemaRef. |
| 226 | + |
| 227 | +func (x *SchemaRef) RefPath() *url.URL |
| 228 | + RefPath returns the path of the $ref relative to the root document. |
| 229 | + |
| 230 | +func (x *SchemaRef) RefString() string |
| 231 | + RefString returns the $ref value. |
| 232 | + |
| 233 | +func (x *SchemaRef) UnmarshalJSON(data []byte) error |
| 234 | + UnmarshalJSON sets SchemaRef to a copy of data. |
| 235 | + |
| 236 | +type SchemaRefs []*SchemaRef |
| 237 | + |
| 238 | +type Schemas map[string]*SchemaRef |
| 239 | + |
7 | 240 | type SecurityRequirements []map[string][]string |
8 | | -type SecurityScheme struct{ ... } |
9 | | -type T struct{ ... } |
| 241 | + |
| 242 | +type SecurityScheme struct { |
| 243 | + Extensions map[string]any `json:"-" yaml:"-"` |
| 244 | + |
| 245 | + Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"` |
| 246 | + |
| 247 | + Description string `json:"description,omitempty" yaml:"description,omitempty"` |
| 248 | + Type string `json:"type,omitempty" yaml:"type,omitempty"` |
| 249 | + In string `json:"in,omitempty" yaml:"in,omitempty"` |
| 250 | + Name string `json:"name,omitempty" yaml:"name,omitempty"` |
| 251 | + Flow string `json:"flow,omitempty" yaml:"flow,omitempty"` |
| 252 | + AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"` |
| 253 | + TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"` |
| 254 | + Scopes map[string]string `json:"scopes,omitempty" yaml:"scopes,omitempty"` |
| 255 | + Tags openapi3.Tags `json:"tags,omitempty" yaml:"tags,omitempty"` |
| 256 | +} |
| 257 | + |
| 258 | +func (securityScheme SecurityScheme) MarshalJSON() ([]byte, error) |
| 259 | + MarshalJSON returns the JSON encoding of SecurityScheme. |
| 260 | + |
| 261 | +func (securityScheme *SecurityScheme) UnmarshalJSON(data []byte) error |
| 262 | + UnmarshalJSON sets SecurityScheme to a copy of data. |
| 263 | + |
| 264 | +type T struct { |
| 265 | + Extensions map[string]any `json:"-" yaml:"-"` |
| 266 | + |
| 267 | + Swagger string `json:"swagger" yaml:"swagger"` // required |
| 268 | + Info openapi3.Info `json:"info" yaml:"info"` // required |
| 269 | + ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"` |
| 270 | + Schemes []string `json:"schemes,omitempty" yaml:"schemes,omitempty"` |
| 271 | + Consumes []string `json:"consumes,omitempty" yaml:"consumes,omitempty"` |
| 272 | + Produces []string `json:"produces,omitempty" yaml:"produces,omitempty"` |
| 273 | + Host string `json:"host,omitempty" yaml:"host,omitempty"` |
| 274 | + BasePath string `json:"basePath,omitempty" yaml:"basePath,omitempty"` |
| 275 | + Paths map[string]*PathItem `json:"paths,omitempty" yaml:"paths,omitempty"` |
| 276 | + Definitions map[string]*SchemaRef `json:"definitions,omitempty" yaml:"definitions,omitempty"` |
| 277 | + Parameters map[string]*Parameter `json:"parameters,omitempty" yaml:"parameters,omitempty"` |
| 278 | + Responses map[string]*Response `json:"responses,omitempty" yaml:"responses,omitempty"` |
| 279 | + SecurityDefinitions map[string]*SecurityScheme `json:"securityDefinitions,omitempty" yaml:"securityDefinitions,omitempty"` |
| 280 | + Security SecurityRequirements `json:"security,omitempty" yaml:"security,omitempty"` |
| 281 | + Tags openapi3.Tags `json:"tags,omitempty" yaml:"tags,omitempty"` |
| 282 | +} |
| 283 | + T is the root of an OpenAPI v2 document |
| 284 | + |
| 285 | +func (doc *T) AddOperation(path string, method string, operation *Operation) |
| 286 | + |
| 287 | +func (doc T) MarshalJSON() ([]byte, error) |
| 288 | + MarshalJSON returns the JSON encoding of T. |
| 289 | + |
| 290 | +func (doc *T) UnmarshalJSON(data []byte) error |
| 291 | + UnmarshalJSON sets T to a copy of data. |
| 292 | + |
0 commit comments