Skip to content

Commit fc30cdc

Browse files
fix: do not use optionals for non-required arrays in Java (#2291)
1 parent 8869c03 commit fc30cdc

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/generators/java/renderers/ClassRenderer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ ${this.indent(this.renderBlock(content, 2))}
149149

150150
private doesContainOptionalProperties(): boolean {
151151
const properties = Object.values(this.model.properties);
152-
return properties.some((prop) => !prop.required);
152+
return properties.some(
153+
(prop) =>
154+
!prop.required && !(prop.property instanceof ConstrainedArrayModel)
155+
);
153156
}
154157

155158
private addCollectionDependencies() {
@@ -310,7 +313,9 @@ export const JAVA_DEFAULT_CLASS_PRESET: ClassPresetType<JavaOptions> = {
310313
)}`;
311314

312315
const useOptional =
313-
options.useOptionalForNullableProperties && !property.required;
316+
options.useOptionalForNullableProperties &&
317+
!property.required &&
318+
!(property.property instanceof ConstrainedArrayModel);
314319

315320
const returnType = useOptional
316321
? `Optional<${property.property.type}>`

test/generators/java/JavaGenerator.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,28 @@ describe('JavaGenerator', () => {
252252
expect(models[0].dependencies).not.toContain(expectedDependencies);
253253
});
254254

255+
test('should not render optional imports when only non-required field of type array', async () => {
256+
const doc = {
257+
$id: 'OtherClass',
258+
type: 'object',
259+
additionalProperties: false,
260+
properties: {
261+
tools: { type: 'array', items: { type: 'string' } }
262+
}
263+
};
264+
const expectedDependencies = [
265+
'import java.util.Optional;',
266+
'import static java.util.Optional.ofNullable;'
267+
];
268+
269+
generator = new JavaGenerator({ useOptionalForNullableProperties: true });
270+
271+
const models = await generator.generate(doc);
272+
expect(models).toHaveLength(1);
273+
expect(models[0].result).toMatchSnapshot();
274+
expect(models[0].dependencies).not.toContain(expectedDependencies);
275+
});
276+
255277
test('should render models and their dependencies', async () => {
256278
const doc = {
257279
$id: 'Address',

test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,15 @@ public interface Vehicle {
16531653
]
16541654
`;
16551655
1656+
exports[`JavaGenerator should not render optional imports when only non-required field of type array 1`] = `
1657+
"public class OtherClass {
1658+
private String[] tools;
1659+
1660+
public String[] getTools() { return this.tools; }
1661+
public void setTools(String[] tools) { this.tools = tools; }
1662+
}"
1663+
`;
1664+
16561665
exports[`JavaGenerator should not render optional imports when only required field 1`] = `
16571666
"public class OtherClass {
16581667
private String color;
@@ -1956,7 +1965,7 @@ exports[`JavaGenerator should render optional for non-required field 1`] = `
19561965
public Optional<String> getColor() { return ofNullable(this.color); }
19571966
public void setColor(String color) { this.color = color; }
19581967
1959-
public Optional<String[]> getTools() { return ofNullable(this.tools); }
1968+
public String[] getTools() { return this.tools; }
19601969
public void setTools(String[] tools) { this.tools = tools; }
19611970
}"
19621971
`;

0 commit comments

Comments
 (0)