Skip to content

Commit

Permalink
feat(models): embedded allOf properties supported
Browse files Browse the repository at this point in the history
closes #80
  • Loading branch information
vmasek committed Aug 28, 2019
1 parent 146c91b commit a42aa4f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,24 @@ function parseInterfaceProperties(
? propSchema.additionalProperties.type
: propSchema.type
);
const isRef = !!parseReference(propSchema);
const allOfParsed = propSchema.allOf && propSchema.allOf.length
? parseInterfaceProperties(propSchema.allOf.reduce<{[key: string]: Schema}>((acc, prop, i) => ({...acc, [i]: prop}), {}))
.map(prop => `${prop.typescriptType}${prop.isArray ? '[]' : ''}`)
: [];
const allOfImports = allOfParsed.map(prop => prop.replace('[]', ''));
const type = typescriptType.replace('[]', '');

return {
isArray,
isDictionary: propSchema.additionalProperties,
isRef: !!parseReference(propSchema),
isRef,
isRequired: requiredProps.includes(propName),
name: /^[A-Za-z_$][\w$]*$/.test(propName) || propName === ADDITIONAL_PROPERTIES_KEY ? propName : `'${propName}'`,
description: replaceNewLines(propSchema.description),
type: typescriptType.replace('[]', ''),
typescriptType,
type: type,
typescriptType: allOfParsed.length ? allOfParsed.join(' & ') : typescriptType,
imports: isRef ? [type, ...allOfImports] : allOfImports,
};
}
).sort(compareStringByKey('name')); // tslint:disable-line:no-array-mutation
Expand Down Expand Up @@ -326,11 +334,11 @@ function defineInterface(schema: Schema, definitionKey: string): Definition {
name,
description: replaceNewLines(schema.description, '$1 * '),
properties: properties,
imports: properties
.filter(({isRef}) => isRef)
.map(({type}) => type || '')
imports: properties.reduce(
(acc, {imports = []}) => [...acc, ...imports],
extendInterface ? [extendInterface] : []
)
.filter((type) => type !== name)
.concat(extendInterface ? [extendInterface] : [])
.sort() // tslint:disable-line:no-array-mutation
// filter duplicate imports
.filter((el, i, a) => (i === a.indexOf(el)) ? 1 : 0),
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface Property {
readonly typescriptType?: TypescriptBasicTypes | string;
readonly importType?: string;
readonly required?: boolean;
readonly imports?: string[];
}

export interface Parameter extends Property {
Expand Down
2 changes: 1 addition & 1 deletion templates/ngx-model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
*/
{{/description}}export interface {{&name}} {{#extend}}extends {{.}} {{/extend}}{
{{#properties}}
{{&name}}{{^isRequired}}?{{/isRequired}}: {{#isDictionary}}{ [key: string]: {{typescriptType}} }{{/isDictionary}}{{^isDictionary}}{{typescriptType}}{{/isDictionary}}{{#isArray}}[]{{/isArray}};{{#description}} // {{&.}}{{/description}}
{{&name}}{{^isRequired}}?{{/isRequired}}: {{#isDictionary}}{ [key: string]: {{&typescriptType}} }{{/isDictionary}}{{^isDictionary}}{{&typescriptType}}{{/isDictionary}}{{#isArray}}[]{{/isArray}};{{#description}} // {{&.}}{{/description}}
{{/properties}}
}
{{/isEnum}}
Expand Down
8 changes: 8 additions & 0 deletions tests/custom/api/models/data-model.model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/* tslint:disable */
import {
Data,
ItemList,
Pet,
} from '.';

export interface DataModel {
audioConfig?: Data;
entities?: number[];
id?: number;
imageData?: string; // base64 user uploaded image string
imageUrl?: string; // url to user avatar image
roleId?: number;
testWithArray?: Pet[] & Data;
text?: ItemList & Data;
}
18 changes: 18 additions & 0 deletions tests/custom/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ definitions:
imageUrl:
type: string
description: url to user avatar image
text:
title: TextInput
allOf:
- $ref: '#/definitions/ItemList'
- $ref: '#/definitions/Data'
description: The natural language text to be processed
testWithArray:
title: TextInput
allOf:
- type: array
items: { type: object, '$ref': '#/definitions/Pet' }
- $ref: '#/definitions/Data'
description: The natural language text to be processed
audioConfig:
title: InputAudioConfig
allOf:
- $ref: '#/definitions/Data'
description: Instructs the speech recognizer how to process the speech audio
TestModel:
type: object
properties:
Expand Down

0 comments on commit a42aa4f

Please sign in to comment.