diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java index c6d5f68ae6..b910d9b544 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java @@ -552,6 +552,26 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context } } + String propSchemaName = null; + io.swagger.v3.oas.annotations.media.Schema ctxSchema = AnnotationsUtils.getSchemaAnnotation(annotations); + if (AnnotationsUtils.hasSchemaAnnotation(ctxSchema)) { + if (!StringUtils.isBlank(ctxSchema.name())) { + propSchemaName = ctxSchema.name(); + } + } + if (propSchemaName == null) { + io.swagger.v3.oas.annotations.media.ArraySchema ctxArraySchema = AnnotationsUtils.getArraySchemaAnnotation(annotations); + if (AnnotationsUtils.hasArrayAnnotation(ctxArraySchema)) { + if (AnnotationsUtils.hasSchemaAnnotation(ctxArraySchema.schema())) { + if (!StringUtils.isBlank(ctxArraySchema.schema().name())) { + propSchemaName = ctxArraySchema.schema().name(); + } + } + } + } + if (StringUtils.isNotBlank(propSchemaName)) { + propName = propSchemaName; + } Annotation propSchemaOrArray = AnnotationsUtils.mergeSchemaAnnotations(annotations, propType); final io.swagger.v3.oas.annotations.media.Schema propResolvedSchemaAnnotation = propSchemaOrArray == null ? @@ -559,9 +579,6 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context propSchemaOrArray instanceof io.swagger.v3.oas.annotations.media.ArraySchema ? ((io.swagger.v3.oas.annotations.media.ArraySchema) propSchemaOrArray).schema() : (io.swagger.v3.oas.annotations.media.Schema) propSchemaOrArray; - if (propResolvedSchemaAnnotation != null && !propResolvedSchemaAnnotation.name().isEmpty()) { - propName = propResolvedSchemaAnnotation.name(); - } io.swagger.v3.oas.annotations.media.Schema.AccessMode accessMode = resolveAccessMode(propDef, type, propResolvedSchemaAnnotation); diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket2915Test.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket2915Test.java new file mode 100644 index 0000000000..961d1b77ec --- /dev/null +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket2915Test.java @@ -0,0 +1,47 @@ +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.core.resolving.resources.TestObject2915; +import io.swagger.v3.core.resolving.resources.Ticket2862Model; +import io.swagger.v3.oas.models.media.Schema; +import org.testng.annotations.Test; + +public class Ticket2915Test extends SwaggerTestBase { + @Test + public void testPropertyName() throws Exception { + final ModelResolver modelResolver = new ModelResolver(mapper()); + + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); + + final Schema model = context + .resolve(new AnnotatedType(TestObject2915.class)); + + SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "QuantitativeValue:\n" + + " required:\n" + + " - value\n" + + " type: object\n" + + " properties:\n" + + " value:\n" + + " type: number\n" + + " format: double\n" + + " unitText:\n" + + " type: string\n" + + " unitCode:\n" + + " type: string\n" + + " description: A combination of a value and associated unit\n" + + "TestObject2616:\n" + + " type: object\n" + + " properties:\n" + + " name:\n" + + " type: string\n" + + " perServing:\n" + + " $ref: '#/components/schemas/QuantitativeValue'\n" + + " per100Gram:\n" + + " $ref: '#/components/schemas/QuantitativeValue'\n" + + " description: Nutritional value specification"); + } + +} diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/resources/TestObject2915.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/resources/TestObject2915.java new file mode 100644 index 0000000000..202bffb092 --- /dev/null +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/resources/TestObject2915.java @@ -0,0 +1,75 @@ +package io.swagger.v3.core.resolving.resources; + +import io.swagger.v3.oas.annotations.media.Schema; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +@Schema(name = "TestObject2616", description = "Nutritional value specification") +public class TestObject2915 implements Serializable { + private final static long serialVersionUID = 1L; + private String name; + + private QuantitativeValueDTO perServing; + private QuantitativeValueDTO per100Gram; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public QuantitativeValueDTO getPerServing() { + return perServing; + } + + public void setPerServing(QuantitativeValueDTO perServing) { + this.perServing = perServing; + } + + public QuantitativeValueDTO getPer100Gram() { + return per100Gram; + } + + public void setPer100Gram(QuantitativeValueDTO per100Gram) { + this.per100Gram = per100Gram; + } + + @Schema(name = "QuantitativeValue", description = "A combination of a value and associated unit") + public class QuantitativeValueDTO implements Serializable { + + private final static long serialVersionUID = 1L; + + @NotNull + private double value; + + private String unitText; + private String unitCode; + + public double getValue() { + return value; + } + + public void setValue(double value) { + this.value = value; + } + + public String getUnitText() { + return unitText; + } + + public void setUnitText(String unitText) { + this.unitText = unitText; + } + + public String getUnitCode() { + return unitCode; + } + + public void setUnitCode(String unitCode) { + this.unitCode = unitCode; + } + } +}