Skip to content

Commit

Permalink
feat: render more accurate array type for Java output (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Jun 24, 2021
1 parent fe4cd62 commit 44d07db
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/generators/java/JavaRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,18 @@ export abstract class JavaRenderer extends AbstractRenderer<JavaOptions> {
case 'binary':
return 'byte[]';
case 'array': {
const newType = model?.items ? this.renderType(model.items) : 'Object';
let arrayItemModel = model.items;
//Since Java dont support tuples, lets make sure that we combine the tuple types to find the appropriate array type
if (Array.isArray(model.items)) {
arrayItemModel = model.items.reduce((prevModel, currentModel) => {
return CommonModel.mergeCommonModels(CommonModel.toCommonModel(prevModel), CommonModel.toCommonModel(currentModel), {});
});
//If tuples and additionalItems make sure to find the appropriate type by merging all the tuples and additionalItems model together to find the combined type.
if (model.additionalItems !== undefined) {
arrayItemModel = CommonModel.mergeCommonModels(arrayItemModel, model.additionalItems, {});
}
}
const newType = arrayItemModel ? this.renderType(arrayItemModel) : 'Object';
return `${newType}[]`;
}
default:
Expand Down
2 changes: 1 addition & 1 deletion src/generators/java/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,5 @@ export const JAVA_DEFAULT_CLASS_PRESET: ClassPreset<ClassRenderer> = {
setterType = `Map<String, ${setterType}>`;
}
return `public void set${setterName}(${setterType} ${propertyName}) { this.${propertyName} = ${propertyName}; }`;
},
}
};
52 changes: 52 additions & 0 deletions test/generators/java/JavaRenderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,58 @@ describe('JavaRenderer', () => {
test('Should be able to return byte array', () => {
expect(renderer.toJavaType('binary', new CommonModel())).toEqual('byte[]');
});
test('Should render matching tuple types as is', () => {
const model = CommonModel.toCommonModel({
items: [
{
type: 'string'
},
{
type: 'string'
}
]
});
expect(renderer.toJavaType('array', model)).toEqual('String[]');
});
test('Should render mismatching tuple types as Object', () => {
const model = CommonModel.toCommonModel({
items: [
{
type: 'string'
},
{
type: 'number'
}
]
});
expect(renderer.toJavaType('array', model)).toEqual('Object[]');
});
test('Should render matching tuple and additionalItem types', () => {
const model = CommonModel.toCommonModel({
items: [
{
type: 'string'
}
],
additionalItems: {
type: 'string'
}
});
expect(renderer.toJavaType('array', model)).toEqual('String[]');
});
test('Should render Object for tuple and additionalItem type mismatch', () => {
const model = CommonModel.toCommonModel({
items: [
{
type: 'string'
}
],
additionalItems: {
type: 'number'
}
});
expect(renderer.toJavaType('array', model)).toEqual('Object[]');
});
});

describe('toClassType()', () => {
Expand Down

0 comments on commit 44d07db

Please sign in to comment.