From 83b0d20c43f7efa6aa4c26c2ec5a3bd2cbf0822c Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Fri, 6 Nov 2020 18:29:27 +0300 Subject: [PATCH] feat: mark not required string property as optional --- npm/ng-packs/packages/schematics/src/utils/model.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/utils/model.ts b/npm/ng-packs/packages/schematics/src/utils/model.ts index f1177d2340..fc8dde37c1 100644 --- a/npm/ng-packs/packages/schematics/src/utils/model.ts +++ b/npm/ng-packs/packages/schematics/src/utils/model.ts @@ -1,5 +1,5 @@ import { VOLO_NAME_VALUE, VOLO_REGEX } from '../constants'; -import { Interface, Model, Property, Type, TypeWithEnum } from '../models'; +import { Interface, Model, Property, PropertyDef, Type, TypeWithEnum } from '../models'; import { parseNamespace } from './namespace'; import { relativePathToModel } from './path'; import { camel } from './text'; @@ -119,7 +119,7 @@ export function createImportRefToInterfaceReducerCreator(params: ModelGeneratorP typeDef.properties?.forEach(prop => { const name = camel(prop.name); - const optional = prop.typeSimple.endsWith('?') ? '?' : ''; + const optional = isOptionalProperty(prop) ? '?' : ''; const type = simplifyType(prop.typeSimple); const refs = parseType(prop.type).reduce( (acc: string[], r) => acc.concat(parseGenerics(r).toGenerics()), @@ -150,3 +150,9 @@ export function createRefToImportReducerCreator(params: ModelGeneratorParams) { const { solution } = params; return (namespace: string) => createTypesToImportsReducer(solution, namespace); } + +function isOptionalProperty(prop: PropertyDef) { + return ( + prop.typeSimple.endsWith('?') || (prop.typeSimple === 'string' && prop.isRequired === false) + ); +}