Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema resolution options - Phase 3: global all-of-ref #4747

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,10 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context

Annotation[] ctxAnnotation31 = null;

if (Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) || openapi31) {
if (
Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) ||
Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution) ||
openapi31) {
List<Annotation> ctxAnnotations31List = new ArrayList<>();
if (annotations != null) {
for (Annotation a : annotations) {
Expand All @@ -692,8 +695,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
ctxAnnotations31List.add(a);
}
if ((ctxSchema != null) && (!ctxSchema.implementation().equals(Void.class) || StringUtils.isNotEmpty(ctxSchema.type()))) {
ctxAnnotations31List.add(a);
}
ctxAnnotations31List.add(a);
}
}
ctxAnnotation31 = ctxAnnotations31List.toArray(new Annotation[ctxAnnotations31List.size()]);
}
Expand All @@ -708,7 +711,10 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
.schemaProperty(true)
.components(annotatedType.getComponents())
.propertyName(propName);
if (Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) || openapi31) {
if (
Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) ||
Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution) ||
openapi31) {
aType.ctxAnnotations(ctxAnnotation31);
} else {
aType.ctxAnnotations(annotations);
Expand Down Expand Up @@ -740,7 +746,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
property = reResolvedProperty.get();
}

} else if (Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution)) {
} else if (Schema.SchemaResolution.ALL_OF.equals(this.schemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution)) {
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, null, schemaResolution, context);
if (reResolvedProperty.isPresent()) {
ctxProperty = reResolvedProperty.get();
Expand Down Expand Up @@ -795,6 +801,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
property = new Schema()
.addAllOfItem(ctxProperty)
.addAllOfItem(new Schema().$ref(constructRef(pName)));
} else if (Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution) && ctxProperty != null) {
property = ctxProperty.addAllOfItem(new Schema().$ref(constructRef(pName)));
} else {
property = new Schema().$ref(constructRef(pName));
}
Expand All @@ -817,6 +825,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
property = new Schema()
.addAllOfItem(ctxProperty)
.addAllOfItem(new Schema().$ref(StringUtils.isNotEmpty(property.get$ref()) ? property.get$ref() : property.getName()));
} else if (Schema.SchemaResolution.ALL_OF_REF.equals(this.schemaResolution) && ctxProperty != null) {
property = ctxProperty
.addAllOfItem(new Schema().$ref(StringUtils.isNotEmpty(property.get$ref()) ? property.get$ref() : property.getName()));
} else {
property = new Schema().$ref(StringUtils.isNotEmpty(property.get$ref()) ? property.get$ref() : property.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public static Optional<Schema> getSchemaFromAnnotation(
} else {
schemaObject = new Schema();
}
} else if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution)) {
} else if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(schemaResolution)) {
if (existingSchema == null) {
schemaObject = new Schema();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static Parameter applyAnnotations(
io.swagger.v3.oas.annotations.media.ArraySchema ctxArraySchema = AnnotationsUtils.getArraySchemaAnnotation(annotations.toArray(new Annotation[0]));
Annotation[] ctxAnnotation31 = null;

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

if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution)) {
if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(schemaResolution)) {
annotatedType.ctxAnnotations(ctxAnnotation31);
} else {
annotatedType.ctxAnnotations(reworkedAnnotations.toArray(new Annotation[reworkedAnnotations.size()]));
Expand All @@ -106,7 +106,7 @@ public static Parameter applyAnnotations(
if (resolvedSchema.schema != null) {
Schema resSchema = AnnotationsUtils.clone(resolvedSchema.schema, openapi31);
Schema ctxSchemaObject = null;
if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution)) {
if (Schema.SchemaResolution.ALL_OF.equals(schemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(schemaResolution)) {
Optional<Schema> reResolvedSchema = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, null, schemaResolution, null);
if (reResolvedSchema.isPresent()) {
ctxSchemaObject = reResolvedSchema.get();
Expand All @@ -121,6 +121,9 @@ public static Parameter applyAnnotations(
resSchema = new Schema()
.addAllOfItem(ctxSchemaObject)
.addAllOfItem(resolvedSchema.schema);
} else if (Schema.SchemaResolution.ALL_OF_REF.equals(schemaResolution) && ctxSchemaObject != null) {
resSchema = ctxSchemaObject
.addAllOfItem(resolvedSchema.schema);
}
parameter.setSchema(resSchema);
}
Expand Down
3 changes: 2 additions & 1 deletion modules/swagger-jaxrs2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-version}</version>
<configuration>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
<argLine>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 --add-opens java.base/java.lang=ALL-UNNAMED</argLine>
<forkCount>0</forkCount>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.swagger.v3.jaxrs2;

import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.jaxrs2.matchers.SerializationMatchers;
import io.swagger.v3.jaxrs2.schemaResolution.SchemaResolutionResource;
import io.swagger.v3.oas.integration.SwaggerConfiguration;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import org.testng.annotations.Test;

public class SchemaResolutionAllOfRefTest {

@Test
public void testSchemaResolutionAllOfRef() {
ModelConverters.reset();
Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).schemaResolution(Schema.SchemaResolution.ALL_OF_REF));
OpenAPI openAPI = reader.read(SchemaResolutionResource.class);
String yaml = "openapi: 3.0.1\n" +
"paths:\n" +
" /test/inlineSchemaFirst:\n" +
" get:\n" +
" operationId: inlineSchemaFirst\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" $ref: \"#/components/schemas/InlineSchemaFirst\"\n" +
" /test/inlineSchemaSecond:\n" +
" get:\n" +
" operationId: inlineSchemaSecond\n" +
" requestBody:\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" description: InlineSchemaSecond API\n" +
" allOf:\n" +
" - $ref: \"#/components/schemas/InlineSchemaSecond\"\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" $ref: \"#/components/schemas/InlineSchemaSecond\"\n" +
"components:\n" +
" schemas:\n" +
" InlineSchemaFirst:\n" +
" type: object\n" +
" properties:\n" +
" property1:\n" +
" description: InlineSchemaFirst property 1\n" +
" nullable: true\n" +
" allOf:\n" +
" - $ref: \"#/components/schemas/InlineSchemaPropertyFirst\"\n" +
" property2:\n" +
" description: ' InlineSchemaFirst property 2'\n" +
" example: example 2\n" +
" allOf:\n" +
" - $ref: \"#/components/schemas/InlineSchemaPropertyFirst\"\n" +
" InlineSchemaPropertyFirst:\n" +
" type: object\n" +
" properties:\n" +
" bar:\n" +
" type: string\n" +
" description: property\n" +
" example: example\n" +
" InlineSchemaPropertySecond:\n" +
" type: object\n" +
" properties:\n" +
" bar:\n" +
" $ref: \"#/components/schemas/InlineSchemaSimple\"\n" +
" description: propertysecond\n" +
" example: examplesecond\n" +
" InlineSchemaPropertySimple:\n" +
" type: object\n" +
" properties:\n" +
" bar:\n" +
" type: string\n" +
" description: property\n" +
" InlineSchemaSecond:\n" +
" type: object\n" +
" properties:\n" +
" foo:\n" +
" type: string\n" +
" propertySecond1:\n" +
" description: InlineSchemaSecond property 1\n" +
" nullable: true\n" +
" allOf:\n" +
" - $ref: \"#/components/schemas/InlineSchemaPropertySecond\"\n" +
" property2:\n" +
" description: InlineSchemaSecond property 2\n" +
" example: InlineSchemaSecond example 2\n" +
" allOf:\n" +
" - $ref: \"#/components/schemas/InlineSchemaPropertyFirst\"\n" +
" InlineSchemaSimple:\n" +
" type: object\n" +
" properties:\n" +
" property1:\n" +
" description: property 1\n" +
" allOf:\n" +
" - $ref: \"#/components/schemas/InlineSchemaPropertySimple\"\n" +
" property2:\n" +
" description: property 2\n" +
" example: example\n" +
" allOf:\n" +
" - $ref: \"#/components/schemas/InlineSchemaPropertySimple\"\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
ModelConverters.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public enum SchemaResolution {
@JsonProperty("inline")
INLINE("inline"),
@JsonProperty("all-of")
ALL_OF("all-of");
ALL_OF("all-of"),
@JsonProperty("all-of-ref")
ALL_OF_REF("all-of-ref");

private String value;

SchemaResolution(String value) {
Expand Down
Loading