Skip to content

Commit 16d403e

Browse files
committed
Add tests for ModelResolver / fix existing tests
1 parent 6c95bfb commit 16d403e

File tree

9 files changed

+143
-2
lines changed

9 files changed

+143
-2
lines changed

modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket4679Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Ticket4679Test extends SwaggerTestBase{
1515
public void testCustomSchemaImplementation() {
1616

1717
String expectedYaml = "ModelWithCustomSchemaImplementationInProperty:\n" +
18+
" type: object\n" +
1819
" properties:\n" +
1920
" exampleField:\n" +
2021
" type: integer\n" +
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.swagger.v3.core.resolving.resources;
2+
3+
import io.swagger.v3.oas.annotations.media.ArraySchema;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class TestArrayType {
9+
10+
private Integer id;
11+
12+
@ArraySchema(maxItems = 10)
13+
private List<String> names;
14+
15+
public Integer getId() {
16+
return id;
17+
}
18+
19+
public void setId(Integer id) {
20+
this.id = id;
21+
}
22+
23+
public List<String> getNames() {
24+
return names;
25+
}
26+
27+
public void setNames(List<String> names) {
28+
this.names = names;
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.swagger.v3.core.resolving.resources;
2+
3+
public class TestObject4715 {
4+
5+
private String foo;
6+
7+
private String bar;
8+
9+
private Integer id;
10+
11+
public String getFoo() {
12+
return foo;
13+
}
14+
15+
public void setFoo(String foo) {
16+
this.foo = foo;
17+
}
18+
19+
public String getBar() {
20+
return bar;
21+
}
22+
23+
public void setBar(String bar) {
24+
this.bar = bar;
25+
}
26+
27+
public Integer getId() {
28+
return id;
29+
}
30+
31+
public void setId(Integer id) {
32+
this.id = id;
33+
}
34+
}

modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/v31/ModelResolverOAS31Test.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import io.swagger.v3.core.jackson.ModelResolver;
66
import io.swagger.v3.core.matchers.SerializationMatchers;
77
import io.swagger.v3.core.resolving.SwaggerTestBase;
8+
import io.swagger.v3.core.resolving.resources.TestArrayType;
9+
import io.swagger.v3.core.resolving.resources.TestObject4715;
810
import io.swagger.v3.core.resolving.v31.model.AnnotatedArray;
911
import io.swagger.v3.core.resolving.v31.model.ModelWithDependentSchema;
1012
import io.swagger.v3.core.resolving.v31.model.ModelWithOAS31Stuff;
@@ -39,6 +41,7 @@ public void testOAS31Fields() {
3941
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
4042
Schema model = context.resolve(new AnnotatedType(ModelWithOAS31Stuff.class));
4143
SerializationMatchers.assertEqualsToYaml31(context.getDefinedModels(), "Address:\n" +
44+
" type: object\n" +
4245
" if:\n" +
4346
" $ref: '#/components/schemas/AnnotatedCountry'\n" +
4447
" then:\n" +
@@ -59,17 +62,20 @@ public void testOAS31Fields() {
5962
" propertyNames:\n" +
6063
" $ref: '#/components/schemas/PropertyNamesPattern'\n" +
6164
"AnnotatedCountry:\n" +
65+
" type: object\n" +
6266
" properties:\n" +
6367
" country:\n" +
6468
" const: United States\n" +
6569
"Client:\n" +
70+
" type: object\n" +
6671
" properties:\n" +
6772
" name:\n" +
6873
" type: string\n" +
6974
" creditCard:\n" +
7075
" type: integer\n" +
7176
" format: int32\n" +
7277
"CreditCard:\n" +
78+
" type: object\n" +
7379
" properties:\n" +
7480
" billingAddress:\n" +
7581
" type: string\n" +
@@ -124,6 +130,7 @@ public void testOAS31Fields() {
124130
" properties:\n" +
125131
" extraObject: {}\n" +
126132
"MultipleBaseBean:\n" +
133+
" type: object\n" +
127134
" description: MultipleBaseBean\n" +
128135
" properties:\n" +
129136
" beanType:\n" +
@@ -152,14 +159,17 @@ public void testOAS31Fields() {
152159
" format: int32\n" +
153160
" description: MultipleSub2Bean\n" +
154161
"PostalCodeNumberPattern:\n" +
162+
" type: object\n" +
155163
" properties:\n" +
156164
" postalCode:\n" +
157165
" pattern: \"[0-9]{5}(-[0-9]{4})?\"\n" +
158166
"PostalCodePattern:\n" +
167+
" type: object\n" +
159168
" properties:\n" +
160169
" postalCode:\n" +
161170
" pattern: \"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]\"\n" +
162171
"PropertyNamesPattern:\n" +
172+
" type: object\n" +
163173
" pattern: \"^[A-Za-z_][A-Za-z0-9_]*$\"\n");
164174
}
165175

@@ -170,10 +180,12 @@ public void testDependentSchemasAnnotation() {
170180
io.swagger.v3.oas.models.media.Schema model = context.resolve(new AnnotatedType(ModelWithDependentSchema.class));
171181

172182
SerializationMatchers.assertEqualsToYaml31(context.getDefinedModels(), "BooleanFakeClass:\n" +
183+
" type: object\n" +
173184
" properties:\n" +
174185
" type:\n" +
175186
" type: boolean\n" +
176187
"ModelWithDependentSchema:\n" +
188+
" type: object\n" +
177189
" dependentSchemas:\n" +
178190
" value:\n" +
179191
" properties:\n" +
@@ -187,4 +199,36 @@ public void testDependentSchemasAnnotation() {
187199
" format: int32\n");
188200
}
189201

202+
@Test(description = "Top level type:object should appear in OAS31")
203+
public void testObjectTypeSchemaOAS31(){
204+
final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(true);
205+
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
206+
io.swagger.v3.oas.models.media.Schema model = context.resolve(new AnnotatedType(TestObject4715.class));
207+
SerializationMatchers.assertEqualsToYaml31(model, "type: object\n" +
208+
"properties:\n" +
209+
" foo:\n" +
210+
" type: string\n" +
211+
" bar:\n" +
212+
" type: string\n" +
213+
" id:\n" +
214+
" type: integer\n" +
215+
" format: int32");
216+
}
217+
218+
@Test
219+
public void testFieldArraySchemaAnnotation() {
220+
final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(true);
221+
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
222+
io.swagger.v3.oas.models.media.Schema model = context.resolve(new AnnotatedType(TestArrayType.class));
223+
SerializationMatchers.assertEqualsToYaml31(model, " type: object\n" +
224+
" properties:\n" +
225+
" id:\n" +
226+
" type: integer\n" +
227+
" format: int32\n" +
228+
" names:\n" +
229+
" type: array\n" +
230+
" items:\n" +
231+
" type: string\n" +
232+
" maxItems: 10");
233+
}
190234
}

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3378,6 +3378,7 @@ public void testOas31Petstore() {
33783378
"components:\n" +
33793379
" schemas:\n" +
33803380
" Bar:\n" +
3381+
" type: object\n" +
33813382
" deprecated: true\n" +
33823383
" description: Bar\n" +
33833384
" properties:\n" +
@@ -3394,6 +3395,7 @@ public void testOas31Petstore() {
33943395
" - string\n" +
33953396
" format: int32\n" +
33963397
" Category:\n" +
3398+
" type: object\n" +
33973399
" properties:\n" +
33983400
" id:\n" +
33993401
" type: integer\n" +
@@ -3403,6 +3405,7 @@ public void testOas31Petstore() {
34033405
" xml:\n" +
34043406
" name: Category\n" +
34053407
" Foo:\n" +
3408+
" type: object\n" +
34063409
" deprecated: true\n" +
34073410
" description: Foo\n" +
34083411
" properties:\n" +
@@ -3420,6 +3423,7 @@ public void testOas31Petstore() {
34203423
" - object\n" +
34213424
" format: int32\n" +
34223425
" IfSchema:\n" +
3426+
" type: object\n" +
34233427
" deprecated: true\n" +
34243428
" description: if schema\n" +
34253429
" properties:\n" +
@@ -3437,6 +3441,7 @@ public void testOas31Petstore() {
34373441
" - object\n" +
34383442
" format: int32\n" +
34393443
" Pet:\n" +
3444+
" type: object\n" +
34403445
" properties:\n" +
34413446
" id:\n" +
34423447
" type: integer\n" +
@@ -3470,6 +3475,7 @@ public void testOas31Petstore() {
34703475
" xml:\n" +
34713476
" name: Pet\n" +
34723477
" Tag:\n" +
3478+
" type: object\n" +
34733479
" properties:\n" +
34743480
" id:\n" +
34753481
" type: integer\n" +
@@ -3510,6 +3516,7 @@ public void test31RefSiblings() {
35103516
"components:\n" +
35113517
" schemas:\n" +
35123518
" Foo:\n" +
3519+
" type: object\n" +
35133520
" deprecated: true\n" +
35143521
" description: Foo\n" +
35153522
" properties:\n" +
@@ -3527,14 +3534,16 @@ public void test31RefSiblings() {
35273534
" - object\n" +
35283535
" format: int32\n" +
35293536
" SimpleTag:\n" +
3537+
" type: object\n" +
35303538
" properties:\n" +
35313539
" annotated:\n" +
35323540
" $ref: '#/components/schemas/SimpleCategory'\n" +
35333541
" description: child description\n" +
35343542
" properties:\n" +
35353543
" foo:\n" +
35363544
" $ref: '#/components/schemas/Foo'\n" +
3537-
" SimpleCategory: {}\n";
3545+
" SimpleCategory:\n" +
3546+
" type: object\n" ;
35383547
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
35393548
}
35403549

@@ -3558,12 +3567,14 @@ public void testSiblings() {
35583567
"components:\n" +
35593568
" schemas:\n" +
35603569
" Category:\n" +
3570+
" type: object\n" +
35613571
" description: parent\n" +
35623572
" properties:\n" +
35633573
" id:\n" +
35643574
" type: integer\n" +
35653575
" format: int64\n" +
35663576
" Pet:\n" +
3577+
" type: object\n" +
35673578
" description: Pet\n" +
35683579
" properties:\n" +
35693580
" category:\n" +
@@ -3606,6 +3617,7 @@ public void testSiblingsOnResource() {
36063617
"components:\n" +
36073618
" schemas:\n" +
36083619
" PetSimple:\n" +
3620+
" type: object\n" +
36093621
" description: Pet\n";
36103622
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
36113623
}
@@ -3654,6 +3666,7 @@ public void testSiblingsOnResourceResponse() {
36543666
"components:\n" +
36553667
" schemas:\n" +
36563668
" PetSimple:\n" +
3669+
" type: object\n" +
36573670
" description: Pet\n";
36583671
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
36593672
}
@@ -3699,6 +3712,7 @@ public void testSiblingsOnResourceRequestBody() {
36993712
"components:\n" +
37003713
" schemas:\n" +
37013714
" PetSimple:\n" +
3715+
" type: object\n" +
37023716
" description: Pet\n";
37033717
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
37043718
}
@@ -3775,6 +3789,7 @@ public void testSiblingsOnResourceRequestBodyMultiple() {
37753789
"components:\n" +
37763790
" schemas:\n" +
37773791
" PetSimple:\n" +
3792+
" type: object\n" +
37783793
" description: Pet\n";
37793794
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
37803795
}
@@ -3838,12 +3853,14 @@ public void testSiblingsOnProperty() {
38383853
"components:\n" +
38393854
" schemas:\n" +
38403855
" Category:\n" +
3856+
" type: object\n" +
38413857
" description: parent\n" +
38423858
" properties:\n" +
38433859
" id:\n" +
38443860
" type: integer\n" +
38453861
" format: int64\n" +
38463862
" Pet:\n" +
3863+
" type: object\n" +
38473864
" description: Pet\n" +
38483865
" properties:\n" +
38493866
" category:\n" +
@@ -3934,6 +3951,7 @@ public void testMisc31() {
39343951
" creditCard:\n" +
39353952
" $ref: '#/components/schemas/CreditCard'\n" +
39363953
" MultipleBaseBean:\n" +
3954+
" type: object\n" +
39373955
" description: MultipleBaseBean\n" +
39383956
" properties:\n" +
39393957
" beanType:\n" +
@@ -3962,6 +3980,7 @@ public void testMisc31() {
39623980
" format: int32\n" +
39633981
" description: MultipleSub2Bean\n" +
39643982
" Address:\n" +
3983+
" type: object\n" +
39653984
" if:\n" +
39663985
" $ref: '#/components/schemas/AnnotatedCountry'\n" +
39673986
" then:\n" +
@@ -3982,22 +4001,27 @@ public void testMisc31() {
39824001
" propertyNames:\n" +
39834002
" $ref: '#/components/schemas/PropertyNamesPattern'\n" +
39844003
" AnnotatedCountry:\n" +
4004+
" type: object\n" +
39854005
" properties:\n" +
39864006
" country:\n" +
39874007
" const: United States\n" +
39884008
" CreditCard:\n" +
4009+
" type: object\n" +
39894010
" properties:\n" +
39904011
" billingAddress:\n" +
39914012
" type: string\n" +
39924013
" PostalCodeNumberPattern:\n" +
4014+
" type: object\n" +
39934015
" properties:\n" +
39944016
" postalCode:\n" +
39954017
" pattern: \"[0-9]{5}(-[0-9]{4})?\"\n" +
39964018
" PostalCodePattern:\n" +
4019+
" type: object\n" +
39974020
" properties:\n" +
39984021
" postalCode:\n" +
39994022
" pattern: \"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]\"\n" +
40004023
" PropertyNamesPattern:\n" +
4024+
" type: object\n" +
40014025
" pattern: \"^[A-Za-z_][A-Za-z0-9_]*$\"\n";
40024026
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
40034027
}

modules/swagger-jaxrs2/src/test/resources/petstore/WebHookResource.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ openapi: 3.1.0
22
components:
33
schemas:
44
SubscriptionResponse:
5+
type: object
56
properties:
67
subscriptionId:
78
type: string

modules/swagger-jaxrs2/src/test/resources/petstore/callbacks/ComplexCallback31Resource.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ paths:
5050
components:
5151
schemas:
5252
User:
53+
type: object
5354
properties:
5455
id:
5556
type: integer

0 commit comments

Comments
 (0)