Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonEnumDefaultValue support #447

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public void applyToConfigBuilder(SchemaGeneratorConfigBuilder builder) {

applySubtypeResolverToConfigBuilder(generalConfigPart, fieldConfigPart, methodConfigPart);

fieldConfigPart.withDefaultResolver(JsonEnumDefaultValueResolver::apply);

generalConfigPart.withCustomDefinitionProvider(new JsonUnwrappedDefinitionProvider());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.victools.jsonschema.module.jackson;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.github.victools.jsonschema.generator.TypeScope;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per below suggestion, declaring the more specific FieldScope instead of TypeScope as method parameter type.

Suggested change
import com.github.victools.jsonschema.generator.TypeScope;
import com.github.victools.jsonschema.generator.FieldScope;

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())
Comment on lines +10 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Since MethodScope.getType() can return null for void methods and you're only applying this to fields, I'd suggest making the declared parameter type FieldScope instead.
Also getErasedType() may return null, I believe, if the given type is merely a generic type placeholder.

Suggested change
public static String apply(TypeScope typeScope) {
if (typeScope.getType().getErasedType().isEnum()) {
return Arrays.stream(typeScope.getType().getErasedType().getDeclaredFields())
public static String apply(FieldScope scope) {
Class<?> erasedType = scope.getType().getErasedType();
if (erasedType != null && erasedType.isEnum()) {
return Arrays.stream(erasedType.getDeclaredFields())

.filter(enumValue -> enumValue.isAnnotationPresent(JsonEnumDefaultValue.class))
.findFirst()
.map(Field::getName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (blocking): This is too simplistic.
There are several ways to represent an enum value in the JSON (Schema). The declared name in the code may therefore not be the appropriate one to declare as default here.
We should at least attempt to cover the handful of standard options already supported by the main schema generator and this JacksonModule (e.g., considering @JsonValue annotations).

.orElse(null);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +96,8 @@ static class TestClass {

public TestEnumWithJsonPropertyAnnotations enumValueWithJsonPropertyAnnotations;

public TestEnumWithJsonEnumDefaultAnnotations enumValueWithJsonEnumDefaultValueAnnotation;

public BaseType interfaceWithDeclaredSubtypes;

@JsonUnwrapped
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"type": "string",
"enum": ["entry1", "entry2", "entry3"]
},
"enumValueWithJsonEnumDefaultValueAnnotation": {
"type": "string",
"enum": ["A", "B", "C"],
"default": "B"
Comment on lines +57 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: The alignment seems a bit off. 😉

},
"fieldWithDescription": {
"type": "string",
"description": "field description"
Expand Down
Loading