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 1: global inline #4735

Merged
merged 1 commit into from
Sep 16, 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 @@ -42,6 +42,15 @@ public ModelConverters(boolean openapi31) {
}
}

public ModelConverters(boolean openapi31, Schema.SchemaResolution schemaResolution) {
converters = new CopyOnWriteArrayList<>();
if (openapi31) {
converters.add(new ModelResolver(Json31.mapper()).openapi31(true));
} else {
converters.add(new ModelResolver(Json.mapper()).schemaResolution(schemaResolution));
}
}

public Set<String> getSkippedPackages() {
return skippedPackages;
}
Expand All @@ -61,6 +70,30 @@ public static ModelConverters getInstance(boolean openapi31) {
return SINGLETON;
}

public static void reset() {
synchronized (ModelConverters.class) {
SINGLETON = null;
SINGLETON31 = null;
}
}

public static ModelConverters getInstance(boolean openapi31, Schema.SchemaResolution schemaResolution) {
synchronized (ModelConverters.class) {
if (openapi31) {
if (SINGLETON31 == null) {
SINGLETON31 = new ModelConverters(openapi31);
init(SINGLETON31);
}
return SINGLETON31;
}
if (SINGLETON == null) {
SINGLETON = new ModelConverters(openapi31, schemaResolution);
init(SINGLETON);
}
return SINGLETON;
}
}

private static void init(ModelConverters converter) {
converter.addPackageToSkip("java.lang");
converter.addPackageToSkip("groovy.lang");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public class ModelResolver extends AbstractModelConverter implements ModelConver

private boolean openapi31;

private Schema.SchemaResolution schemaResolution = Schema.SchemaResolution.DEFAULT;

public ModelResolver(ObjectMapper mapper) {
super(mapper);
}
Expand Down Expand Up @@ -725,7 +727,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
property = context.resolve(aType);
property = clone(property);
if (openapi31) {
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, context);
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, schemaResolution, context);
if (reResolvedProperty.isPresent()) {
property = reResolvedProperty.get();
}
Expand Down Expand Up @@ -773,10 +775,14 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
}

if (context.getDefinedModels().containsKey(pName)) {
property = new Schema().$ref(constructRef(pName));
if (Schema.SchemaResolution.INLINE.equals(this.schemaResolution)) {
property = context.getDefinedModels().get(pName);
} else {
property = new Schema().$ref(constructRef(pName));
}
property = clone(property);
if (openapi31) {
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, context);
if (openapi31 || Schema.SchemaResolution.INLINE.equals(this.schemaResolution)) {
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, this.schemaResolution, context);
if (reResolvedProperty.isPresent()) {
property = reResolvedProperty.get();
}
Expand Down Expand Up @@ -1000,7 +1006,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
StringUtils.isNotBlank(model.getName()))
{
if (context.getDefinedModels().containsKey(model.getName())) {
model = new Schema().$ref(constructRef(model.getName()));
if (!Schema.SchemaResolution.INLINE.equals(this.schemaResolution)) {
model = new Schema().$ref(constructRef(model.getName()));
}
}
} else if (model != null && model.get$ref() != null) {
model = new Schema().$ref(StringUtils.isNotEmpty(model.get$ref()) ? model.get$ref() : model.getName());
Expand Down Expand Up @@ -1255,6 +1263,18 @@ private void handleUnwrapped(List<Schema> props, Schema innerModel, String prefi
}
}

public Schema.SchemaResolution getSchemaResolution() {
return schemaResolution;
}

public void setSchemaResolution(Schema.SchemaResolution schemaResolution) {
this.schemaResolution = schemaResolution;
}

public ModelResolver schemaResolution(Schema.SchemaResolution schemaResolution) {
setSchemaResolution(schemaResolution);
return this;
}

private class GeneratorWrapper {

Expand Down Expand Up @@ -1870,7 +1890,7 @@ protected Map<String, Schema> resolveDependentSchemas(JavaType a, Annotation[] a
}
Annotation[] propAnnotations = new Annotation[]{dependentSchemaAnnotation.schema(), dependentSchemaAnnotation.array()};
Schema existingSchema = null;
Optional<Schema> resolvedPropSchema = AnnotationsUtils.getSchemaFromAnnotation(dependentSchemaAnnotation.schema(), components, jsonViewAnnotation, openapi31, null, context);
Optional<Schema> resolvedPropSchema = AnnotationsUtils.getSchemaFromAnnotation(dependentSchemaAnnotation.schema(), components, jsonViewAnnotation, openapi31, null, Schema.SchemaResolution.DEFAULT, context);
if (resolvedPropSchema.isPresent()) {
existingSchema = resolvedPropSchema.get();
dependentSchemas.put(name, existingSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,24 +587,40 @@ public static Optional<Schema> getSchemaFromAnnotation(
boolean openapi31,
Schema existingSchema,
ModelConverterContext context) {
return getSchemaFromAnnotation(schema, components, jsonViewAnnotation, openapi31, existingSchema, Schema.SchemaResolution.DEFAULT, null);
}
public static Optional<Schema> getSchemaFromAnnotation(
io.swagger.v3.oas.annotations.media.Schema schema,
Components components,
JsonView jsonViewAnnotation,
boolean openapi31,
Schema existingSchema,
Schema.SchemaResolution schemaResolution,
ModelConverterContext context) {
if (schema == null || !hasSchemaAnnotation(schema)) {
if (existingSchema == null || !openapi31) {
if (existingSchema == null || (!openapi31 && Schema.SchemaResolution.DEFAULT.equals(schemaResolution))) {
return Optional.empty();
} else if (existingSchema != null && openapi31) {
} else if (existingSchema != null && (openapi31 || Schema.SchemaResolution.INLINE.equals(schemaResolution))) {
return Optional.of(existingSchema);
}
}
Schema schemaObject = null;
if (!openapi31) {
if (existingSchema != null) {
return Optional.of(existingSchema);
if (!Schema.SchemaResolution.DEFAULT.equals(schemaResolution)) {
schemaObject = existingSchema;
} else {
return Optional.of(existingSchema);
}
}
if (schema.oneOf().length > 0 ||
schema.allOf().length > 0 ||
schema.anyOf().length > 0) {
schemaObject = new ComposedSchema();
} else {
schemaObject = new Schema();
if (Schema.SchemaResolution.DEFAULT.equals(schemaResolution)) {
if (schema != null && (schema.oneOf().length > 0 ||
schema.allOf().length > 0 ||
schema.anyOf().length > 0)) {
schemaObject = new ComposedSchema();
} else {
schemaObject = new Schema();
}
}
} else {
if (existingSchema == null) {
Expand All @@ -613,6 +629,9 @@ public static Optional<Schema> getSchemaFromAnnotation(
schemaObject = existingSchema;
}
}
if (schema == null) {
return Optional.of(schemaObject);
}
if (StringUtils.isNotBlank(schema.description())) {
schemaObject.setDescription(schema.description());
}
Expand Down Expand Up @@ -1956,11 +1975,15 @@ public static void updateAnnotation(Class<?> clazz, io.swagger.v3.oas.annotation

}

public static Annotation mergeSchemaAnnotations(
Annotation[] ctxAnnotations, JavaType type) {
return mergeSchemaAnnotations(ctxAnnotations, type, false);
}
/*
* returns null if no annotations, otherwise either ArraySchema or Schema
*/
public static Annotation mergeSchemaAnnotations(
Annotation[] ctxAnnotations, JavaType type) {
Annotation[] ctxAnnotations, JavaType type, boolean contextWins) {
// get type array and schema
io.swagger.v3.oas.annotations.media.Schema tS = type.getRawClass().getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
if (!hasSchemaAnnotation(tS)) {
Expand Down Expand Up @@ -2020,6 +2043,9 @@ else if (tS != null && tA == null && cS == null && cA != null) {
}

else if (tA != null && cA != null) {
if (contextWins) {
return mergeArraySchemaAnnotations(tA, cA);
}
return mergeArraySchemaAnnotations(cA, tA);
}

Expand All @@ -2028,6 +2054,9 @@ else if (tS != null && cS == null && cA == null) {
}

else if (tS != null && cS != null) {
if (contextWins) {
return mergeSchemaAnnotations(cS, tS);
}
return mergeSchemaAnnotations(tS, cS);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package io.swagger.v3.core.resolving;

import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverterContextImpl;
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.oas.annotations.media.Schema;
import org.testng.annotations.Test;

public class InlineResolvingTest extends SwaggerTestBase{

@Test
public void testInlineResolving() {

final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(false).schemaResolution(io.swagger.v3.oas.models.media.Schema.SchemaResolution.INLINE);
final ModelConverterContextImpl c = new ModelConverterContextImpl(modelResolver);
// ModelConverters c = ModelConverters.getInstance(false, io.swagger.v3.oas.models.media.Schema.SchemaResolution.INLINE);
c.resolve(new AnnotatedType(InlineSchemaFirst.class));

String expectedYaml = "InlineSchemaFirst:\n" +
" type: object\n" +
" properties:\n" +
" property1:\n" +
" type: object\n" +
" description: InlineSchemaFirst property 1\n" +
" nullable: true\n" +
" example: example\n" +
" property2:\n" +
" type: object\n" +
" description: ' InlineSchemaFirst property 2'\n" +
" example: example 2\n" +
"InlineSchemaPropertyFirst:\n" +
" type: object\n" +
" description: property\n" +
" example: example\n";

SerializationMatchers.assertEqualsToYaml(c.getDefinedModels(), expectedYaml);
// stringSchemaMap = c.readAll(InlineSchemaSecond.class);
c.resolve(new AnnotatedType(InlineSchemaSecond.class));
expectedYaml = "InlineSchemaFirst:\n" +
" type: object\n" +
" properties:\n" +
" property1:\n" +
" type: object\n" +
" description: InlineSchemaFirst property 1\n" +
" nullable: true\n" +
" example: example\n" +
" property2:\n" +
" type: object\n" +
" description: ' InlineSchemaFirst property 2'\n" +
" example: example 2\n" +
"InlineSchemaPropertyFirst:\n" +
" type: object\n" +
" description: property\n" +
" example: example\n" +
"InlineSchemaPropertySecond:\n" +
" type: object\n" +
" properties:\n" +
" bar:\n" +
" type: object\n" +
" properties:\n" +
" property1:\n" +
" type: object\n" +
" description: property 1\n" +
" property2:\n" +
" type: object\n" +
" description: property 2\n" +
" example: example\n" +
" description: propertysecond\n" +
" nullable: true\n" +
" example: examplesecond\n" +
"InlineSchemaPropertySimple:\n" +
" type: object\n" +
" description: property\n" +
" example: example\n" +
"InlineSchemaSecond:\n" +
" type: object\n" +
" properties:\n" +
" propertySecond1:\n" +
" type: object\n" +
" properties:\n" +
" bar:\n" +
" type: object\n" +
" properties:\n" +
" property1:\n" +
" type: object\n" +
" description: property 1\n" +
" property2:\n" +
" type: object\n" +
" description: property 2\n" +
" example: example\n" +
" description: InlineSchemaSecond property 1\n" +
" nullable: true\n" +
" example: examplesecond\n" +
" property2:\n" +
" type: object\n" +
" description: InlineSchemaSecond property 2\n" +
" example: InlineSchemaSecond example 2\n" +
"InlineSchemaSimple:\n" +
" type: object\n" +
" properties:\n" +
" property1:\n" +
" type: object\n" +
" description: property 1\n" +
" property2:\n" +
" type: object\n" +
" description: property 2\n" +
" example: example\n";
SerializationMatchers.assertEqualsToYaml(c.getDefinedModels(), expectedYaml);
}

static class InlineSchemaFirst {

// public String foo;

@Schema(description = "InlineSchemaFirst property 1", nullable = true)
public InlineSchemaPropertyFirst property1;


private InlineSchemaPropertyFirst property2;

@Schema(description = " InlineSchemaFirst property 2", example = "example 2")
public InlineSchemaPropertyFirst getProperty2() {
return null;
}
}

static class InlineSchemaSecond {

// public String foo;

@Schema(description = "InlineSchemaSecond property 1", nullable = true)
public InlineSchemaPropertySecond propertySecond1;


private InlineSchemaPropertyFirst property2;

@Schema(description = "InlineSchemaSecond property 2", example = "InlineSchemaSecond example 2")
public InlineSchemaPropertyFirst getProperty2() {
return null;
}
}

@Schema(description = "property", example = "example")
static class InlineSchemaPropertyFirst {
// public String bar;
}

@Schema(description = "propertysecond", example = "examplesecond")
static class InlineSchemaPropertySecond {
public InlineSchemaSimple bar;
}

static class InlineSchemaSimple {

@Schema(description = "property 1")
public InlineSchemaPropertySimple property1;


private InlineSchemaPropertySimple property2;

@Schema(description = "property 2", example = "example")
public InlineSchemaPropertySimple getProperty2() {
return null;
}
}

@Schema(description = "property")
static class InlineSchemaPropertySimple {
// public String bar;
}
}
Loading
Loading