-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi.go
145 lines (127 loc) · 4.68 KB
/
openapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2022 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package openapi
import (
"context"
"fmt"
"github.com/getkin/kin-openapi/openapi3"
)
// Schema represents a root of an OpenAPI v3 document.
type Schema struct {
*openapi3.T
}
// RootOption represents a Protocol Buffers root options.
type RootOption openapi3.ExtensionProps
// Extension represents a Protocol Buffers extension from within OpenAPI spec.
//
// The key name must be "x-extension".
type Extension struct {
Extend string `json:"extend" yaml:"extend"`
Fields []*ExtensionField `json:"fields" yaml:"fields"`
}
// ExtensionField defines the field to be added to the extend message type.
type ExtensionField struct {
Name string `yaml:"name" json:"name"`
Type string `yaml:"type" json:"type"`
Number int `yaml:"number" json:"number"`
}
// LoadFile loads f OpenAPI file and returns the new *Schema.
func LoadFile(ctx context.Context, f string) (*Schema, error) {
loader := &openapi3.Loader{
IsExternalRefsAllowed: true,
Context: ctx,
}
schema, err := loader.LoadFromFile(f)
if err != nil {
return nil, fmt.Errorf("could not open %s file: %w", f, err)
}
schema.InternalizeRefs(ctx, openapi3.DefaultRefNameResolver)
return &Schema{T: schema}, nil
}
// The following tokens are OpenAPI Specification Header Object field names.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#headerObject
const (
TokenName = "name"
TokenIn = "in"
TokenAllowEmptyValue = "allowEmptyValue"
)
// The following tokens are OpenAPI Specification MediaType Object field names.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#mediaTypeObject
const (
TokenEncoding = "encoding"
)
// The following tokens are OpenAPI Specification Operation Object field names.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#operation-object
const (
TokenTags = "tags"
TokenSummary = "summary"
TokenOperationID = "operationId"
TokenParameters = "parameters"
TokenRequestBody = "requestBody"
TokenResponses = "responses"
TokenCallbacks = "callbacks"
TokenSecurity = "security"
TokenServers = "servers"
)
// The following tokens are OpenAPI Specification Paramerter Object field names.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#parameter-object
const (
TokenStyle = "style"
TokenExplode = "explode"
TokenAllowReserved = "allowReserved"
TokenSchema = "schema"
TokenExamples = "examples"
TokenContent = "content"
)
// The following tokens are OpenAPI Specification Reference Object field names.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#referenceObject
const (
TokenRef = "$ref"
)
// The following tokens are taken directly from the JSON Schema definition.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schema-object
const (
TokenTitle = "title"
TokenMultipleOf = "multipleOf"
TokenMaximum = "maximum"
TokenExclusiveMaximum = "exclusiveMaximum"
TokenMinimum = "minimum"
TokenExclusiveMinimum = "exclusiveMinimum"
TokenMaxLength = "maxLength"
TokenMinLength = "minLength"
TokenPattern = "pattern"
TokenMaxItems = "maxItems"
TokenMinItems = "minItems"
TokenUniqueItems = "uniqueItems"
TokenMaxProperties = "maxProperties"
TokenMinProperties = "minProperties"
TokenRequired = "required"
TokenEnum = "enum"
)
// The following tokens are taken from the JSON Schema definition but their definitions were adjusted to the OpenAPI Specification.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schema-object
const (
TokenType = "type"
TokenAllOf = "allOf"
TokenOneOf = "oneOf"
TokenAnyOf = "anyOf"
TokenNot = "not"
TokenItems = "items"
TokenProperties = "properties"
TokenAdditionalProperties = "additionalProperties"
TokenDescription = "description"
TokenFormat = "format"
TokenDefault = "default"
)
// The following tokens are JSON Schema subset fields, definitions from the OpenAPI Specification.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schema-object
const (
TokenNullable = "nullable"
TokenDiscriminator = "discriminator"
TokenReadOnly = "readOnly"
TokenWriteOnly = "writeOnly"
TokenXml = "xml"
TokenExternalDocs = "externalDocs"
TokenExample = "example"
TokenDeprecated = "deprecated"
)