From c37b72cc653c41dc1ab90a4e10d2c957c84d52d4 Mon Sep 17 00:00:00 2001 From: MicRyc Date: Thu, 22 Aug 2024 15:53:25 +0200 Subject: [PATCH] refs #4679 - fix @Schema#implementation resolving for OAS 3.1 --- .../v3/core/jackson/ModelResolver.java | 3 + .../v3/core/resolving/Ticket4679Test.java | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket4679Test.java 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 73437bb660..66634f693f 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 @@ -683,6 +683,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context !(a instanceof io.swagger.v3.oas.annotations.media.ArraySchema)) { ctxAnnotations31List.add(a); } + if ((ctxSchema != null) && (!ctxSchema.implementation().equals(Void.class) || StringUtils.isNotEmpty(ctxSchema.type()))) { + ctxAnnotations31List.add(a); + } } ctxAnnotation31 = ctxAnnotations31List.toArray(new Annotation[ctxAnnotations31List.size()]); } diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket4679Test.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket4679Test.java new file mode 100644 index 0000000000..9998f8b6a2 --- /dev/null +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket4679Test.java @@ -0,0 +1,55 @@ +package io.swagger.v3.core.resolving; + +import io.swagger.v3.core.converter.ModelConverters; +import io.swagger.v3.core.matchers.SerializationMatchers; +import io.swagger.v3.core.util.Yaml; +import io.swagger.v3.core.util.Yaml31; +import io.swagger.v3.oas.annotations.media.Schema; +import org.testng.annotations.Test; + +import java.util.Map; + +public class Ticket4679Test extends SwaggerTestBase{ + + @Test(description = "Custom schema implementation in property overrides type value") + public void testCustomSchemaImplementation() { + + String expectedYaml = "ModelWithCustomSchemaImplementationInProperty:\n" + + " properties:\n" + + " exampleField:\n" + + " type: integer\n" + + " format: int32\n" + + " secondExampleField:\n" + + " type: string\n"; + + Map stringSchemaMap = ModelConverters.getInstance(true).readAll(ModelWithCustomSchemaImplementationInProperty.class); + SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml); + } + + static class ModelWithCustomSchemaImplementationInProperty { + + @Schema(implementation = Integer.class) + private String exampleField; + + @Schema(type = "string") + private Integer secondExampleField; + + public String getExampleField() { + return exampleField; + } + + public void setExampleField(String exampleField) { + this.exampleField = exampleField; + } + + public Integer getSecondExampleField() { + return secondExampleField; + } + + public void setSecondExampleField(Integer secondExampleField) { + this.secondExampleField = secondExampleField; + } + + + } +}