Skip to content

Commit

Permalink
fix: java does not import correct types (asyncapi#1948)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Apr 29, 2024
1 parent a18195c commit e40eb69
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
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

0 comments on commit e40eb69

Please sign in to comment.