diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java index 1d887605..0fa6df67 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java @@ -114,6 +114,8 @@ public void applyToConfigBuilder(SchemaGeneratorConfigBuilder builder) { applySubtypeResolverToConfigBuilder(generalConfigPart, fieldConfigPart, methodConfigPart); + fieldConfigPart.withDefaultResolver(JsonEnumDefaultValueResolver::apply); + generalConfigPart.withCustomDefinitionProvider(new JsonUnwrappedDefinitionProvider()); } diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonEnumDefaultValueResolver.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonEnumDefaultValueResolver.java new file mode 100644 index 00000000..96c32e8c --- /dev/null +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonEnumDefaultValueResolver.java @@ -0,0 +1,20 @@ +package com.github.victools.jsonschema.module.jackson; + +import com.fasterxml.jackson.annotation.JsonEnumDefaultValue; +import com.github.victools.jsonschema.generator.TypeScope; +import java.lang.reflect.Field; +import java.util.Arrays; + +public class JsonEnumDefaultValueResolver { + + public static String apply(TypeScope typeScope) { + if (typeScope.getType().getErasedType().isEnum()) { + return Arrays.stream(typeScope.getType().getErasedType().getDeclaredFields()) + .filter(enumValue -> enumValue.isAnnotationPresent(JsonEnumDefaultValue.class)) + .findFirst() + .map(Field::getName) + .orElse(null); + } + return null; + } +} 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 061a5312..e560d923 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 @@ -17,6 +17,7 @@ package com.github.victools.jsonschema.module.jackson; import com.fasterxml.jackson.annotation.JsonClassDescription; +import com.fasterxml.jackson.annotation.JsonEnumDefaultValue; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIdentityReference; import com.fasterxml.jackson.annotation.JsonProperty; @@ -95,6 +96,8 @@ static class TestClass { public TestEnumWithJsonPropertyAnnotations enumValueWithJsonPropertyAnnotations; + public TestEnumWithJsonEnumDefaultAnnotations enumValueWithJsonEnumDefaultValueAnnotation; + public BaseType interfaceWithDeclaredSubtypes; @JsonUnwrapped @@ -131,6 +134,10 @@ enum TestEnumWithJsonPropertyAnnotations { @JsonProperty Y } + enum TestEnumWithJsonEnumDefaultAnnotations { + A, @JsonEnumDefaultValue B, C + } + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") static class TestTypeWithObjectId { IdType id; 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 8e48e311..ce5c1ed1 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 @@ -54,6 +54,11 @@ "type": "string", "enum": ["entry1", "entry2", "entry3"] }, + "enumValueWithJsonEnumDefaultValueAnnotation": { + "type": "string", + "enum": ["A", "B", "C"], + "default": "B" + }, "fieldWithDescription": { "type": "string", "description": "field description"