Skip to content

Commit 8d98138

Browse files
committed
Add tests for subschemas
Tests for: - if/then/else - allOf/anyOf/oneOf/not - dependentSchemas - prefixItems - contains/maxContains/minContains - propertyNames - patternProperties - contentEncoding - contentMediaType - contentSchema
1 parent 907da67 commit 8d98138

11 files changed

+755
-0
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
package io.smallrye.openapi.runtime.scanner;
2+
3+
import static org.eclipse.microprofile.openapi.annotations.enums.SchemaType.OBJECT;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
9+
import org.eclipse.microprofile.openapi.annotations.media.DependentRequired;
10+
import org.eclipse.microprofile.openapi.annotations.media.DependentSchema;
11+
import org.eclipse.microprofile.openapi.annotations.media.PatternProperty;
12+
import org.eclipse.microprofile.openapi.annotations.media.Schema;
13+
import org.eclipse.microprofile.openapi.annotations.media.SchemaProperty;
14+
import org.eclipse.microprofile.openapi.models.OpenAPI;
15+
import org.jboss.jandex.Index;
16+
import org.junit.jupiter.api.Test;
17+
18+
public class SubschemaApplicationTest extends IndexScannerTestBase {
19+
20+
@Test
21+
public void testSubschemaApplication() throws Exception {
22+
Index index = indexOf(A.class, B.class, C.class, TestOneOf.class, TestAnyOf.class, TestAllOf.class, TestNot.class);
23+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
24+
25+
OpenAPI result = scanner.scan();
26+
27+
printToConsole(result);
28+
assertJsonEquals("components.schemas.subschema-application.json", result);
29+
}
30+
31+
@Test
32+
public void testSubschemaApplicationProperty() throws Exception {
33+
Index index = indexOf(A.class, B.class, C.class, TestOneOfProperty.class, TestAnyOfProperty.class,
34+
TestAllOfProperty.class, TestNotProperty.class);
35+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
36+
37+
OpenAPI result = scanner.scan();
38+
39+
printToConsole(result);
40+
assertJsonEquals("components.schemas.subschema-application-property.json", result);
41+
}
42+
43+
@Test
44+
public void testSubschemaIfThenElse() throws Exception {
45+
Index index = indexOf(A.class, B.class, C.class, TestIfThenElse.class);
46+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
47+
48+
OpenAPI result = scanner.scan();
49+
50+
printToConsole(result);
51+
assertJsonEquals("components.schemas.subschema-ifthenelse.json", result);
52+
}
53+
54+
@Test
55+
public void testSubschemaIfThenElseProperty() throws Exception {
56+
Index index = indexOf(A.class, B.class, C.class, TestIfThenElseProperty.class);
57+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
58+
59+
OpenAPI result = scanner.scan();
60+
61+
printToConsole(result);
62+
assertJsonEquals("components.schemas.subschema-ifthenelse-property.json", result);
63+
}
64+
65+
@Test
66+
public void testDependentSchemas() throws Exception {
67+
Index index = indexOf(A.class, B.class, TestDepedentSchemas.class);
68+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
69+
70+
OpenAPI result = scanner.scan();
71+
72+
printToConsole(result);
73+
assertJsonEquals("components.schemas.subschema-dependent-schemas.json", result);
74+
}
75+
76+
@Test
77+
public void testDependentSchemasProperty() throws Exception {
78+
Index index = indexOf(A.class, B.class, TestDepedentSchemasProperty.class);
79+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
80+
81+
OpenAPI result = scanner.scan();
82+
83+
printToConsole(result);
84+
assertJsonEquals("components.schemas.subschema-dependent-schemas-property.json", result);
85+
}
86+
87+
@Test
88+
public void testDependentRequired() throws Exception {
89+
Index index = indexOf(TestDependentRequired.class);
90+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
91+
92+
OpenAPI result = scanner.scan();
93+
94+
printToConsole(result);
95+
assertJsonEquals("components.schemas.subschema-dependent-required.json", result);
96+
}
97+
98+
@Test
99+
public void testDependentRequiredProperty() throws Exception {
100+
Index index = indexOf(TestDependentRequiredProperty.class);
101+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
102+
103+
OpenAPI result = scanner.scan();
104+
105+
printToConsole(result);
106+
assertJsonEquals("components.schemas.subschema-dependent-required-property.json", result);
107+
}
108+
109+
@Test
110+
public void testSubschemaCollections() throws Exception {
111+
Index index = indexOf(A.class, JavaTypeString.class, TestCollections.class);
112+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
113+
114+
OpenAPI result = scanner.scan();
115+
116+
printToConsole(result);
117+
assertJsonEquals("components.schemas.subschema-collections.json", result);
118+
}
119+
120+
@Test
121+
public void testSubschemaCollectionsProperty() throws Exception {
122+
Index index = indexOf(A.class, JavaTypeString.class, TestCollectionsProperty.class);
123+
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
124+
125+
OpenAPI result = scanner.scan();
126+
127+
printToConsole(result);
128+
assertJsonEquals("components.schemas.subschema-collections-property.json", result);
129+
}
130+
131+
@Schema(oneOf = { A.class, B.class, C.class })
132+
public static class TestOneOf {
133+
}
134+
135+
@Schema(anyOf = { A.class, B.class, C.class })
136+
public static class TestAnyOf {
137+
}
138+
139+
@Schema(allOf = { A.class, B.class, C.class })
140+
public static class TestAllOf {
141+
}
142+
143+
@Schema(not = A.class)
144+
public static class TestNot {
145+
}
146+
147+
@Schema(properties = {
148+
@SchemaProperty(name = "prop", oneOf = { A.class, B.class, C.class })
149+
})
150+
public static class TestOneOfProperty {
151+
}
152+
153+
@Schema(properties = {
154+
@SchemaProperty(name = "prop", anyOf = { A.class, B.class, C.class })
155+
})
156+
public static class TestAnyOfProperty {
157+
}
158+
159+
@Schema(properties = {
160+
@SchemaProperty(name = "prop", allOf = { A.class, B.class, C.class })
161+
})
162+
public static class TestAllOfProperty {
163+
}
164+
165+
@Schema(properties = @SchemaProperty(name = "prop", not = A.class))
166+
public static class TestNotProperty {
167+
}
168+
169+
@Schema(ifSchema = A.class, thenSchema = B.class, elseSchema = C.class)
170+
public static class TestIfThenElse {
171+
}
172+
173+
@Schema(properties = @SchemaProperty(name = "prop", ifSchema = A.class, thenSchema = B.class, elseSchema = C.class))
174+
public static class TestIfThenElseProperty {
175+
}
176+
177+
@Schema(dependentSchemas = {
178+
@DependentSchema(name = "field1", schema = A.class),
179+
@DependentSchema(name = "field2", schema = B.class)
180+
})
181+
public static class TestDepedentSchemas {
182+
public String field1;
183+
}
184+
185+
@Schema(properties = @SchemaProperty(name = "prop", dependentSchemas = {
186+
@DependentSchema(name = "field1", schema = A.class),
187+
@DependentSchema(name = "field2", schema = B.class)
188+
}))
189+
public static class TestDepedentSchemasProperty {
190+
}
191+
192+
@Schema(dependentRequired = {
193+
@DependentRequired(name = "field1", requires = {
194+
"field2",
195+
"field3"
196+
}),
197+
@DependentRequired(name = "field4", requires = "field5")
198+
})
199+
public static class TestDependentRequired {
200+
public String field1;
201+
public String field2;
202+
public String field3;
203+
}
204+
205+
@Schema(properties = @SchemaProperty(name = "prop", dependentRequired = {
206+
@DependentRequired(name = "field1", requires = {
207+
"field2",
208+
"field3"
209+
}),
210+
@DependentRequired(name = "field4", requires = "field5")
211+
}))
212+
public static class TestDependentRequiredProperty {
213+
}
214+
215+
@Schema
216+
public static class TestCollections {
217+
@Schema(prefixItems = { JavaTypeString.class, JavaTypeString.class })
218+
public List<String> mustStartWithTwoTypeNames;
219+
220+
@Schema(contains = JavaTypeString.class)
221+
public List<String> mustContainATypeName;
222+
223+
@Schema(contains = JavaTypeString.class, minContains = 3, maxContains = 5)
224+
public List<String> mustContain3To5TypeNames;
225+
226+
@Schema(propertyNames = JavaTypeString.class)
227+
public Map<String, String> keysMustBeTypeNames;
228+
229+
@Schema(patternProperties = {
230+
@PatternProperty(regex = "^str", schema = String.class),
231+
@PatternProperty(regex = "^int", schema = Integer.class)
232+
})
233+
public Map<String, Object> keysNamedByType;
234+
235+
@Schema(contentEncoding = "base64", contentMediaType = "application/json", contentSchema = A.class)
236+
public String encodedJson;
237+
}
238+
239+
@Schema(properties = {
240+
@SchemaProperty(name = "mustStartWithTwoTypeNames", type = SchemaType.ARRAY, implementation = String.class, prefixItems = {
241+
JavaTypeString.class,
242+
JavaTypeString.class
243+
}),
244+
@SchemaProperty(name = "mustContainATypeName", type = SchemaType.ARRAY, implementation = String.class, contains = JavaTypeString.class),
245+
@SchemaProperty(name = "mustContain3To5TypeNames", type = SchemaType.ARRAY, implementation = String.class, contains = JavaTypeString.class, minContains = 3, maxContains = 5),
246+
@SchemaProperty(name = "keysMustBeTypeNames", type = OBJECT, propertyNames = JavaTypeString.class, additionalProperties = String.class),
247+
@SchemaProperty(name = "keysNamedByType", type = OBJECT, implementation = Object.class, patternProperties = {
248+
@PatternProperty(regex = "^str", schema = String.class),
249+
@PatternProperty(regex = "^int", schema = Integer.class)
250+
}, additionalProperties = Object.class),
251+
@SchemaProperty(name = "encodedJson", implementation = String.class, contentEncoding = "base64", contentMediaType = "application/json", contentSchema = A.class)
252+
})
253+
public static class TestCollectionsProperty {
254+
}
255+
256+
@Schema(type = SchemaType.STRING, pattern = "^[A-Z][a-zA-Z0-9]*$")
257+
public static class JavaTypeString {
258+
}
259+
260+
@Schema(type = OBJECT, description = "A")
261+
public static class A {
262+
}
263+
264+
@Schema(description = "B")
265+
public static class B {
266+
}
267+
268+
@Schema(description = "C")
269+
public static class C {
270+
}
271+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"openapi": "3.0.3",
3+
"components": {
4+
"schemas": {
5+
"A": {
6+
"description": "A",
7+
"type": "object"
8+
},
9+
"B": {
10+
"description": "B",
11+
"type": "object"
12+
},
13+
"C": {
14+
"description": "C",
15+
"type": "object"
16+
},
17+
"TestAnyOfProperty": {
18+
"properties": {
19+
"prop": {
20+
"anyOf": [
21+
{
22+
"$ref": "#/components/schemas/A"
23+
},
24+
{
25+
"$ref": "#/components/schemas/B"
26+
},
27+
{
28+
"$ref": "#/components/schemas/C"
29+
}
30+
]
31+
}
32+
},
33+
"type": "object"
34+
},
35+
"TestAllOfProperty": {
36+
"properties": {
37+
"prop": {
38+
"allOf": [
39+
{
40+
"$ref": "#/components/schemas/A"
41+
},
42+
{
43+
"$ref": "#/components/schemas/B"
44+
},
45+
{
46+
"$ref": "#/components/schemas/C"
47+
}
48+
]
49+
}
50+
},
51+
"type": "object"
52+
},
53+
"TestNotProperty": {
54+
"properties": {
55+
"prop": {
56+
"not": {
57+
"$ref": "#/components/schemas/A"
58+
}
59+
}
60+
},
61+
"type": "object"
62+
},
63+
"TestOneOfProperty": {
64+
"properties": {
65+
"prop": {
66+
"oneOf": [
67+
{
68+
"$ref": "#/components/schemas/A"
69+
},
70+
{
71+
"$ref": "#/components/schemas/B"
72+
},
73+
{
74+
"$ref": "#/components/schemas/C"
75+
}
76+
]
77+
}
78+
},
79+
"type": "object"
80+
}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)