From beb33c0daf317f77729d2c75e3efa7e4258f3e72 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Thu, 25 Apr 2024 10:41:36 +0200 Subject: [PATCH] fix: java does not import correct types (#1948) --- src/generators/java/JavaConstrainer.ts | 9 ++++++--- src/generators/java/renderers/ClassRenderer.ts | 7 ------- .../generators/java/presets/CommonPreset.spec.ts | 16 ++++++++-------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/generators/java/JavaConstrainer.ts b/src/generators/java/JavaConstrainer.ts index fc850c81cb..afda26cf82 100644 --- a/src/generators/java/JavaConstrainer.ts +++ b/src/generators/java/JavaConstrainer.ts @@ -193,16 +193,18 @@ export const JavaDefaultTypeMapping: JavaTypeMapping = { type: 'boolean' }); }, - Tuple({ options }): string { + Tuple({ options, dependencyManager }): string { //Because Java have no notion of tuples (and no custom implementation), we have to render it as a list of any value. const tupleType = 'Object'; if (options.collectionType && options.collectionType === 'List') { + dependencyManager.addDependency('import java.util.List;'); return `List<${tupleType}>`; } return `${tupleType}[]`; }, - Array({ constrainedModel, options }): string { + Array({ constrainedModel, options, dependencyManager }): string { if (options.collectionType && options.collectionType === 'List') { + dependencyManager.addDependency('import java.util.List;'); return `List<${constrainedModel.valueModel.type}>`; } return `${constrainedModel.valueModel.type}[]`; @@ -230,11 +232,12 @@ export const JavaDefaultTypeMapping: JavaTypeMapping = { } return constrainedModel.name; }, - Dictionary({ constrainedModel }): string { + Dictionary({ constrainedModel, dependencyManager }): string { //Limitations to Java is that maps cannot have specific value types... if (constrainedModel.value.type === 'int') { constrainedModel.value.type = 'Integer'; } + dependencyManager.addDependency('import java.util.Map;'); return `Map<${constrainedModel.key.type}, ${constrainedModel.value.type}>`; } }; diff --git a/src/generators/java/renderers/ClassRenderer.ts b/src/generators/java/renderers/ClassRenderer.ts index 7f33076523..2abd8b8c47 100644 --- a/src/generators/java/renderers/ClassRenderer.ts +++ b/src/generators/java/renderers/ClassRenderer.ts @@ -25,13 +25,6 @@ export class ClassRenderer extends JavaRenderer { await this.runAdditionalContentPreset() ]; - if (this.options?.collectionType === 'List') { - this.dependencyManager.addDependency('import java.util.List;'); - } - if (this.model.containsPropertyType(ConstrainedDictionaryModel)) { - this.dependencyManager.addDependency('import java.util.Map;'); - } - if (this.model.options.isExtended) { return `public interface ${this.model.name} { ${this.indent(this.renderBlock(content, 2))} diff --git a/test/generators/java/presets/CommonPreset.spec.ts b/test/generators/java/presets/CommonPreset.spec.ts index cf433fd2bd..db09e2c1a6 100644 --- a/test/generators/java/presets/CommonPreset.spec.ts +++ b/test/generators/java/presets/CommonPreset.spec.ts @@ -52,8 +52,8 @@ describe('JAVA_COMMON_PRESET', () => { expect(models).toHaveLength(1); expect(models[0].result).toMatchSnapshot(); expect(models[0].dependencies).toEqual([ - 'import java.util.Objects;', - 'import java.util.Map;' + 'import java.util.Map;', + 'import java.util.Objects;' ]); }); test('should not render any functions when all 4 options are disabled', async () => { @@ -94,8 +94,8 @@ describe('JAVA_COMMON_PRESET', () => { expect(models).toHaveLength(1); expect(models[0].result).toMatchSnapshot(); expect(models[0].dependencies).toEqual([ - 'import java.util.Objects;', - 'import java.util.Map;' + 'import java.util.Map;', + 'import java.util.Objects;' ]); }); test('should render hashCode', async () => { @@ -116,8 +116,8 @@ describe('JAVA_COMMON_PRESET', () => { expect(models).toHaveLength(1); expect(models[0].result).toMatchSnapshot(); expect(models[0].dependencies).toEqual([ - 'import java.util.Objects;', - 'import java.util.Map;' + 'import java.util.Map;', + 'import java.util.Objects;' ]); }); test('should render classToString', async () => { @@ -157,9 +157,9 @@ describe('JAVA_COMMON_PRESET', () => { expect(models).toHaveLength(1); expect(models[0].result).toMatchSnapshot(); expect(models[0].dependencies).toEqual([ + 'import java.util.Map;', 'import java.util.stream;', - 'import org.json.JSONObject;', - 'import java.util.Map;' + 'import org.json.JSONObject;' ]); }); test('should not render anything when isExtended is true', async () => {