From df7eb3782783bcf4bf20232865187469d40a30f1 Mon Sep 17 00:00:00 2001 From: Jeffrey Douangpaseuth <11084623+Nephery@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:01:08 -0500 Subject: [PATCH] fix Jackson JsonUnwrapped from inherited properties --- .../jackson/JsonUnwrappedDefinitionProvider.java | 13 +++++++------ .../jsonschema/module/jackson/IntegrationTest.java | 11 +++++++++++ .../module/jackson/integration-test-result.json | 8 ++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonUnwrappedDefinitionProvider.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonUnwrappedDefinitionProvider.java index f8ea7c51..79a6e86c 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonUnwrappedDefinitionProvider.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonUnwrappedDefinitionProvider.java @@ -18,7 +18,6 @@ import com.fasterxml.classmate.ResolvedType; import com.fasterxml.classmate.ResolvedTypeWithMembers; -import com.fasterxml.classmate.members.RawMember; import com.fasterxml.classmate.members.ResolvedMember; import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.JsonNode; @@ -30,6 +29,7 @@ import com.github.victools.jsonschema.generator.SchemaKeyword; import java.lang.annotation.Annotation; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Stream; @@ -43,17 +43,18 @@ public class JsonUnwrappedDefinitionProvider implements CustomDefinitionProvider @Override public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, SchemaGenerationContext context) { - if (javaType.getMemberFields().stream().noneMatch(this::hasJsonUnwrappedAnnotation) - && javaType.getMemberMethods().stream().noneMatch(this::hasJsonUnwrappedAnnotation)) { + ResolvedTypeWithMembers typeWithMembers = context.getTypeContext().resolveWithMembers(javaType); + + if (Arrays.stream(typeWithMembers.getMemberFields()).noneMatch(this::hasJsonUnwrappedAnnotation) + && Arrays.stream(typeWithMembers.getMemberMethods()).noneMatch(this::hasJsonUnwrappedAnnotation)) { // no need for custom handling here, if no relevant annotation is present return null; } // include the target type itself (assuming the annotated members are being ignored then) ObjectNode definition = context.createStandardDefinition(javaType, this); ArrayNode allOf = definition.withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF)); - // include each annotated member's type considering the optional prefix and/or suffix - ResolvedTypeWithMembers typeWithMembers = context.getTypeContext().resolveWithMembers(javaType); + // include each annotated member's type considering the optional prefix and/or suffix Stream.concat(Stream.of(typeWithMembers.getMemberFields()), Stream.of(typeWithMembers.getMemberMethods())) .filter(member -> Optional.ofNullable(member.getAnnotations().get(JsonUnwrapped.class)) .filter(JsonUnwrapped::enabled).isPresent()) @@ -69,7 +70,7 @@ public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, Sch * @param member field/method to check * @return whether the given member has an {@code enabled} {@link JsonUnwrapped @JsonUnwrapped} annotation */ - private boolean hasJsonUnwrappedAnnotation(RawMember member) { + private boolean hasJsonUnwrappedAnnotation(ResolvedMember member) { for (Annotation annotation : member.getAnnotations()) { if (annotation instanceof JsonUnwrapped && ((JsonUnwrapped) annotation).enabled()) { return true; diff --git a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IntegrationTest.java b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IntegrationTest.java index c446ad7d..061a5312 100644 --- a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IntegrationTest.java +++ b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IntegrationTest.java @@ -100,6 +100,8 @@ static class TestClass { @JsonUnwrapped public TypeToBeUnwrapped typeToBeUnwrapped; + public TypeWithInheritedFieldToBeUnwrapped typeWithInheritedFieldToBeUnwrapped; + public String ignoredUnannotatedMethod() { return "nothing"; } @@ -158,4 +160,13 @@ static class SubType2 implements BaseType { static class TypeToBeUnwrapped { public String unwrappedProperty; } + + static class TypeWithInheritedFieldToBeUnwrapped extends TypeWithFieldToBeUnwrapped { + + } + + static class TypeWithFieldToBeUnwrapped { + @JsonUnwrapped + public TypeToBeUnwrapped typeToBeUnwrapped; + } } diff --git a/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/integration-test-result.json b/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/integration-test-result.json index 0ae36e1c..8e48e311 100644 --- a/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/integration-test-result.json +++ b/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/integration-test-result.json @@ -76,6 +76,14 @@ } } }, + "typeWithInheritedFieldToBeUnwrapped": { + "type": "object", + "properties": { + "unwrappedProperty": { + "type": "string" + } + } + }, "unwrappedProperty": { "type": "string" }