From 66df31c9881e5afb383d4ac36cdd9e9aef125d83 Mon Sep 17 00:00:00 2001 From: Richard Lindsay Date: Fri, 22 Nov 2024 15:49:15 +0000 Subject: [PATCH] Fix for record component annotations on fields with JsonProperty annotations --- .../v3/core/jackson/ModelResolver.java | 2 +- .../v3/java17/resolving/JavaRecordTest.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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 2e5ea01248..5164dabaa4 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 @@ -1112,7 +1112,7 @@ private Stream extractGenericTypeArgumentAnnotations(BeanPropertyDef private Stream getRecordComponentAnnotations(BeanPropertyDefinition propDef) { try { - Method accessor = propDef.getPrimaryMember().getDeclaringClass().getDeclaredMethod(propDef.getName()); + Method accessor = propDef.getPrimaryMember().getDeclaringClass().getDeclaredMethod(propDef.getPrimaryMember().getName()); return getGenericTypeArgumentAnnotations(accessor.getAnnotatedReturnType()); } catch (NoSuchMethodException e) { LOGGER.error("Accessor for record component not found"); diff --git a/modules/swagger-java17-support/src/test/java/io/swagger/v3/java17/resolving/JavaRecordTest.java b/modules/swagger-java17-support/src/test/java/io/swagger/v3/java17/resolving/JavaRecordTest.java index 065b6c2ea5..afbfd11fa6 100644 --- a/modules/swagger-java17-support/src/test/java/io/swagger/v3/java17/resolving/JavaRecordTest.java +++ b/modules/swagger-java17-support/src/test/java/io/swagger/v3/java17/resolving/JavaRecordTest.java @@ -1,5 +1,6 @@ package io.swagger.v3.java17.resolving; +import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.v3.core.converter.ModelConverters; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.java17.matchers.SerializationMatchers; @@ -117,4 +118,25 @@ public record JavaRecordWithAnnotationsOnGenericType( List<@Min(1)@Max(10000) Integer> id ){ } + + @Test + public void testJavaRecordWithJsonPropertyAnnotationNotMatchingFieldName() { + String expectedYaml = "JavaRecordWithJsonPropertyAnnotationNotMatchingFieldName:\n" + + " type: object\n" + + " properties:\n" + + " listOfStrings:\n" + + " type: array\n" + + " items:\n" + + " maxLength: 5\n" + + " minLength: 1\n" + + " type: string"; + + Map stringSchemaMap = ModelConverters.getInstance(false).readAll(JavaRecordWithJsonPropertyAnnotationNotMatchingFieldName.class); + SerializationMatchers.assertEqualsToYaml(stringSchemaMap, expectedYaml); + } + + public record JavaRecordWithJsonPropertyAnnotationNotMatchingFieldName( + @JsonProperty("listOfStrings") List<@Size(min = 1, max = 5)String> stringList + ) { } + }