Skip to content

Commit 2274d85

Browse files
feat: add support for default enum values in Jackson annotations (#2324)
1 parent 634c004 commit 2274d85

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/generators/java/presets/JacksonPreset.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ export const JAVA_JACKSON_PRESET: JavaPreset = {
9191
}
9292
},
9393
enum: {
94+
// Fallback for unknown enum values has to be configured on the ObjectMapper
95+
// objectMapper.configure(
96+
// DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true
97+
// );
98+
item({ content, model, item }) {
99+
const defaultEnumValue = model.originalInput?.default;
100+
if (item.originalInput === defaultEnumValue) {
101+
return `@JsonEnumDefaultValue ${content}`;
102+
}
103+
return content;
104+
},
94105
self({ renderer, content }) {
95106
renderer.dependencyManager.addDependency(JACKSON_ANNOTATION_DEPENDENCY);
96107
return content;

test/generators/java/presets/JacksonPreset.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ describe('JAVA_JACKSON_PRESET', () => {
4444
expect(models[0].dependencies).toEqual(expectedDependencies);
4545
});
4646

47+
test('should render Jackson annotations with default for enum', async () => {
48+
const doc = {
49+
$id: 'Status',
50+
type: 'string',
51+
description: 'Description for enum',
52+
enum: ['on', 'off'],
53+
default: 'off'
54+
};
55+
56+
const expectedDependencies = ['import com.fasterxml.jackson.annotation.*;'];
57+
58+
const models = await generator.generate(doc);
59+
expect(models).toHaveLength(1);
60+
expect(models[0].result).toMatchSnapshot();
61+
expect(models[0].dependencies).toEqual(expectedDependencies);
62+
});
63+
4764
test('should not render anything when isExtended is true', async () => {
4865
const extend = {
4966
$id: 'extend',

test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,38 @@ exports[`JAVA_JACKSON_PRESET should render Jackson annotations for enum 1`] = `
8181
}"
8282
`;
8383

84+
exports[`JAVA_JACKSON_PRESET should render Jackson annotations with default for enum 1`] = `
85+
"public enum Status {
86+
ON((String)\\"on\\"), @JsonEnumDefaultValue OFF((String)\\"off\\");
87+
88+
private final String value;
89+
90+
Status(String value) {
91+
this.value = value;
92+
}
93+
94+
@JsonValue
95+
public String getValue() {
96+
return value;
97+
}
98+
99+
@JsonCreator
100+
public static Status fromValue(String value) {
101+
for (Status e : Status.values()) {
102+
if (e.value.equals(value)) {
103+
return e;
104+
}
105+
}
106+
throw new IllegalArgumentException(\\"Unexpected value '\\" + value + \\"'\\");
107+
}
108+
109+
@Override
110+
public String toString() {
111+
return String.valueOf(value);
112+
}
113+
}"
114+
`;
115+
84116
exports[`JAVA_JACKSON_PRESET union handle oneOf with AsyncAPI discriminator with Jackson 1`] = `
85117
Array [
86118
"@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.EXISTING_PROPERTY, property=\\"vehicle_type\\", visible=true)

0 commit comments

Comments
 (0)