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

fix: java does not import correct types #1948

Merged
merged 1 commit into from
Apr 25, 2024
Merged
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
9 changes: 6 additions & 3 deletions src/generators/java/JavaConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}[]`;
Expand Down Expand Up @@ -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}>`;
}
};
Expand Down
7 changes: 0 additions & 7 deletions src/generators/java/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ export class ClassRenderer extends JavaRenderer<ConstrainedObjectModel> {
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))}
Expand Down
16 changes: 8 additions & 8 deletions test/generators/java/presets/CommonPreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down
Loading