|
1 | 1 | package main |
2 | | - |
3 | | -import ( |
4 | | - "encoding/json" |
5 | | - "testing" |
6 | | - |
7 | | - "github.com/getkin/kin-openapi/openapi3" |
8 | | - "github.com/stretchr/testify/assert" |
9 | | -) |
10 | | - |
11 | | -type getTypedExampleTestData struct { |
12 | | - name string |
13 | | - generateInputSchema func() *openapi3.Schema |
14 | | - validateResult func(*testing.T, string) |
15 | | -} |
16 | | - |
17 | | -func Test_GetTypedExampleShouldGetFromExampleField(t *testing.T) { |
18 | | - exampleData := map[string]string{"name1": "value1", "name2": "value2"} |
19 | | - |
20 | | - mediaType := openapi3.NewMediaType() |
21 | | - mediaType.WithExample("example1", exampleData) |
22 | | - |
23 | | - selectedExample, err := getTypedExample(mediaType) |
24 | | - assert.Nil(t, err) |
25 | | - |
26 | | - selectedExampleJSON, err := json.Marshal(selectedExample) |
27 | | - assert.Nil(t, err) |
28 | | - assert.NotEmpty(t, string(selectedExampleJSON)) |
29 | | - assert.Equal(t, `{"name1":"value1","name2":"value2"}`, string(selectedExampleJSON)) |
30 | | -} |
31 | | - |
32 | | -var getTypedExampleTestDataEntries = []getTypedExampleTestData{ |
33 | | - getTypedExampleTestData{ |
34 | | - name: "SchemaExampleField", |
35 | | - generateInputSchema: func() *openapi3.Schema { |
36 | | - exampleData := map[string]string{"name1": "value1", "name2": "value2"} |
37 | | - schema := openapi3.NewSchema() |
38 | | - schema.Example = exampleData |
39 | | - return schema |
40 | | - }, |
41 | | - validateResult: func(t *testing.T, s string) { |
42 | | - assert.Equal(t, `{"name1":"value1","name2":"value2"}`, s) |
43 | | - }, |
44 | | - }, |
45 | | - getTypedExampleTestData{ |
46 | | - name: "SchemaPropertiesExampleField", |
47 | | - generateInputSchema: func() *openapi3.Schema { |
48 | | - schema := openapi3.NewSchema() |
49 | | - |
50 | | - parameterSchema1 := openapi3.NewStringSchema() |
51 | | - parameterSchema1.Example = "testvalue" |
52 | | - |
53 | | - parameterSchema2 := openapi3.NewObjectSchema() |
54 | | - nestedParameterSchema := openapi3.NewBoolSchema() |
55 | | - nestedParameterSchema.Example = true |
56 | | - parameterSchema2.WithProperty("nestedProperty", nestedParameterSchema) |
57 | | - |
58 | | - schema.WithProperties(map[string]*openapi3.Schema{ |
59 | | - "name1": parameterSchema1, |
60 | | - "name2": parameterSchema2, |
61 | | - }) |
62 | | - return schema |
63 | | - }, |
64 | | - validateResult: func(t *testing.T, s string) { |
65 | | - assert.Equal(t, `{"name1":"testvalue","name2":{"nestedProperty":true}}`, s) |
66 | | - }, |
67 | | - }, |
68 | | - getTypedExampleTestData{ |
69 | | - name: "SchemaArrayItemsStringExampleField", |
70 | | - generateInputSchema: func() *openapi3.Schema { |
71 | | - schema := openapi3.NewArraySchema() |
72 | | - itemSchema := openapi3.NewStringSchema() |
73 | | - itemSchema.Example = "testvalue" |
74 | | - schema.WithItems(itemSchema) |
75 | | - return schema |
76 | | - }, |
77 | | - validateResult: func(t *testing.T, s string) { |
78 | | - assert.Equal(t, `["testvalue"]`, s) |
79 | | - }, |
80 | | - }, |
81 | | - getTypedExampleTestData{ |
82 | | - name: "SchemaArrayItemsObjectExampleField", |
83 | | - generateInputSchema: func() *openapi3.Schema { |
84 | | - schema := openapi3.NewArraySchema() |
85 | | - |
86 | | - itemSchema := openapi3.NewObjectSchema() |
87 | | - |
88 | | - parameterSchema1 := openapi3.NewStringSchema() |
89 | | - parameterSchema1.Example = "testvalue" |
90 | | - |
91 | | - itemSchema.WithProperties(map[string]*openapi3.Schema{ |
92 | | - "name1": parameterSchema1, |
93 | | - }) |
94 | | - |
95 | | - schema.WithItems(itemSchema) |
96 | | - return schema |
97 | | - }, |
98 | | - validateResult: func(t *testing.T, s string) { |
99 | | - assert.Equal(t, `[{"name1":"testvalue"}]`, s) |
100 | | - }, |
101 | | - }, |
102 | | - getTypedExampleTestData{ |
103 | | - name: "StringSchemaWithExample", |
104 | | - generateInputSchema: func() *openapi3.Schema { |
105 | | - schema := openapi3.NewStringSchema() |
106 | | - schema.Example = "examplestr" |
107 | | - return schema |
108 | | - }, |
109 | | - validateResult: func(t *testing.T, s string) { |
110 | | - assert.Equal(t, `"examplestr"`, s) |
111 | | - }, |
112 | | - }, |
113 | | - getTypedExampleTestData{ |
114 | | - name: "StringSchemaWithoutExample", |
115 | | - generateInputSchema: func() *openapi3.Schema { |
116 | | - schema := openapi3.NewStringSchema() |
117 | | - return schema |
118 | | - }, |
119 | | - validateResult: func(t *testing.T, s string) { |
120 | | - assert.Equal(t, `"string"`, s) |
121 | | - }, |
122 | | - }, |
123 | | - getTypedExampleTestData{ |
124 | | - name: "BooleanSchema", |
125 | | - generateInputSchema: func() *openapi3.Schema { |
126 | | - schema := openapi3.NewBoolSchema() |
127 | | - return schema |
128 | | - }, |
129 | | - validateResult: func(t *testing.T, s string) { |
130 | | - assert.Equal(t, `true`, s) |
131 | | - }, |
132 | | - }, |
133 | | - getTypedExampleTestData{ |
134 | | - name: "IntegerSchemaWithoutExample", |
135 | | - generateInputSchema: func() *openapi3.Schema { |
136 | | - schema := openapi3.NewIntegerSchema() |
137 | | - return schema |
138 | | - }, |
139 | | - validateResult: func(t *testing.T, s string) { |
140 | | - assert.Equal(t, `0`, s) |
141 | | - }, |
142 | | - }, |
143 | | - getTypedExampleTestData{ |
144 | | - name: "IntegerSchemaWithExample", |
145 | | - generateInputSchema: func() *openapi3.Schema { |
146 | | - schema := openapi3.NewIntegerSchema() |
147 | | - schema.Example = 1 |
148 | | - return schema |
149 | | - }, |
150 | | - validateResult: func(t *testing.T, s string) { |
151 | | - assert.Equal(t, `1`, s) |
152 | | - }, |
153 | | - }, |
154 | | - getTypedExampleTestData{ |
155 | | - name: "NumberSchemaWithoutExample", |
156 | | - generateInputSchema: func() *openapi3.Schema { |
157 | | - schema := openapi3.NewSchema() |
158 | | - schema.Type = "number" |
159 | | - return schema |
160 | | - }, |
161 | | - validateResult: func(t *testing.T, s string) { |
162 | | - assert.Equal(t, `0`, s) |
163 | | - }, |
164 | | - }, |
165 | | - getTypedExampleTestData{ |
166 | | - name: "NumberSchemaWithExample", |
167 | | - generateInputSchema: func() *openapi3.Schema { |
168 | | - schema := openapi3.NewSchema() |
169 | | - schema.Type = "number" |
170 | | - schema.Example = 1.1 |
171 | | - return schema |
172 | | - }, |
173 | | - validateResult: func(t *testing.T, s string) { |
174 | | - assert.Equal(t, `1.1`, s) |
175 | | - }, |
176 | | - }, |
177 | | -} |
178 | | - |
179 | | -func Test_GetTypedExampleTest(t *testing.T) { |
180 | | - |
181 | | - for _, td := range getTypedExampleTestDataEntries { |
182 | | - t.Logf("testcase: '%s'", td.name) |
183 | | - |
184 | | - mediaType := openapi3.NewMediaType() |
185 | | - mediaType.WithSchema(td.generateInputSchema()) |
186 | | - |
187 | | - selectedExample, err := getTypedExample(mediaType) |
188 | | - assert.Nil(t, err) |
189 | | - |
190 | | - selectedExampleJSON, err := json.Marshal(selectedExample) |
191 | | - assert.Nil(t, err) |
192 | | - assert.NotEmpty(t, string(selectedExampleJSON)) |
193 | | - td.validateResult(t, string(selectedExampleJSON)) |
194 | | - } |
195 | | -} |
196 | | - |
197 | | -func Test_GetTypedExampleShouldReturnErrorIfCannotGetFullExample(t *testing.T) { |
198 | | - schema := openapi3.NewSchema() |
199 | | - |
200 | | - parameterSchema1 := openapi3.NewStringSchema() |
201 | | - parameterSchema1.Example = "testvalue" |
202 | | - |
203 | | - parameterSchema2 := openapi3.NewObjectSchema() |
204 | | - nestedParameterSchemaWithoutExample := openapi3.NewSchema() |
205 | | - parameterSchema2.WithProperty("nestedProperty", nestedParameterSchemaWithoutExample) |
206 | | - |
207 | | - schema.WithProperties(map[string]*openapi3.Schema{ |
208 | | - "name1": parameterSchema1, |
209 | | - "name2": parameterSchema2, |
210 | | - }) |
211 | | - |
212 | | - mediaType := openapi3.NewMediaType() |
213 | | - mediaType.WithSchema(schema) |
214 | | - |
215 | | - selectedExample, err := getTypedExample(mediaType) |
216 | | - assert.NotNil(t, err) |
217 | | - assert.Nil(t, selectedExample) |
218 | | -} |
0 commit comments