forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi_test.go
170 lines (162 loc) · 3.58 KB
/
openapi_test.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package huma_test
import (
"testing"
"github.com/danielgtaylor/huma/v2"
"github.com/stretchr/testify/require"
)
func TestOpenAPIMarshal(t *testing.T) {
// Simple spot check to make sure we are generating valid YAML and that
// the OpenAPI generally works as expected.
num := 1.0
v := huma.OpenAPI{
OpenAPI: "3.0.0",
Info: &huma.Info{
Title: "Test API",
Version: "1.0.0",
Contact: &huma.Contact{
Name: "Daniel Taylor",
},
License: &huma.License{
Name: "MIT",
},
},
ExternalDocs: &huma.ExternalDocs{
URL: "https://example.com",
},
Tags: []*huma.Tag{
{
Name: "test",
},
},
Servers: []*huma.Server{
{
URL: "https://example.com/{foo}",
Variables: map[string]*huma.ServerVariable{
"foo": {
Default: "bar",
Enum: []string{"bar", "baz"},
},
},
},
},
Components: &huma.Components{
Schemas: huma.NewMapRegistry("#/components/schemas/", huma.DefaultSchemaNamer),
SecuritySchemes: map[string]*huma.SecurityScheme{
"oauth2": {
Type: "oauth2",
Flows: &huma.OAuthFlows{
ClientCredentials: &huma.OAuthFlow{
AuthorizationURL: "https://example.com/oauth2/authorize",
TokenURL: "https://example.com/oauth2/token",
Scopes: map[string]string{
"test": "Test scope",
},
},
},
},
},
},
Paths: map[string]*huma.PathItem{
"/test": {
Get: &huma.Operation{
Responses: map[string]*huma.Response{
"200": {
Description: "OK",
Content: map[string]*huma.MediaType{
"application/json": {
Examples: map[string]*huma.Example{
"test": {
Value: `{"test": "example"}`,
},
},
Encoding: map[string]*huma.Encoding{
"test": {
ContentType: "application/json",
},
},
Schema: &huma.Schema{
Type: "object",
Properties: map[string]*huma.Schema{
"test": {
Type: "integer",
Minimum: &num,
},
},
},
},
},
Links: map[string]*huma.Link{
"related": {
OperationID: "another-operation",
},
},
},
},
},
},
},
Extensions: map[string]any{
"x-test": 123,
},
}
// This will marshal to JSON, then convert to YAML.
out, err := v.YAML()
require.NoError(t, err)
expected := `components:
schemas: {}
securitySchemes:
oauth2:
flows:
clientCredentials:
authorizationUrl: https://example.com/oauth2/authorize
scopes:
test: Test scope
tokenUrl: https://example.com/oauth2/token
type: oauth2
externalDocs:
url: https://example.com
info:
contact:
name: Daniel Taylor
license:
name: MIT
title: Test API
version: 1.0.0
openapi: 3.0.0
paths:
/test:
get:
responses:
"200":
content:
application/json:
encoding:
test:
contentType: application/json
examples:
test:
value: "{\"test\": \"example\"}"
schema:
properties:
test:
minimum: 1
type: integer
type: object
description: OK
links:
related:
operationId: another-operation
servers:
- url: https://example.com/{foo}
variables:
foo:
default: bar
enum:
- bar
- baz
tags:
- name: test
x-test: 123
`
require.Equal(t, expected, string(out))
}