Skip to content

Commit 9f57589

Browse files
committed
schema resolution options - Phase 3: global all-of-ref
1 parent 744f283 commit 9f57589

File tree

6 files changed

+141
-11
lines changed

6 files changed

+141
-11
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,10 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
682682

683683
Annotation[] ctxAnnotation31 = null;
684684

685-
if (Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) || openapi31) {
685+
if (
686+
Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) ||
687+
Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution) ||
688+
openapi31) {
686689
List<Annotation> ctxAnnotations31List = new ArrayList<>();
687690
if (annotations != null) {
688691
for (Annotation a : annotations) {
@@ -692,8 +695,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
692695
ctxAnnotations31List.add(a);
693696
}
694697
if ((ctxSchema != null) && (!ctxSchema.implementation().equals(Void.class) || StringUtils.isNotEmpty(ctxSchema.type()))) {
695-
ctxAnnotations31List.add(a);
696-
}
698+
ctxAnnotations31List.add(a);
699+
}
697700
}
698701
ctxAnnotation31 = ctxAnnotations31List.toArray(new Annotation[ctxAnnotations31List.size()]);
699702
}
@@ -708,7 +711,10 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
708711
.schemaProperty(true)
709712
.components(annotatedType.getComponents())
710713
.propertyName(propName);
711-
if (Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) || openapi31) {
714+
if (
715+
Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) ||
716+
Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution) ||
717+
openapi31) {
712718
aType.ctxAnnotations(ctxAnnotation31);
713719
} else {
714720
aType.ctxAnnotations(annotations);
@@ -740,7 +746,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
740746
property = reResolvedProperty.get();
741747
}
742748

743-
} else if (Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution)) {
749+
} else if (Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution)) {
744750
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, null, schemaResolution, context);
745751
if (reResolvedProperty.isPresent()) {
746752
ctxProperty = reResolvedProperty.get();
@@ -795,6 +801,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
795801
property = new Schema()
796802
.addAllOfItem(ctxProperty)
797803
.addAllOfItem(new Schema().$ref(constructRef(pName)));
804+
} else if (Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution) && ctxProperty != null) {
805+
property = ctxProperty.addAllOfItem(new Schema().$ref(constructRef(pName)));
798806
} else {
799807
property = new Schema().$ref(constructRef(pName));
800808
}
@@ -817,6 +825,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
817825
property = new Schema()
818826
.addAllOfItem(ctxProperty)
819827
.addAllOfItem(new Schema().$ref(StringUtils.isNotEmpty(property.get$ref()) ? property.get$ref() : property.getName()));
828+
} else if (Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution) && ctxProperty != null) {
829+
property = ctxProperty
830+
.addAllOfItem(new Schema().$ref(StringUtils.isNotEmpty(property.get$ref()) ? property.get$ref() : property.getName()));
820831
} else {
821832
property = new Schema().$ref(StringUtils.isNotEmpty(property.get$ref()) ? property.get$ref() : property.getName());
822833
}

modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ public static Optional<Schema> getSchemaFromAnnotation(
621621
} else {
622622
schemaObject = new Schema();
623623
}
624-
} else if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution)) {
624+
} else if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(schemaResolution)) {
625625
if (existingSchema == null) {
626626
schemaObject = new Schema();
627627
} else {

modules/swagger-core/src/main/java/io/swagger/v3/core/util/ParameterProcessor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static Parameter applyAnnotations(
7676
io.swagger.v3.oas.annotations.media.ArraySchema ctxArraySchema = AnnotationsUtils.getArraySchemaAnnotation(annotations.toArray(new Annotation[0]));
7777
Annotation[] ctxAnnotation31 = null;
7878

79-
if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution)) {
79+
if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(schemaResolution)) {
8080
List<Annotation> ctxAnnotations31List = new ArrayList<>();
8181
if (annotations != null) {
8282
for (Annotation a : annotations) {
@@ -95,7 +95,7 @@ public static Parameter applyAnnotations(
9595
.skipOverride(true)
9696
.jsonViewAnnotation(jsonViewAnnotation);
9797

98-
if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution)) {
98+
if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(schemaResolution)) {
9999
annotatedType.ctxAnnotations(ctxAnnotation31);
100100
} else {
101101
annotatedType.ctxAnnotations(reworkedAnnotations.toArray(new Annotation[reworkedAnnotations.size()]));
@@ -106,7 +106,7 @@ public static Parameter applyAnnotations(
106106
if (resolvedSchema.schema != null) {
107107
Schema resSchema = AnnotationsUtils.clone(resolvedSchema.schema, openapi31);
108108
Schema ctxSchemaObject = null;
109-
if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution)) {
109+
if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(schemaResolution)) {
110110
Optional<Schema> reResolvedSchema = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, null, schemaResolution, null);
111111
if (reResolvedSchema.isPresent()) {
112112
ctxSchemaObject = reResolvedSchema.get();
@@ -121,6 +121,9 @@ public static Parameter applyAnnotations(
121121
resSchema = new Schema()
122122
.addAllOfItem(ctxSchemaObject)
123123
.addAllOfItem(resolvedSchema.schema);
124+
} else if (Schema.SchemaResolution.ALL_OF_REF.equals(schemaResolution) && ctxSchemaObject != null) {
125+
resSchema = ctxSchemaObject
126+
.addAllOfItem(resolvedSchema.schema);
124127
}
125128
parameter.setSchema(resSchema);
126129
}

modules/swagger-jaxrs2/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@
9696
<artifactId>maven-surefire-plugin</artifactId>
9797
<version>${surefire-version}</version>
9898
<configuration>
99-
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
99+
<argLine>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 --add-opens java.base/java.lang=ALL-UNNAMED</argLine>
100+
<forkCount>0</forkCount>
100101
</configuration>
101102
</plugin>
102103
<plugin>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package io.swagger.v3.jaxrs2;
2+
3+
import io.swagger.v3.core.converter.ModelConverters;
4+
import io.swagger.v3.jaxrs2.matchers.SerializationMatchers;
5+
import io.swagger.v3.jaxrs2.schemaResolution.SchemaResolutionResource;
6+
import io.swagger.v3.oas.integration.SwaggerConfiguration;
7+
import io.swagger.v3.oas.models.OpenAPI;
8+
import io.swagger.v3.oas.models.media.Schema;
9+
import org.testng.annotations.Test;
10+
11+
public class SchemaResolutionAllOfRefTest {
12+
13+
@Test
14+
public void testSchemaResolutionAllOfRef() {
15+
ModelConverters.reset();
16+
Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).schemaResolution(Schema.SchemaResolution.ALL_OF_REF));
17+
OpenAPI openAPI = reader.read(SchemaResolutionResource.class);
18+
String yaml = "openapi: 3.0.1\n" +
19+
"paths:\n" +
20+
" /test/inlineSchemaFirst:\n" +
21+
" get:\n" +
22+
" operationId: inlineSchemaFirst\n" +
23+
" responses:\n" +
24+
" default:\n" +
25+
" description: default response\n" +
26+
" content:\n" +
27+
" '*/*':\n" +
28+
" schema:\n" +
29+
" $ref: \"#/components/schemas/InlineSchemaFirst\"\n" +
30+
" /test/inlineSchemaSecond:\n" +
31+
" get:\n" +
32+
" operationId: inlineSchemaSecond\n" +
33+
" requestBody:\n" +
34+
" content:\n" +
35+
" '*/*':\n" +
36+
" schema:\n" +
37+
" description: InlineSchemaSecond API\n" +
38+
" allOf:\n" +
39+
" - $ref: \"#/components/schemas/InlineSchemaSecond\"\n" +
40+
" responses:\n" +
41+
" default:\n" +
42+
" description: default response\n" +
43+
" content:\n" +
44+
" '*/*':\n" +
45+
" schema:\n" +
46+
" $ref: \"#/components/schemas/InlineSchemaSecond\"\n" +
47+
"components:\n" +
48+
" schemas:\n" +
49+
" InlineSchemaFirst:\n" +
50+
" type: object\n" +
51+
" properties:\n" +
52+
" property1:\n" +
53+
" description: InlineSchemaFirst property 1\n" +
54+
" nullable: true\n" +
55+
" allOf:\n" +
56+
" - $ref: \"#/components/schemas/InlineSchemaPropertyFirst\"\n" +
57+
" property2:\n" +
58+
" description: ' InlineSchemaFirst property 2'\n" +
59+
" example: example 2\n" +
60+
" allOf:\n" +
61+
" - $ref: \"#/components/schemas/InlineSchemaPropertyFirst\"\n" +
62+
" InlineSchemaPropertyFirst:\n" +
63+
" type: object\n" +
64+
" properties:\n" +
65+
" bar:\n" +
66+
" type: string\n" +
67+
" description: property\n" +
68+
" example: example\n" +
69+
" InlineSchemaPropertySecond:\n" +
70+
" type: object\n" +
71+
" properties:\n" +
72+
" bar:\n" +
73+
" $ref: \"#/components/schemas/InlineSchemaSimple\"\n" +
74+
" description: propertysecond\n" +
75+
" example: examplesecond\n" +
76+
" InlineSchemaPropertySimple:\n" +
77+
" type: object\n" +
78+
" properties:\n" +
79+
" bar:\n" +
80+
" type: string\n" +
81+
" description: property\n" +
82+
" InlineSchemaSecond:\n" +
83+
" type: object\n" +
84+
" properties:\n" +
85+
" foo:\n" +
86+
" type: string\n" +
87+
" propertySecond1:\n" +
88+
" description: InlineSchemaSecond property 1\n" +
89+
" nullable: true\n" +
90+
" allOf:\n" +
91+
" - $ref: \"#/components/schemas/InlineSchemaPropertySecond\"\n" +
92+
" property2:\n" +
93+
" description: InlineSchemaSecond property 2\n" +
94+
" example: InlineSchemaSecond example 2\n" +
95+
" allOf:\n" +
96+
" - $ref: \"#/components/schemas/InlineSchemaPropertyFirst\"\n" +
97+
" InlineSchemaSimple:\n" +
98+
" type: object\n" +
99+
" properties:\n" +
100+
" property1:\n" +
101+
" description: property 1\n" +
102+
" allOf:\n" +
103+
" - $ref: \"#/components/schemas/InlineSchemaPropertySimple\"\n" +
104+
" property2:\n" +
105+
" description: property 2\n" +
106+
" example: example\n" +
107+
" allOf:\n" +
108+
" - $ref: \"#/components/schemas/InlineSchemaPropertySimple\"\n";
109+
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
110+
ModelConverters.reset();
111+
}
112+
}

modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/Schema.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public enum SchemaResolution {
5252
@JsonProperty("inline")
5353
INLINE("inline"),
5454
@JsonProperty("all-of")
55-
ALL_OF("all-of");
55+
ALL_OF("all-of"),
56+
@JsonProperty("all-of-ref")
57+
ALL_OF_REF("all-of-ref");
58+
5659
private String value;
5760

5861
SchemaResolution(String value) {

0 commit comments

Comments
 (0)