This repository was archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopenapi.go
66 lines (54 loc) · 1.62 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
package openapi
import (
"strings"
)
// SchemaError -.
type SchemaError struct {
Ref string
}
// Error -.
func (e *SchemaError) Error() string {
return "unknown schema " + e.Ref
}
// RequestBodyError -.
type RequestBodyError struct {
Ref string
}
// Error -.
func (e *RequestBodyError) Error() string {
return "unknown request body " + e.Ref
}
// OpenAPI Object
// See specification https://swagger.io/specification/#openapi-object
type OpenAPI struct {
Paths Paths `json:"paths" yaml:"paths"`
Components Components `json:"components,omitempty" yaml:"components,omitempty"`
OpenAPI string `json:"openapi" yaml:"openapi"`
Servers Servers `json:"servers,omitempty" yaml:"servers,omitempty"`
Security []Security `json:"security,omitempty" yaml:"security,omitempty"`
Tags Tags `json:"tags,omitempty" yaml:"tags,omitempty"`
Info Info `json:"info" yaml:"info"`
}
// LookupSchemaByReference -.
func (api OpenAPI) LookupSchemaByReference(ref string) (Schema, error) {
schema, ok := api.Components.Schemas[schemaKey(ref)]
if ok {
return *schema, nil
}
return Schema{}, &SchemaError{Ref: ref}
}
func (api OpenAPI) LookupRequestBodyByReference(ref string) (RequestBody, error) {
requestBody, ok := api.Components.RequestBodies[requestBodiesKey(ref)]
if ok {
return *requestBody, nil
}
return RequestBody{}, &RequestBodyError{Ref: ref}
}
func schemaKey(ref string) string {
const prefix = "#/components/schemas/"
return strings.TrimPrefix(ref, prefix)
}
func requestBodiesKey(ref string) string {
const prefix = "#/components/requestBodies/"
return strings.TrimPrefix(ref, prefix)
}