From a224c110e0e2554a091d282342ce4b8f87d13e2d Mon Sep 17 00:00:00 2001 From: Kenneth Aasan Date: Mon, 21 Aug 2023 17:33:56 +0200 Subject: [PATCH 1/5] feat: enable optional properties in java --- src/generators/java/JavaConstrainer.ts | 32 +++++-- src/generators/java/presets/JacksonPreset.ts | 24 ++++- src/helpers/CommonModelToMetaModel.ts | 4 + src/interpreter/InterpretProperties.ts | 3 + src/models/CommonModel.ts | 4 + src/models/ConstrainedMetaModel.ts | 1 - src/models/MetaModel.ts | 1 + test/generators/java/JavaConstrainer.spec.ts | 44 +++++++-- .../__snapshots__/JavaGenerator.spec.ts.snap | 17 ++-- .../java/presets/JacksonPreset.spec.ts | 3 +- .../__snapshots__/CommonPreset.spec.ts.snap | 96 +++++++++---------- .../__snapshots__/JacksonPreset.spec.ts.snap | 17 +++- .../unit/InterpretProperties.spec.ts | 2 + .../AsyncAPIInputProcessor.spec.ts.snap | 16 ++++ 14 files changed, 183 insertions(+), 81 deletions(-) diff --git a/src/generators/java/JavaConstrainer.ts b/src/generators/java/JavaConstrainer.ts index 1148ecf899..cfd834f7a1 100644 --- a/src/generators/java/JavaConstrainer.ts +++ b/src/generators/java/JavaConstrainer.ts @@ -1,6 +1,7 @@ import { Constraints } from '../../helpers'; import { ConstrainedEnumValueModel, + ConstrainedMetaModel, ConstrainedObjectModel, ConstrainedReferenceModel, ConstrainedUnionModel @@ -90,6 +91,21 @@ export function unionIncludesBuiltInTypes( ); } +function getType( + constrainedModel: ConstrainedMetaModel, + typeWhenNullableOrOptional: string, + type: string +) { + if ( + constrainedModel.options.isNullable || + !constrainedModel.options.isRequired + ) { + return typeWhenNullableOrOptional; + } + + return type; +} + export const JavaDefaultTypeMapping: JavaTypeMapping = { Object({ constrainedModel }): string { return constrainedModel.name; @@ -109,31 +125,27 @@ export const JavaDefaultTypeMapping: JavaTypeMapping = { return 'Object'; }, Float({ constrainedModel }): string { - let type = constrainedModel.options.isNullable ? 'Double' : 'double'; const format = constrainedModel.originalInput && constrainedModel.originalInput['format']; switch (format) { case 'float': - type = constrainedModel.options.isNullable ? 'Float' : 'float'; - break; + return getType(constrainedModel, 'Float', 'float'); } - return type; + return getType(constrainedModel, 'Double', 'double'); }, Integer({ constrainedModel }): string { - let type = constrainedModel.options.isNullable ? 'Integer' : 'int'; + const type = getType(constrainedModel, 'Integer', 'int'); const format = constrainedModel.originalInput && constrainedModel.originalInput['format']; switch (format) { case 'integer': case 'int32': - type = constrainedModel.options.isNullable ? 'Integer' : 'int'; - break; + return type; case 'long': case 'int64': - type = constrainedModel.options.isNullable ? 'Long' : 'long'; - break; + return getType(constrainedModel, 'Long', 'long'); } return type; }, @@ -160,7 +172,7 @@ export const JavaDefaultTypeMapping: JavaTypeMapping = { return type; }, Boolean({ constrainedModel }): string { - return constrainedModel.options.isNullable ? 'Boolean' : 'boolean'; + return getType(constrainedModel, 'Boolean', 'boolean'); }, Tuple({ options }): string { //Because Java have no notion of tuples (and no custom implementation), we have to render it as a list of any value. diff --git a/src/generators/java/presets/JacksonPreset.ts b/src/generators/java/presets/JacksonPreset.ts index 7fc1d3f753..b0f813bb9c 100644 --- a/src/generators/java/presets/JacksonPreset.ts +++ b/src/generators/java/presets/JacksonPreset.ts @@ -28,11 +28,27 @@ export const JAVA_JACKSON_PRESET: JavaPreset = { (property.property as ConstrainedDictionaryModel).serializationType === 'unwrap'; if (!hasUnwrappedOptions) { - const annotation = renderer.renderAnnotation( - 'JsonProperty', - `"${property.unconstrainedPropertyName}"` + const blocks: string[] = []; + + blocks.push( + renderer.renderAnnotation( + 'JsonProperty', + `"${property.unconstrainedPropertyName}"` + ) ); - return renderer.renderBlock([annotation, content]); + + if (!property.required) { + blocks.push( + renderer.renderAnnotation( + 'JsonInclude', + 'JsonInclude.Include.NON_NULL' + ) + ); + } + + blocks.push(content); + + return renderer.renderBlock(blocks); } return renderer.renderBlock([content]); } diff --git a/src/helpers/CommonModelToMetaModel.ts b/src/helpers/CommonModelToMetaModel.ts index dbbf52e13e..b4e5eeadf7 100644 --- a/src/helpers/CommonModelToMetaModel.ts +++ b/src/helpers/CommonModelToMetaModel.ts @@ -40,6 +40,10 @@ function getMetaModelOptions(commonModel: CommonModel): MetaModelOptions { }; } + if (commonModel.propertyIsRequired) { + options.isRequired = true; + } + return options; } diff --git a/src/interpreter/InterpretProperties.ts b/src/interpreter/InterpretProperties.ts index fcbf6efe22..a524190484 100644 --- a/src/interpreter/InterpretProperties.ts +++ b/src/interpreter/InterpretProperties.ts @@ -45,6 +45,9 @@ export default function interpretProperties( discriminator }); if (propertyModel !== undefined) { + if (model.required?.includes(propertyName)) { + propertyModel.propertyIsRequired = true; + } model.addProperty(propertyName, propertyModel, schema); } } diff --git a/src/models/CommonModel.ts b/src/models/CommonModel.ts index f0b96898bf..5b4c2015a1 100644 --- a/src/models/CommonModel.ts +++ b/src/models/CommonModel.ts @@ -29,6 +29,7 @@ export class CommonModel { required?: string[]; additionalItems?: CommonModel; union?: CommonModel[]; + propertyIsRequired?: boolean; /** * Takes a deep copy of the input object and converts it to an instance of CommonModel. @@ -820,6 +821,9 @@ export class CommonModel { ]; } + mergeTo.propertyIsRequired = + mergeTo.propertyIsRequired || mergeFrom.propertyIsRequired; + if ( CommonModel.idIncludesAnonymousSchema(mergeTo) && !CommonModel.idIncludesAnonymousSchema(mergeFrom) diff --git a/src/models/ConstrainedMetaModel.ts b/src/models/ConstrainedMetaModel.ts index 6e81e91160..5613aa132d 100644 --- a/src/models/ConstrainedMetaModel.ts +++ b/src/models/ConstrainedMetaModel.ts @@ -19,7 +19,6 @@ export interface ConstrainedMetaModelOptionsDiscriminator export class ConstrainedMetaModelOptions extends MetaModelOptions { const?: ConstrainedMetaModelOptionsConst; discriminator?: ConstrainedMetaModelOptionsDiscriminator; - isNullable?: boolean = false; } export abstract class ConstrainedMetaModel extends MetaModel { diff --git a/src/models/MetaModel.ts b/src/models/MetaModel.ts index 29e9704a61..a710753a37 100644 --- a/src/models/MetaModel.ts +++ b/src/models/MetaModel.ts @@ -10,6 +10,7 @@ export class MetaModelOptions { const?: MetaModelOptionsConst; discriminator?: MetaModelOptionsDiscriminator; isNullable?: boolean = false; + isRequired?: boolean = false; } export class MetaModel { diff --git a/test/generators/java/JavaConstrainer.spec.ts b/test/generators/java/JavaConstrainer.spec.ts index 1df238592e..339dc2cabd 100644 --- a/test/generators/java/JavaConstrainer.spec.ts +++ b/test/generators/java/JavaConstrainer.spec.ts @@ -108,13 +108,31 @@ describe('JavaConstrainer', () => { }); describe('Float', () => { test('should render type', () => { - const model = new ConstrainedFloatModel('test', undefined, {}, ''); + const model = new ConstrainedFloatModel( + 'test', + undefined, + { isRequired: true }, + '' + ); const type = JavaDefaultTypeMapping.Float({ constrainedModel: model, ...defaultOptions }); expect(type).toEqual('double'); }); + test('should render optional type', () => { + const model = new ConstrainedFloatModel( + 'test', + undefined, + { isRequired: false, isNullable: false }, + '' + ); + const type = JavaDefaultTypeMapping.Float({ + constrainedModel: model, + ...defaultOptions + }); + expect(type).toEqual('Double'); + }); test('should render nullable type', () => { const model = new ConstrainedFloatModel( 'test', @@ -132,7 +150,7 @@ describe('JavaConstrainer', () => { const model = new ConstrainedFloatModel( 'test', { format: 'float' }, - {}, + { isRequired: true }, '' ); const type = JavaDefaultTypeMapping.Float({ @@ -144,7 +162,12 @@ describe('JavaConstrainer', () => { }); describe('Integer', () => { test('should render type', () => { - const model = new ConstrainedIntegerModel('test', undefined, {}, ''); + const model = new ConstrainedIntegerModel( + 'test', + undefined, + { isRequired: true }, + '' + ); const type = JavaDefaultTypeMapping.Integer({ constrainedModel: model, ...defaultOptions @@ -168,7 +191,7 @@ describe('JavaConstrainer', () => { const model = new ConstrainedIntegerModel( 'test', { format: 'integer' }, - {}, + { isRequired: true }, '' ); const type = JavaDefaultTypeMapping.Integer({ @@ -181,7 +204,7 @@ describe('JavaConstrainer', () => { const model = new ConstrainedIntegerModel( 'test', { format: 'int32' }, - {}, + { isRequired: true }, '' ); const type = JavaDefaultTypeMapping.Integer({ @@ -194,7 +217,7 @@ describe('JavaConstrainer', () => { const model = new ConstrainedIntegerModel( 'test', { format: 'long' }, - {}, + { isRequired: true }, '' ); const type = JavaDefaultTypeMapping.Integer({ @@ -207,7 +230,7 @@ describe('JavaConstrainer', () => { const model = new ConstrainedIntegerModel( 'test', { format: 'int64' }, - {}, + { isRequired: true }, '' ); const type = JavaDefaultTypeMapping.Integer({ @@ -294,7 +317,12 @@ describe('JavaConstrainer', () => { }); describe('Boolean', () => { test('should render type', () => { - const model = new ConstrainedBooleanModel('test', undefined, {}, ''); + const model = new ConstrainedBooleanModel( + 'test', + undefined, + { isRequired: true }, + '' + ); const type = JavaDefaultTypeMapping.Boolean({ constrainedModel: model, ...defaultOptions diff --git a/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap b/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap index cb5bcfee98..1bd723227b 100644 --- a/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap +++ b/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap @@ -83,8 +83,10 @@ public interface Pet { @JsonProperty(\\"type\\") private final CloudEventType type = CloudEventType.DOG; @JsonProperty(\\"dataschema\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String dataschema; @JsonProperty(\\"time\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private java.time.OffsetDateTime time; private Map additionalProperties; @@ -198,8 +200,10 @@ public interface Pet { @JsonProperty(\\"type\\") private final CloudEventType type = CloudEventType.CAT; @JsonProperty(\\"dataschema\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String dataschema; @JsonProperty(\\"time\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private java.time.OffsetDateTime time; private Map additionalProperties; @@ -540,6 +544,7 @@ exports[`JavaGenerator oneOf/discriminator with jackson preset should create an Array [ "public class Cargo { @JsonProperty(\\"vehicle\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private Vehicle vehicle; private Map additionalProperties; @@ -748,7 +753,7 @@ Array [ private String city; private String state; private double houseNumber; - private boolean marriage; + private Boolean marriage; private Object members; private Object[] arrayType; private Map additionalProperties; @@ -765,8 +770,8 @@ Array [ public double getHouseNumber() { return this.houseNumber; } public void setHouseNumber(double houseNumber) { this.houseNumber = houseNumber; } - public boolean getMarriage() { return this.marriage; } - public void setMarriage(boolean marriage) { this.marriage = marriage; } + public Boolean getMarriage() { return this.marriage; } + public void setMarriage(Boolean marriage) { this.marriage = marriage; } public Object getMembers() { return this.members; } public void setMembers(Object members) { this.members = members; } @@ -953,7 +958,7 @@ public class Address { private String city; private String state; private double houseNumber; - private boolean marriage; + private Boolean marriage; private Object members; private Object[] arrayType; private OtherModel otherModel; @@ -971,8 +976,8 @@ public class Address { public double getHouseNumber() { return this.houseNumber; } public void setHouseNumber(double houseNumber) { this.houseNumber = houseNumber; } - public boolean getMarriage() { return this.marriage; } - public void setMarriage(boolean marriage) { this.marriage = marriage; } + public Boolean getMarriage() { return this.marriage; } + public void setMarriage(Boolean marriage) { this.marriage = marriage; } public Object getMembers() { return this.members; } public void setMembers(Object members) { this.members = members; } diff --git a/test/generators/java/presets/JacksonPreset.spec.ts b/test/generators/java/presets/JacksonPreset.spec.ts index efe0646fae..e140589a82 100644 --- a/test/generators/java/presets/JacksonPreset.spec.ts +++ b/test/generators/java/presets/JacksonPreset.spec.ts @@ -14,7 +14,8 @@ describe('JAVA_JACKSON_PRESET', () => { properties: { min_number_prop: { type: 'number' }, max_number_prop: { type: 'number' } - } + }, + required: ['min_number_prop'] }; const expectedDependencies = [ 'import java.util.Map;', diff --git a/test/generators/java/presets/__snapshots__/CommonPreset.spec.ts.snap b/test/generators/java/presets/__snapshots__/CommonPreset.spec.ts.snap index 19377d5950..accc8c5036 100644 --- a/test/generators/java/presets/__snapshots__/CommonPreset.spec.ts.snap +++ b/test/generators/java/presets/__snapshots__/CommonPreset.spec.ts.snap @@ -4,8 +4,8 @@ exports[`JAVA_COMMON_PRESET should render accurately when there is no additional "public class Clazz { private boolean requiredProp; private String stringProp; - private double numberProp; - private boolean booleanProp; + private Double numberProp; + private Boolean booleanProp; private String[] arrayProp; public boolean getRequiredProp() { return this.requiredProp; } @@ -14,11 +14,11 @@ exports[`JAVA_COMMON_PRESET should render accurately when there is no additional public String getStringProp() { return this.stringProp; } public void setStringProp(String stringProp) { this.stringProp = stringProp; } - public double getNumberProp() { return this.numberProp; } - public void setNumberProp(double numberProp) { this.numberProp = numberProp; } + public Double getNumberProp() { return this.numberProp; } + public void setNumberProp(Double numberProp) { this.numberProp = numberProp; } - public boolean getBooleanProp() { return this.booleanProp; } - public void setBooleanProp(boolean booleanProp) { this.booleanProp = booleanProp; } + public Boolean getBooleanProp() { return this.booleanProp; } + public void setBooleanProp(Boolean booleanProp) { this.booleanProp = booleanProp; } public String[] getArrayProp() { return this.arrayProp; } public void setArrayProp(String[] arrayProp) { this.arrayProp = arrayProp; } @@ -73,8 +73,8 @@ exports[`JAVA_COMMON_PRESET should render common function in class by common pre "public class Clazz { private boolean requiredProp; private String stringProp; - private double numberProp; - private boolean booleanProp; + private Double numberProp; + private Boolean booleanProp; private String[] arrayProp; private Map additionalProperties; @@ -84,11 +84,11 @@ exports[`JAVA_COMMON_PRESET should render common function in class by common pre public String getStringProp() { return this.stringProp; } public void setStringProp(String stringProp) { this.stringProp = stringProp; } - public double getNumberProp() { return this.numberProp; } - public void setNumberProp(double numberProp) { this.numberProp = numberProp; } + public Double getNumberProp() { return this.numberProp; } + public void setNumberProp(Double numberProp) { this.numberProp = numberProp; } - public boolean getBooleanProp() { return this.booleanProp; } - public void setBooleanProp(boolean booleanProp) { this.booleanProp = booleanProp; } + public Boolean getBooleanProp() { return this.booleanProp; } + public void setBooleanProp(Boolean booleanProp) { this.booleanProp = booleanProp; } public String[] getArrayProp() { return this.arrayProp; } public void setArrayProp(String[] arrayProp) { this.arrayProp = arrayProp; } @@ -148,8 +148,8 @@ exports[`JAVA_COMMON_PRESET with option should not render any functions when all "public class Clazz { private boolean requiredProp; private String stringProp; - private double numberProp; - private boolean booleanProp; + private Double numberProp; + private Boolean booleanProp; private String[] arrayProp; private Map additionalProperties; @@ -159,11 +159,11 @@ exports[`JAVA_COMMON_PRESET with option should not render any functions when all public String getStringProp() { return this.stringProp; } public void setStringProp(String stringProp) { this.stringProp = stringProp; } - public double getNumberProp() { return this.numberProp; } - public void setNumberProp(double numberProp) { this.numberProp = numberProp; } + public Double getNumberProp() { return this.numberProp; } + public void setNumberProp(Double numberProp) { this.numberProp = numberProp; } - public boolean getBooleanProp() { return this.booleanProp; } - public void setBooleanProp(boolean booleanProp) { this.booleanProp = booleanProp; } + public Boolean getBooleanProp() { return this.booleanProp; } + public void setBooleanProp(Boolean booleanProp) { this.booleanProp = booleanProp; } public String[] getArrayProp() { return this.arrayProp; } public void setArrayProp(String[] arrayProp) { this.arrayProp = arrayProp; } @@ -177,8 +177,8 @@ exports[`JAVA_COMMON_PRESET with option should render all functions 1`] = ` "public class Clazz { private boolean requiredProp; private String stringProp; - private double numberProp; - private boolean booleanProp; + private Double numberProp; + private Boolean booleanProp; private String[] arrayProp; private Map additionalProperties; @@ -188,11 +188,11 @@ exports[`JAVA_COMMON_PRESET with option should render all functions 1`] = ` public String getStringProp() { return this.stringProp; } public void setStringProp(String stringProp) { this.stringProp = stringProp; } - public double getNumberProp() { return this.numberProp; } - public void setNumberProp(double numberProp) { this.numberProp = numberProp; } + public Double getNumberProp() { return this.numberProp; } + public void setNumberProp(Double numberProp) { this.numberProp = numberProp; } - public boolean getBooleanProp() { return this.booleanProp; } - public void setBooleanProp(boolean booleanProp) { this.booleanProp = booleanProp; } + public Boolean getBooleanProp() { return this.booleanProp; } + public void setBooleanProp(Boolean booleanProp) { this.booleanProp = booleanProp; } public String[] getArrayProp() { return this.arrayProp; } public void setArrayProp(String[] arrayProp) { this.arrayProp = arrayProp; } @@ -252,8 +252,8 @@ exports[`JAVA_COMMON_PRESET with option should render classToString 1`] = ` "public class Clazz { private boolean requiredProp; private String stringProp; - private double numberProp; - private boolean booleanProp; + private Double numberProp; + private Boolean booleanProp; private String[] arrayProp; private Map additionalProperties; @@ -263,11 +263,11 @@ exports[`JAVA_COMMON_PRESET with option should render classToString 1`] = ` public String getStringProp() { return this.stringProp; } public void setStringProp(String stringProp) { this.stringProp = stringProp; } - public double getNumberProp() { return this.numberProp; } - public void setNumberProp(double numberProp) { this.numberProp = numberProp; } + public Double getNumberProp() { return this.numberProp; } + public void setNumberProp(Double numberProp) { this.numberProp = numberProp; } - public boolean getBooleanProp() { return this.booleanProp; } - public void setBooleanProp(boolean booleanProp) { this.booleanProp = booleanProp; } + public Boolean getBooleanProp() { return this.booleanProp; } + public void setBooleanProp(Boolean booleanProp) { this.booleanProp = booleanProp; } public String[] getArrayProp() { return this.arrayProp; } public void setArrayProp(String[] arrayProp) { this.arrayProp = arrayProp; } @@ -304,8 +304,8 @@ exports[`JAVA_COMMON_PRESET with option should render equals 1`] = ` "public class Clazz { private boolean requiredProp; private String stringProp; - private double numberProp; - private boolean booleanProp; + private Double numberProp; + private Boolean booleanProp; private String[] arrayProp; private Map additionalProperties; @@ -315,11 +315,11 @@ exports[`JAVA_COMMON_PRESET with option should render equals 1`] = ` public String getStringProp() { return this.stringProp; } public void setStringProp(String stringProp) { this.stringProp = stringProp; } - public double getNumberProp() { return this.numberProp; } - public void setNumberProp(double numberProp) { this.numberProp = numberProp; } + public Double getNumberProp() { return this.numberProp; } + public void setNumberProp(Double numberProp) { this.numberProp = numberProp; } - public boolean getBooleanProp() { return this.booleanProp; } - public void setBooleanProp(boolean booleanProp) { this.booleanProp = booleanProp; } + public Boolean getBooleanProp() { return this.booleanProp; } + public void setBooleanProp(Boolean booleanProp) { this.booleanProp = booleanProp; } public String[] getArrayProp() { return this.arrayProp; } public void setArrayProp(String[] arrayProp) { this.arrayProp = arrayProp; } @@ -351,8 +351,8 @@ exports[`JAVA_COMMON_PRESET with option should render hashCode 1`] = ` "public class Clazz { private boolean requiredProp; private String stringProp; - private double numberProp; - private boolean booleanProp; + private Double numberProp; + private Boolean booleanProp; private String[] arrayProp; private Map additionalProperties; @@ -362,11 +362,11 @@ exports[`JAVA_COMMON_PRESET with option should render hashCode 1`] = ` public String getStringProp() { return this.stringProp; } public void setStringProp(String stringProp) { this.stringProp = stringProp; } - public double getNumberProp() { return this.numberProp; } - public void setNumberProp(double numberProp) { this.numberProp = numberProp; } + public Double getNumberProp() { return this.numberProp; } + public void setNumberProp(Double numberProp) { this.numberProp = numberProp; } - public boolean getBooleanProp() { return this.booleanProp; } - public void setBooleanProp(boolean booleanProp) { this.booleanProp = booleanProp; } + public Boolean getBooleanProp() { return this.booleanProp; } + public void setBooleanProp(Boolean booleanProp) { this.booleanProp = booleanProp; } public String[] getArrayProp() { return this.arrayProp; } public void setArrayProp(String[] arrayProp) { this.arrayProp = arrayProp; } @@ -385,8 +385,8 @@ exports[`JAVA_COMMON_PRESET with option should render un/marshal 1`] = ` "public class Clazz { private boolean requiredProp; private String stringProp; - private double numberProp; - private boolean booleanProp; + private Double numberProp; + private Boolean booleanProp; private String[] arrayProp; private Map additionalProperties; @@ -396,11 +396,11 @@ exports[`JAVA_COMMON_PRESET with option should render un/marshal 1`] = ` public String getStringProp() { return this.stringProp; } public void setStringProp(String stringProp) { this.stringProp = stringProp; } - public double getNumberProp() { return this.numberProp; } - public void setNumberProp(double numberProp) { this.numberProp = numberProp; } + public Double getNumberProp() { return this.numberProp; } + public void setNumberProp(Double numberProp) { this.numberProp = numberProp; } - public boolean getBooleanProp() { return this.booleanProp; } - public void setBooleanProp(boolean booleanProp) { this.booleanProp = booleanProp; } + public Boolean getBooleanProp() { return this.booleanProp; } + public void setBooleanProp(Boolean booleanProp) { this.booleanProp = booleanProp; } public String[] getArrayProp() { return this.arrayProp; } public void setArrayProp(String[] arrayProp) { this.arrayProp = arrayProp; } diff --git a/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap b/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap index 5e1d129f34..87c635d97a 100644 --- a/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap +++ b/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap @@ -5,14 +5,15 @@ exports[`JAVA_JACKSON_PRESET should render Jackson annotations for class 1`] = ` @JsonProperty(\\"min_number_prop\\") private double minNumberProp; @JsonProperty(\\"max_number_prop\\") - private double maxNumberProp; + @JsonInclude(JsonInclude.Include.NON_NULL) + private Double maxNumberProp; private Map additionalProperties; public double getMinNumberProp() { return this.minNumberProp; } public void setMinNumberProp(double minNumberProp) { this.minNumberProp = minNumberProp; } - public double getMaxNumberProp() { return this.maxNumberProp; } - public void setMaxNumberProp(double maxNumberProp) { this.maxNumberProp = maxNumberProp; } + public Double getMaxNumberProp() { return this.maxNumberProp; } + public void setMaxNumberProp(Double maxNumberProp) { this.maxNumberProp = maxNumberProp; } public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -66,8 +67,10 @@ public interface Vehicle { }", "public class Car implements Vehicle { @JsonProperty(\\"vehicleType\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; @JsonProperty(\\"name\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String name; private Map additionalProperties; @@ -82,8 +85,10 @@ public interface Vehicle { }", "public class Truck implements Vehicle { @JsonProperty(\\"vehicleType\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; @JsonProperty(\\"name\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String name; private Map additionalProperties; @@ -114,6 +119,7 @@ public interface Vehicle { }", "public class Car implements Vehicle { @JsonProperty(\\"vehicleType\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; private Map additionalProperties; @@ -125,6 +131,7 @@ public interface Vehicle { }", "public class Truck implements Vehicle { @JsonProperty(\\"vehicleType\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; private Map additionalProperties; @@ -152,6 +159,7 @@ public interface Vehicle { }", "public class Car implements Vehicle { @JsonProperty(\\"vehicleType\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; private Map additionalProperties; @@ -163,6 +171,7 @@ public interface Vehicle { }", "public class Truck implements Vehicle { @JsonProperty(\\"vehicleType\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; private Map additionalProperties; @@ -190,6 +199,7 @@ public interface Vehicle { }", "public class Car implements Vehicle { @JsonProperty(\\"passengers\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String passengers; private Map additionalProperties; @@ -201,6 +211,7 @@ public interface Vehicle { }", "public class Truck implements Vehicle { @JsonProperty(\\"cargo\\") + @JsonInclude(JsonInclude.Include.NON_NULL) private String cargo; private Map additionalProperties; diff --git a/test/interpreter/unit/InterpretProperties.spec.ts b/test/interpreter/unit/InterpretProperties.spec.ts index 794e1aedc6..8ca41122bf 100644 --- a/test/interpreter/unit/InterpretProperties.spec.ts +++ b/test/interpreter/unit/InterpretProperties.spec.ts @@ -53,6 +53,7 @@ describe('Interpretation of properties', () => { test('should go trough properties and add it to model', () => { const schema: any = { properties: { property1: { type: 'string' } } }; const model = new CommonModel(); + model.required = ['property1']; const interpreter = new Interpreter(); const mockedReturnModel = new CommonModel(); (interpreter.interpret as jest.Mock).mockReturnValue(mockedReturnModel); @@ -70,6 +71,7 @@ describe('Interpretation of properties', () => { mockedReturnModel, schema ); + expect(mockedReturnModel.propertyIsRequired).toBe(true); }); test('should set discriminator when discriminator is set in interpreterOptions', () => { diff --git a/test/processors/__snapshots__/AsyncAPIInputProcessor.spec.ts.snap b/test/processors/__snapshots__/AsyncAPIInputProcessor.spec.ts.snap index 3502077cce..95986daec7 100644 --- a/test/processors/__snapshots__/AsyncAPIInputProcessor.spec.ts.snap +++ b/test/processors/__snapshots__/AsyncAPIInputProcessor.spec.ts.snap @@ -513,6 +513,7 @@ InputMetaModel { "name": "anonymous_schema_2", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "type": "string", @@ -528,6 +529,7 @@ InputMetaModel { "name": "anonymous_schema_3", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "format": "uri-reference", @@ -544,6 +546,7 @@ InputMetaModel { "name": "anonymous_schema_4", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "default": "1.0", @@ -582,6 +585,7 @@ InputMetaModel { "originalInput": "Dog", }, "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "allOf": Array [ @@ -1018,6 +1022,7 @@ InputMetaModel { "name": "anonymous_schema_2", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "type": "string", @@ -1033,6 +1038,7 @@ InputMetaModel { "name": "anonymous_schema_3", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "format": "uri-reference", @@ -1049,6 +1055,7 @@ InputMetaModel { "name": "anonymous_schema_4", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "default": "1.0", @@ -1087,6 +1094,7 @@ InputMetaModel { "originalInput": "Cat", }, "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "allOf": Array [ @@ -2468,6 +2476,7 @@ InputMetaModel { "name": "anonymous_schema_2", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "type": "string", @@ -2483,6 +2492,7 @@ InputMetaModel { "name": "anonymous_schema_3", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "format": "uri-reference", @@ -2499,6 +2509,7 @@ InputMetaModel { "name": "anonymous_schema_4", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "default": "1.0", @@ -2537,6 +2548,7 @@ InputMetaModel { "originalInput": "Dog", }, "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "allOf": Array [ @@ -2973,6 +2985,7 @@ InputMetaModel { "name": "anonymous_schema_2", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "type": "string", @@ -2988,6 +3001,7 @@ InputMetaModel { "name": "anonymous_schema_3", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "format": "uri-reference", @@ -3004,6 +3018,7 @@ InputMetaModel { "name": "anonymous_schema_4", "options": Object { "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "default": "1.0", @@ -3042,6 +3057,7 @@ InputMetaModel { "originalInput": "Cat", }, "isNullable": false, + "isRequired": true, }, "originalInput": AsyncapiV2Schema { "allOf": Array [ From 6d6e01acf3b127c9126de61bfca324558c0df30a Mon Sep 17 00:00:00 2001 From: Kenneth Aasan Date: Mon, 21 Aug 2023 17:44:54 +0200 Subject: [PATCH 2/5] feat: enable optional properties in java --- .../__snapshots__/index.spec.ts.snap | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap b/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap index 7179165cbf..5236bc1f41 100644 --- a/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap +++ b/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap @@ -4,16 +4,18 @@ exports[`Should be able to generate data models for jackson annotation and shoul Array [ "public class Root { @JsonProperty(\\"min_number_prop\\") - private double minNumberProp; + @JsonInclude(JsonInclude.Include.NON_NULL) + private Double minNumberProp; @JsonProperty(\\"max_number_prop\\") - private double maxNumberProp; + @JsonInclude(JsonInclude.Include.NON_NULL) + private Double maxNumberProp; private Map additionalProperties; - public double getMinNumberProp() { return this.minNumberProp; } - public void setMinNumberProp(double minNumberProp) { this.minNumberProp = minNumberProp; } + public Double getMinNumberProp() { return this.minNumberProp; } + public void setMinNumberProp(Double minNumberProp) { this.minNumberProp = minNumberProp; } - public double getMaxNumberProp() { return this.maxNumberProp; } - public void setMaxNumberProp(double maxNumberProp) { this.maxNumberProp = maxNumberProp; } + public Double getMaxNumberProp() { return this.maxNumberProp; } + public void setMaxNumberProp(Double maxNumberProp) { this.maxNumberProp = maxNumberProp; } public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } From 8d4384e1522499257f7876b09dbf0fa8dcb2d2a3 Mon Sep 17 00:00:00 2001 From: Kenneth Aasan Date: Tue, 22 Aug 2023 10:02:02 +0200 Subject: [PATCH 3/5] feat: enable optional properties in java --- src/generators/java/presets/JacksonPreset.ts | 31 +++++++++++++------ .../__snapshots__/JavaGenerator.spec.ts.snap | 8 +++++ .../__snapshots__/JacksonPreset.spec.ts.snap | 9 ++++++ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/generators/java/presets/JacksonPreset.ts b/src/generators/java/presets/JacksonPreset.ts index b0f813bb9c..ce48744183 100644 --- a/src/generators/java/presets/JacksonPreset.ts +++ b/src/generators/java/presets/JacksonPreset.ts @@ -27,16 +27,10 @@ export const JAVA_JACKSON_PRESET: JavaPreset = { isDictionary && (property.property as ConstrainedDictionaryModel).serializationType === 'unwrap'; - if (!hasUnwrappedOptions) { - const blocks: string[] = []; - blocks.push( - renderer.renderAnnotation( - 'JsonProperty', - `"${property.unconstrainedPropertyName}"` - ) - ); + const blocks: string[] = []; + if (hasUnwrappedOptions) { if (!property.required) { blocks.push( renderer.renderAnnotation( @@ -50,7 +44,26 @@ export const JAVA_JACKSON_PRESET: JavaPreset = { return renderer.renderBlock(blocks); } - return renderer.renderBlock([content]); + + blocks.push( + renderer.renderAnnotation( + 'JsonProperty', + `"${property.unconstrainedPropertyName}"` + ) + ); + + if (!property.required) { + blocks.push( + renderer.renderAnnotation( + 'JsonInclude', + 'JsonInclude.Include.NON_NULL' + ) + ); + } + + blocks.push(content); + + return renderer.renderBlock(blocks); } }, enum: { diff --git a/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap b/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap index 1bd723227b..f015415dbf 100644 --- a/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap +++ b/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap @@ -88,6 +88,7 @@ public interface Pet { @JsonProperty(\\"time\\") @JsonInclude(JsonInclude.Include.NON_NULL) private java.time.OffsetDateTime time; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getId() { return this.id; } @@ -205,6 +206,7 @@ public interface Pet { @JsonProperty(\\"time\\") @JsonInclude(JsonInclude.Include.NON_NULL) private java.time.OffsetDateTime time; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getId() { return this.id; } @@ -286,6 +288,7 @@ Array [ @NotNull @JsonProperty(\\"type\\") private final CloudEventType type = CloudEventType.DOG; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getId() { return this.id; } @@ -385,6 +388,7 @@ public interface Pet { @NotNull @JsonProperty(\\"type\\") private final DogType type = DogType.DOG; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public DogType getType() { return this.type; } @@ -463,6 +467,7 @@ public interface Pet { @NotNull @JsonProperty(\\"type\\") private final CatType type = CatType.CAT; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public CatType getType() { return this.type; } @@ -546,6 +551,7 @@ Array [ @JsonProperty(\\"vehicle\\") @JsonInclude(JsonInclude.Include.NON_NULL) private Vehicle vehicle; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public Vehicle getVehicle() { return this.vehicle; } @@ -607,6 +613,7 @@ public interface Vehicle { @NotNull @JsonProperty(\\"vehicleType\\") private final VehicleType vehicleType = VehicleType.CAR; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public VehicleType getVehicleType() { return this.vehicleType; } @@ -685,6 +692,7 @@ public interface Vehicle { @NotNull @JsonProperty(\\"vehicleType\\") private final VehicleType vehicleType = VehicleType.TRUCK; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public VehicleType getVehicleType() { return this.vehicleType; } diff --git a/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap b/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap index 87c635d97a..c4cc7a48e3 100644 --- a/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap +++ b/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap @@ -7,6 +7,7 @@ exports[`JAVA_JACKSON_PRESET should render Jackson annotations for class 1`] = ` @JsonProperty(\\"max_number_prop\\") @JsonInclude(JsonInclude.Include.NON_NULL) private Double maxNumberProp; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public double getMinNumberProp() { return this.minNumberProp; } @@ -72,6 +73,7 @@ public interface Vehicle { @JsonProperty(\\"name\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String name; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } @@ -90,6 +92,7 @@ public interface Vehicle { @JsonProperty(\\"name\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String name; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } @@ -121,6 +124,7 @@ public interface Vehicle { @JsonProperty(\\"vehicleType\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } @@ -133,6 +137,7 @@ public interface Vehicle { @JsonProperty(\\"vehicleType\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } @@ -161,6 +166,7 @@ public interface Vehicle { @JsonProperty(\\"vehicleType\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } @@ -173,6 +179,7 @@ public interface Vehicle { @JsonProperty(\\"vehicleType\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } @@ -201,6 +208,7 @@ public interface Vehicle { @JsonProperty(\\"passengers\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String passengers; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getPassengers() { return this.passengers; } @@ -213,6 +221,7 @@ public interface Vehicle { @JsonProperty(\\"cargo\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String cargo; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getCargo() { return this.cargo; } From 5df899167207d8219dae9000dc3c1a63a27b231a Mon Sep 17 00:00:00 2001 From: Kenneth Aasan Date: Tue, 22 Aug 2023 10:46:44 +0200 Subject: [PATCH 4/5] fix test --- .../__snapshots__/index.spec.ts.snap | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap b/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap index 5236bc1f41..1d3f1d6fc0 100644 --- a/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap +++ b/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap @@ -9,6 +9,7 @@ Array [ @JsonProperty(\\"max_number_prop\\") @JsonInclude(JsonInclude.Include.NON_NULL) private Double maxNumberProp; + @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public Double getMinNumberProp() { return this.minNumberProp; } From 82b39dd21b1dc63ee3fca6da208a56bda72b517b Mon Sep 17 00:00:00 2001 From: Kenneth Aasan Date: Tue, 22 Aug 2023 16:20:44 +0200 Subject: [PATCH 5/5] uses partOfProperty in java constrainer --- src/generators/java/JavaConstrainer.ts | 63 +++++++++---- src/helpers/CommonModelToMetaModel.ts | 4 - src/interpreter/InterpretProperties.ts | 3 - src/models/CommonModel.ts | 4 - src/models/MetaModel.ts | 1 - test/generators/java/JavaConstrainer.spec.ts | 93 +++++++++++++------ .../unit/InterpretProperties.spec.ts | 2 - .../AsyncAPIInputProcessor.spec.ts.snap | 16 ---- 8 files changed, 110 insertions(+), 76 deletions(-) diff --git a/src/generators/java/JavaConstrainer.ts b/src/generators/java/JavaConstrainer.ts index cfd834f7a1..6e4b2d7140 100644 --- a/src/generators/java/JavaConstrainer.ts +++ b/src/generators/java/JavaConstrainer.ts @@ -3,6 +3,7 @@ import { ConstrainedEnumValueModel, ConstrainedMetaModel, ConstrainedObjectModel, + ConstrainedObjectPropertyModel, ConstrainedReferenceModel, ConstrainedUnionModel } from '../../models'; @@ -91,15 +92,18 @@ export function unionIncludesBuiltInTypes( ); } -function getType( - constrainedModel: ConstrainedMetaModel, - typeWhenNullableOrOptional: string, - type: string -) { - if ( - constrainedModel.options.isNullable || - !constrainedModel.options.isRequired - ) { +function getType({ + constrainedModel, + partOfProperty, + typeWhenNullableOrOptional, + type +}: { + constrainedModel: ConstrainedMetaModel; + partOfProperty: ConstrainedObjectPropertyModel | undefined; + typeWhenNullableOrOptional: string; + type: string; +}) { + if (constrainedModel.options.isNullable || !partOfProperty?.required) { return typeWhenNullableOrOptional; } @@ -124,18 +128,33 @@ export const JavaDefaultTypeMapping: JavaTypeMapping = { Any(): string { return 'Object'; }, - Float({ constrainedModel }): string { + Float({ constrainedModel, partOfProperty }): string { const format = constrainedModel.originalInput && constrainedModel.originalInput['format']; switch (format) { case 'float': - return getType(constrainedModel, 'Float', 'float'); + return getType({ + constrainedModel, + partOfProperty, + typeWhenNullableOrOptional: 'Float', + type: 'float' + }); } - return getType(constrainedModel, 'Double', 'double'); + return getType({ + constrainedModel, + partOfProperty, + typeWhenNullableOrOptional: 'Double', + type: 'double' + }); }, - Integer({ constrainedModel }): string { - const type = getType(constrainedModel, 'Integer', 'int'); + Integer({ constrainedModel, partOfProperty }): string { + const type = getType({ + constrainedModel, + partOfProperty, + typeWhenNullableOrOptional: 'Integer', + type: 'int' + }); const format = constrainedModel.originalInput && constrainedModel.originalInput['format']; @@ -145,7 +164,12 @@ export const JavaDefaultTypeMapping: JavaTypeMapping = { return type; case 'long': case 'int64': - return getType(constrainedModel, 'Long', 'long'); + return getType({ + constrainedModel, + partOfProperty, + typeWhenNullableOrOptional: 'Long', + type: 'long' + }); } return type; }, @@ -171,8 +195,13 @@ export const JavaDefaultTypeMapping: JavaTypeMapping = { } return type; }, - Boolean({ constrainedModel }): string { - return getType(constrainedModel, 'Boolean', 'boolean'); + Boolean({ constrainedModel, partOfProperty }): string { + return getType({ + constrainedModel, + partOfProperty, + typeWhenNullableOrOptional: 'Boolean', + type: 'boolean' + }); }, Tuple({ options }): string { //Because Java have no notion of tuples (and no custom implementation), we have to render it as a list of any value. diff --git a/src/helpers/CommonModelToMetaModel.ts b/src/helpers/CommonModelToMetaModel.ts index b4e5eeadf7..dbbf52e13e 100644 --- a/src/helpers/CommonModelToMetaModel.ts +++ b/src/helpers/CommonModelToMetaModel.ts @@ -40,10 +40,6 @@ function getMetaModelOptions(commonModel: CommonModel): MetaModelOptions { }; } - if (commonModel.propertyIsRequired) { - options.isRequired = true; - } - return options; } diff --git a/src/interpreter/InterpretProperties.ts b/src/interpreter/InterpretProperties.ts index a524190484..fcbf6efe22 100644 --- a/src/interpreter/InterpretProperties.ts +++ b/src/interpreter/InterpretProperties.ts @@ -45,9 +45,6 @@ export default function interpretProperties( discriminator }); if (propertyModel !== undefined) { - if (model.required?.includes(propertyName)) { - propertyModel.propertyIsRequired = true; - } model.addProperty(propertyName, propertyModel, schema); } } diff --git a/src/models/CommonModel.ts b/src/models/CommonModel.ts index 5b4c2015a1..f0b96898bf 100644 --- a/src/models/CommonModel.ts +++ b/src/models/CommonModel.ts @@ -29,7 +29,6 @@ export class CommonModel { required?: string[]; additionalItems?: CommonModel; union?: CommonModel[]; - propertyIsRequired?: boolean; /** * Takes a deep copy of the input object and converts it to an instance of CommonModel. @@ -821,9 +820,6 @@ export class CommonModel { ]; } - mergeTo.propertyIsRequired = - mergeTo.propertyIsRequired || mergeFrom.propertyIsRequired; - if ( CommonModel.idIncludesAnonymousSchema(mergeTo) && !CommonModel.idIncludesAnonymousSchema(mergeFrom) diff --git a/src/models/MetaModel.ts b/src/models/MetaModel.ts index a710753a37..29e9704a61 100644 --- a/src/models/MetaModel.ts +++ b/src/models/MetaModel.ts @@ -10,7 +10,6 @@ export class MetaModelOptions { const?: MetaModelOptionsConst; discriminator?: MetaModelOptionsDiscriminator; isNullable?: boolean = false; - isRequired?: boolean = false; } export class MetaModel { diff --git a/test/generators/java/JavaConstrainer.spec.ts b/test/generators/java/JavaConstrainer.spec.ts index 339dc2cabd..af93d29218 100644 --- a/test/generators/java/JavaConstrainer.spec.ts +++ b/test/generators/java/JavaConstrainer.spec.ts @@ -9,6 +9,7 @@ import { ConstrainedFloatModel, ConstrainedIntegerModel, ConstrainedObjectModel, + ConstrainedObjectPropertyModel, ConstrainedReferenceModel, ConstrainedStringModel, ConstrainedTupleModel, @@ -108,27 +109,29 @@ describe('JavaConstrainer', () => { }); describe('Float', () => { test('should render type', () => { - const model = new ConstrainedFloatModel( - 'test', - undefined, - { isRequired: true }, - '' - ); + const model = new ConstrainedFloatModel('test', undefined, {}, ''); const type = JavaDefaultTypeMapping.Float({ constrainedModel: model, + partOfProperty: new ConstrainedObjectPropertyModel( + 'test', + 'test', + true, + model + ), ...defaultOptions }); expect(type).toEqual('double'); }); test('should render optional type', () => { - const model = new ConstrainedFloatModel( - 'test', - undefined, - { isRequired: false, isNullable: false }, - '' - ); + const model = new ConstrainedFloatModel('test', undefined, {}, ''); const type = JavaDefaultTypeMapping.Float({ constrainedModel: model, + partOfProperty: new ConstrainedObjectPropertyModel( + 'test', + 'test', + false, + model + ), ...defaultOptions }); expect(type).toEqual('Double'); @@ -150,11 +153,17 @@ describe('JavaConstrainer', () => { const model = new ConstrainedFloatModel( 'test', { format: 'float' }, - { isRequired: true }, + {}, '' ); const type = JavaDefaultTypeMapping.Float({ constrainedModel: model, + partOfProperty: new ConstrainedObjectPropertyModel( + 'test', + 'test', + true, + model + ), ...defaultOptions }); expect(type).toEqual('float'); @@ -162,14 +171,15 @@ describe('JavaConstrainer', () => { }); describe('Integer', () => { test('should render type', () => { - const model = new ConstrainedIntegerModel( - 'test', - undefined, - { isRequired: true }, - '' - ); + const model = new ConstrainedIntegerModel('test', undefined, {}, ''); const type = JavaDefaultTypeMapping.Integer({ constrainedModel: model, + partOfProperty: new ConstrainedObjectPropertyModel( + 'test', + 'test', + true, + model + ), ...defaultOptions }); expect(type).toEqual('int'); @@ -191,11 +201,17 @@ describe('JavaConstrainer', () => { const model = new ConstrainedIntegerModel( 'test', { format: 'integer' }, - { isRequired: true }, + {}, '' ); const type = JavaDefaultTypeMapping.Integer({ constrainedModel: model, + partOfProperty: new ConstrainedObjectPropertyModel( + 'test', + 'test', + true, + model + ), ...defaultOptions }); expect(type).toEqual('int'); @@ -204,11 +220,17 @@ describe('JavaConstrainer', () => { const model = new ConstrainedIntegerModel( 'test', { format: 'int32' }, - { isRequired: true }, + {}, '' ); const type = JavaDefaultTypeMapping.Integer({ constrainedModel: model, + partOfProperty: new ConstrainedObjectPropertyModel( + 'test', + 'test', + true, + model + ), ...defaultOptions }); expect(type).toEqual('int'); @@ -217,11 +239,17 @@ describe('JavaConstrainer', () => { const model = new ConstrainedIntegerModel( 'test', { format: 'long' }, - { isRequired: true }, + {}, '' ); const type = JavaDefaultTypeMapping.Integer({ constrainedModel: model, + partOfProperty: new ConstrainedObjectPropertyModel( + 'test', + 'test', + true, + model + ), ...defaultOptions }); expect(type).toEqual('long'); @@ -230,11 +258,17 @@ describe('JavaConstrainer', () => { const model = new ConstrainedIntegerModel( 'test', { format: 'int64' }, - { isRequired: true }, + {}, '' ); const type = JavaDefaultTypeMapping.Integer({ constrainedModel: model, + partOfProperty: new ConstrainedObjectPropertyModel( + 'test', + 'test', + true, + model + ), ...defaultOptions }); expect(type).toEqual('long'); @@ -317,14 +351,15 @@ describe('JavaConstrainer', () => { }); describe('Boolean', () => { test('should render type', () => { - const model = new ConstrainedBooleanModel( - 'test', - undefined, - { isRequired: true }, - '' - ); + const model = new ConstrainedBooleanModel('test', undefined, {}, ''); const type = JavaDefaultTypeMapping.Boolean({ constrainedModel: model, + partOfProperty: new ConstrainedObjectPropertyModel( + 'test', + 'test', + true, + model + ), ...defaultOptions }); expect(type).toEqual('boolean'); diff --git a/test/interpreter/unit/InterpretProperties.spec.ts b/test/interpreter/unit/InterpretProperties.spec.ts index 8ca41122bf..794e1aedc6 100644 --- a/test/interpreter/unit/InterpretProperties.spec.ts +++ b/test/interpreter/unit/InterpretProperties.spec.ts @@ -53,7 +53,6 @@ describe('Interpretation of properties', () => { test('should go trough properties and add it to model', () => { const schema: any = { properties: { property1: { type: 'string' } } }; const model = new CommonModel(); - model.required = ['property1']; const interpreter = new Interpreter(); const mockedReturnModel = new CommonModel(); (interpreter.interpret as jest.Mock).mockReturnValue(mockedReturnModel); @@ -71,7 +70,6 @@ describe('Interpretation of properties', () => { mockedReturnModel, schema ); - expect(mockedReturnModel.propertyIsRequired).toBe(true); }); test('should set discriminator when discriminator is set in interpreterOptions', () => { diff --git a/test/processors/__snapshots__/AsyncAPIInputProcessor.spec.ts.snap b/test/processors/__snapshots__/AsyncAPIInputProcessor.spec.ts.snap index 95986daec7..3502077cce 100644 --- a/test/processors/__snapshots__/AsyncAPIInputProcessor.spec.ts.snap +++ b/test/processors/__snapshots__/AsyncAPIInputProcessor.spec.ts.snap @@ -513,7 +513,6 @@ InputMetaModel { "name": "anonymous_schema_2", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "type": "string", @@ -529,7 +528,6 @@ InputMetaModel { "name": "anonymous_schema_3", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "format": "uri-reference", @@ -546,7 +544,6 @@ InputMetaModel { "name": "anonymous_schema_4", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "default": "1.0", @@ -585,7 +582,6 @@ InputMetaModel { "originalInput": "Dog", }, "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "allOf": Array [ @@ -1022,7 +1018,6 @@ InputMetaModel { "name": "anonymous_schema_2", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "type": "string", @@ -1038,7 +1033,6 @@ InputMetaModel { "name": "anonymous_schema_3", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "format": "uri-reference", @@ -1055,7 +1049,6 @@ InputMetaModel { "name": "anonymous_schema_4", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "default": "1.0", @@ -1094,7 +1087,6 @@ InputMetaModel { "originalInput": "Cat", }, "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "allOf": Array [ @@ -2476,7 +2468,6 @@ InputMetaModel { "name": "anonymous_schema_2", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "type": "string", @@ -2492,7 +2483,6 @@ InputMetaModel { "name": "anonymous_schema_3", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "format": "uri-reference", @@ -2509,7 +2499,6 @@ InputMetaModel { "name": "anonymous_schema_4", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "default": "1.0", @@ -2548,7 +2537,6 @@ InputMetaModel { "originalInput": "Dog", }, "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "allOf": Array [ @@ -2985,7 +2973,6 @@ InputMetaModel { "name": "anonymous_schema_2", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "type": "string", @@ -3001,7 +2988,6 @@ InputMetaModel { "name": "anonymous_schema_3", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "format": "uri-reference", @@ -3018,7 +3004,6 @@ InputMetaModel { "name": "anonymous_schema_4", "options": Object { "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "default": "1.0", @@ -3057,7 +3042,6 @@ InputMetaModel { "originalInput": "Cat", }, "isNullable": false, - "isRequired": true, }, "originalInput": AsyncapiV2Schema { "allOf": Array [