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

@JsonValue annotation on interface is not used #1978

Closed
ChristianKernDev opened this issue Sep 9, 2024 · 0 comments · Fixed by #1992
Closed

@JsonValue annotation on interface is not used #1978

ChristianKernDev opened this issue Sep 9, 2024 · 0 comments · Fixed by #1992

Comments

@ChristianKernDev
Copy link

For the following enum example, which implements an interface, which declares a @JsonValue method, the method is not used when creating the OpenAPI schema:

Interface:

public interface ValueAndLabel<T extends Serializable> {
	@JsonValue
	T getValue();

	String getLabel();
}

Enum:

public enum ExampleEnum implements ValueAndLabel<Integer> {
	_1_ONE(1, "One"),
	_2_TWO(2, "Two"),
	_3_THREE(3, "Three"),
	;

	private final Integer value;
	private final String label;

	ExampleEnum(final Integer value, final String label) {
		this.value = value;
		this.label = label;
	}

	public Integer getValue() {
		return value;
	}
	
	public String getLabel() {
		return label;
	}
}

Produces:

"ExampleEnum" : {
        "enum" : [ "_1_ONE", "_2_TWO", "_3_THREE" ],
        "type" : "string"
      },

When adding the @JsonValue annotation to the implementation of the enum it works as expected:
Enum:

public enum ExampleEnum implements ValueAndLabel<Integer> {
	_1_ONE(1, "One"),
	_2_TWO(2, "Two"),
	_3_THREE(3, "Three"),
	;

	private final Integer value;
	private final String label;

	ExampleEnum(final Integer value, final String label) {
		this.value = value;
		this.label = label;
	}

	@JsonValue
	public Integer getValue() {
		return value;
	}

	public String getLabel() {
		return label;
	}
}

Produces:

"ExampleEnum" : {
        "enum" : [ 1, 2, 3 ],
        "type" : "string"
      },

The problem is, that it is nescessary to add the annotation on every single implementation of the enum, for the schema to be correct. Jackson serializer is using the annotation of the interface, so the application is working perfectly fine without the annotation, but the schema is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant