Skip to content

Commit 04111d9

Browse files
committed
feat(models): properties are optional as default, required stated explicitly
Signed-off-by: Vojtech Mašek <[email protected]>
1 parent 5d2949f commit 04111d9

File tree

204 files changed

+882
-875
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

204 files changed

+882
-875
lines changed

src/parser.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ function defineEnum(
222222
};
223223
}
224224

225-
function parseInterfaceProperties(properties: { [propertyName: string]: Schema } = {}): Property[] {
225+
function parseInterfaceProperties(
226+
properties: { [propertyName: string]: Schema } = {},
227+
requiredProps: string[] = [],
228+
): Property[] {
226229
return Object.entries<Schema>(properties).map(
227230
([propName, propSchema]: [string, Schema]) => {
228231
const isArray = /^array$/i.test(propSchema.type || '');
@@ -240,6 +243,7 @@ function parseInterfaceProperties(properties: { [propertyName: string]: Schema }
240243
isArray,
241244
isDictionary: propSchema.additionalProperties,
242245
isRef: !!parseReference(propSchema),
246+
isRequired: requiredProps.includes(propName),
243247
name: /^[A-Za-z_$][\w$]*$/.test(propName) ? propName : `'${propName}'`,
244248
description: replaceNewLines(propSchema.description),
245249
type: typescriptType.replace('[]', ''),
@@ -290,10 +294,13 @@ function defineInterface(schema: Schema, definitionKey: string): Definition {
290294
? toCamelCase(dereferenceType((schema.allOf.find(allOfSchema => !!allOfSchema.$ref) || {}).$ref), false)
291295
: undefined;
292296
const allOfProps: Schema = schema.allOf ? schema.allOf.reduce((props, allOfSchema) => ({...props, ...allOfSchema.properties}), {}) : {};
293-
const properties: Property[] = parseInterfaceProperties({
294-
...schema.properties,
295-
...allOfProps,
296-
} as { [propertyName: string]: Schema });
297+
const properties: Property[] = parseInterfaceProperties(
298+
{
299+
...schema.properties,
300+
...allOfProps,
301+
} as { [propertyName: string]: Schema },
302+
schema.required,
303+
);
297304

298305
return {
299306
name: name,

templates/ngx-model.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
*/
1515
{{/description}}export interface {{&name}} {{#extend}}extends {{.}} {{/extend}}{
1616
{{#properties}}
17-
{{&name}}: {{#isDictionary}}{ [key: string]: {{typescriptType}} }{{/isDictionary}}{{^isDictionary}}{{typescriptType}}{{/isDictionary}}{{#isArray}}[]{{/isArray}};{{#description}} // {{&.}}{{/description}}
17+
{{&name}}{{^isRequired}}?{{/isRequired}}: {{#isDictionary}}{ [key: string]: {{typescriptType}} }{{/isDictionary}}{{^isDictionary}}{{typescriptType}}{{/isDictionary}}{{#isArray}}[]{{/isArray}};{{#description}} // {{&.}}{{/description}}
1818
{{/properties}}
1919
}
2020
{{/isEnum}}
@@ -29,4 +29,4 @@ import {
2929
{{name}} = {{#isNumeric}}{{value}}{{/isNumeric}}{{^isNumeric}}"{{value}}"{{/isNumeric}},
3030
{{/properties}}
3131
}
32-
{{/isEnum}}
32+
{{/isEnum}}

tests/custom/api/models/cat.model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '.';
66

77
export interface Cat extends Pet {
8-
age: number;
9-
eaten: Mouse[];
10-
hunts: boolean;
8+
age?: number;
9+
eaten?: Mouse[];
10+
hunts?: boolean;
1111
}

tests/custom/api/models/customer.model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '.';
66

77
export interface Customer extends Model {
8-
address: string;
9-
name: string;
10-
right: Right;
8+
address?: string;
9+
name?: string;
10+
right?: Right;
1111
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* tslint:disable */
22

33
export interface DataModel {
4-
entities: number[];
5-
id: number;
6-
imageData: string; // base64 user uploaded image string
7-
imageUrl: string; // url to user avatar image
8-
roleId: number;
4+
entities?: number[];
5+
id?: number;
6+
imageData?: string; // base64 user uploaded image string
7+
imageUrl?: string; // url to user avatar image
8+
roleId?: number;
99
}

tests/custom/api/models/data.model.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* tslint:disable */
22

33
export interface Data {
4-
email: string;
5-
firstName: string;
6-
id: number;
7-
lastName: string;
8-
phone: string;
9-
title: string;
4+
email?: string;
5+
firstName?: string;
6+
id?: number;
7+
lastName?: string;
8+
phone?: string;
9+
title?: string;
1010
}

tests/custom/api/models/dog.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import {
44
} from '.';
55

66
export interface Dog extends Pet {
7-
bark: boolean;
8-
breed: string;
7+
bark?: boolean;
8+
breed?: string;
99
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* tslint:disable */
22

33
export interface Model {
4-
created: number;
5-
deleted: number;
6-
id: string;
7-
recursivePointer: Model;
8-
updated: number;
4+
created?: number;
5+
deleted?: number;
6+
id?: string;
7+
recursivePointer?: Model;
8+
updated?: number;
99
}

tests/custom/api/models/mouse.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import {
44
} from '.';
55

66
export interface Mouse extends Pet {
7-
color: string;
7+
color?: string;
88
}

tests/custom/api/models/my-interface.model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from '.';
55

66
export interface MyInterface {
7-
myId: string;
8-
myMap: { [key: string]: Data };
9-
myName: string;
7+
myId?: string;
8+
myMap?: { [key: string]: Data };
9+
myName?: string;
1010
}

0 commit comments

Comments
 (0)