Browse Source

Handle nullable properties in model generator

Adds support for marking properties as nullable by appending '| null' to their type in the model generator. Also refines the logic for determining optional properties to better align with 'isRequired' and 'isNullable' flags.
pull/24335/head
Fahri Gedik 2 months ago
parent
commit
8fee4a8a10
  1. 10
      npm/ng-packs/packages/schematics/src/utils/model.ts

10
npm/ng-packs/packages/schematics/src/utils/model.ts

@ -153,6 +153,10 @@ export function createImportRefToInterfaceReducerCreator(params: ModelGeneratorP
type = simplifyType(prop.type);
}
if (prop.isNullable) {
type = `${type} | null`;
}
const refs = parseType(prop.type).reduce(
(acc: string[], r) => acc.concat(parseGenerics(r).toGenerics()),
[],
@ -186,11 +190,7 @@ export function createRefToImportReducerCreator(params: ModelGeneratorParams) {
}
function isOptionalProperty(prop: PropertyDef) {
return (
prop.isNullable ||
prop.typeSimple.endsWith('?') ||
((prop.typeSimple === 'string' || prop.typeSimple.includes('enum')) && !prop.isRequired)
);
return !prop.isRequired && prop.isNullable;
}
export function parseBaseTypeWithGenericTypes(type: string): string[] {

Loading…
Cancel
Save