Skip to content

Commit

Permalink
fix: non-required properties are required by constructor (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahakporwal02 authored Jan 10, 2022
1 parent b203752 commit d84b8b5
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Array [
email;
constructor(input) {
this.email = input.email;
}
get email() { return this.email; }
Expand Down
4 changes: 2 additions & 2 deletions examples/javascript-use-cjs/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Person {
email;
constructor(input) {
this.email = input.email;
}
get email() { return this.email; }
Expand All @@ -26,7 +26,7 @@ class Root {
person;
constructor(input) {
this.person = input.person;
}
get person() { return this.person; }
Expand Down
4 changes: 2 additions & 2 deletions examples/javascript-use-esm/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Person {
email;
constructor(input) {
this.email = input.email;
}
get email() { return this.email; }
Expand All @@ -27,7 +27,7 @@ class Root {
person;
constructor(input) {
this.person = input.person;
}
get person() { return this.person; }
Expand Down
2 changes: 1 addition & 1 deletion src/generators/javascript/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const JS_DEFAULT_CLASS_PRESET: ClassPreset<ClassRenderer> = {
},
ctor({ renderer, model }) {
const properties = model.properties || {};
const assigments = Object.entries(properties).map(([propertyName, property]) => {
const assigments = Object.entries(properties).filter(([propertyName]) => model.isRequired(propertyName)).map(([propertyName, property]) => {
propertyName = renderer.nameProperty(propertyName, property);
return `this.${propertyName} = input.${propertyName};`;
});
Expand Down
9 changes: 4 additions & 5 deletions test/generators/javascript/JavaScriptGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('JavaScriptGenerator', () => {
enum: { type: 'string' },
reservedEnum: { type: 'string' }
},
additionalProperties: false
additionalProperties: false,
required: ['reservedEnum', 'enum'],
};
const expected = `class Address {
reservedReservedEnum;
Expand Down Expand Up @@ -77,8 +78,6 @@ describe('JavaScriptGenerator', () => {
this.city = input.city;
this.state = input.state;
this.houseNumber = input.houseNumber;
this.marriage = input.marriage;
this.members = input.members;
this.arrayType = input.arrayType;
}
Expand Down Expand Up @@ -201,7 +200,7 @@ describe('JavaScriptGenerator', () => {
marriage: { type: 'boolean', description: 'Status if marriage live in given house' },
members: { oneOf: [{ type: 'string' }, { type: 'number' }, { type: 'boolean' }], },
array_type: { type: 'array', items: [{ type: 'string' }, { type: 'number' }] },
other_model: { type: 'object', $id: 'OtherModel', properties: { street_name: { type: 'string' } } },
other_model: { type: 'object', $id: 'OtherModel', properties: { street_name: { type: 'string' } }, required: ['street_name'] },
},
patternProperties: {
'^S(.?*)test&': {
Expand All @@ -227,7 +226,7 @@ describe('JavaScriptGenerator', () => {
marriage: { type: 'boolean', description: 'Status if marriage live in given house' },
members: { oneOf: [{ type: 'string' }, { type: 'number' }, { type: 'boolean' }], },
array_type: { type: 'array', items: [{ type: 'string' }, { type: 'number' }] },
other_model: { type: 'object', $id: 'OtherModel', properties: { street_name: { type: 'string' } } },
other_model: { type: 'object', $id: 'OtherModel', properties: { street_name: { type: 'string' } }, required: ['street_name'] },
},
patternProperties: {
'^S(.?*)test&': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ class Address {
this.city = input.city;
this.state = input.state;
this.houseNumber = input.houseNumber;
this.marriage = input.marriage;
this.members = input.members;
this.arrayType = input.arrayType;
this.otherModel = input.otherModel;
}
get streetName() { return this.streetName; }
Expand Down Expand Up @@ -99,10 +96,7 @@ class Address {
this.city = input.city;
this.state = input.state;
this.houseNumber = input.houseNumber;
this.marriage = input.marriage;
this.members = input.members;
this.arrayType = input.arrayType;
this.otherModel = input.otherModel;
}
get streetName() { return this.streetName; }
Expand Down

0 comments on commit d84b8b5

Please sign in to comment.