diff --git a/src/generators/java/JavaRenderer.ts b/src/generators/java/JavaRenderer.ts index 01e67452b7..b5c6324903 100644 --- a/src/generators/java/JavaRenderer.ts +++ b/src/generators/java/JavaRenderer.ts @@ -69,7 +69,18 @@ export abstract class JavaRenderer extends AbstractRenderer { 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: diff --git a/src/generators/java/renderers/ClassRenderer.ts b/src/generators/java/renderers/ClassRenderer.ts index 35cb78cad0..3f52a0952b 100644 --- a/src/generators/java/renderers/ClassRenderer.ts +++ b/src/generators/java/renderers/ClassRenderer.ts @@ -111,5 +111,5 @@ export const JAVA_DEFAULT_CLASS_PRESET: ClassPreset = { setterType = `Map`; } return `public void set${setterName}(${setterType} ${propertyName}) { this.${propertyName} = ${propertyName}; }`; - }, + } }; diff --git a/test/generators/java/JavaRenderer.spec.ts b/test/generators/java/JavaRenderer.spec.ts index 932e445ba8..bd399cd1aa 100644 --- a/test/generators/java/JavaRenderer.spec.ts +++ b/test/generators/java/JavaRenderer.spec.ts @@ -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()', () => {