Skip to content

Commit

Permalink
fix(template): generate nullable interface when isNullable is true
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Mar 4, 2024
1 parent e2daf50 commit c1223ca
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface WithEnumExtension {
'x-enum-varnames'?: string[];
'x-enum-descriptions'?: string[];
'x-enum-varnames'?: string[];
}
72 changes: 36 additions & 36 deletions src/openApi/v3/interfaces/OpenApiSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,51 @@ import type { OpenApiXml } from './OpenApiXml';
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject
*/
export interface OpenApiSchema extends OpenApiReference, WithEnumExtension {
title?: string;
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: boolean;
minimum?: number;
exclusiveMinimum?: boolean;
maxLength?: number;
minLength?: number;
pattern?: string;
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
maxProperties?: number;
minProperties?: number;
required?: string[];
enum?: (string | number)[];
type?: string | string[];
const?: string | number | boolean | null;
additionalProperties?: boolean | OpenApiSchema;
allOf?: OpenApiSchema[];
oneOf?: OpenApiSchema[];
anyOf?: OpenApiSchema[];
not?: OpenApiSchema[];
items?: OpenApiSchema;
properties?: Dictionary<OpenApiSchema>;
additionalProperties?: boolean | OpenApiSchema;
const?: string | number | boolean | null;
default?: any;
deprecated?: boolean;
description?: string;
discriminator?: OpenApiDiscriminator;
enum?: (string | number)[];
example?: any;
exclusiveMaximum?: boolean;
exclusiveMinimum?: boolean;
externalDocs?: OpenApiExternalDocs;
format?:
| 'int32'
| 'int64'
| 'float'
| 'double'
| 'string'
| 'binary'
| 'boolean'
| 'byte'
| 'binary'
| 'date'
| 'date-time'
| 'password';
default?: any;
| 'date'
| 'double'
| 'float'
| 'int32'
| 'int64'
| 'password'
| 'string';
items?: OpenApiSchema;
maximum?: number;
maxItems?: number;
maxLength?: number;
maxProperties?: number;
minimum?: number;
minItems?: number;
minLength?: number;
minProperties?: number;
multipleOf?: number;
not?: OpenApiSchema[];
nullable?: boolean;
discriminator?: OpenApiDiscriminator;
oneOf?: OpenApiSchema[];
pattern?: string;
properties?: Dictionary<OpenApiSchema>;
readOnly?: boolean;
required?: string[];
title?: string;
type?: string | string[];
uniqueItems?: boolean;
writeOnly?: boolean;
xml?: OpenApiXml;
externalDocs?: OpenApiExternalDocs;
example?: any;
deprecated?: boolean;
}
78 changes: 39 additions & 39 deletions src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,45 @@ export const getModel = (
parentDefinition: OpenApiSchema | null = null
): Model => {
const model: Model = {
name,
export: 'interface',
type: 'any',
base: 'any',
template: null,
link: null,
deprecated: Boolean(definition.deprecated),
description: definition.description || null,
deprecated: definition.deprecated === true,
enum: [],
enums: [],
exclusiveMaximum: definition.exclusiveMaximum,
exclusiveMinimum: definition.exclusiveMinimum,
export: 'interface',
format: definition.format,
imports: [],
isDefinition,
isReadOnly: definition.readOnly === true,
isNullable: definition.nullable === true,
isReadOnly: definition.readOnly === true,
isRequired: false,
format: definition.format,
link: null,
maximum: definition.maximum,
exclusiveMaximum: definition.exclusiveMaximum,
minimum: definition.minimum,
exclusiveMinimum: definition.exclusiveMinimum,
multipleOf: definition.multipleOf,
maxLength: definition.maxLength,
minLength: definition.minLength,
maxItems: definition.maxItems,
minItems: definition.minItems,
uniqueItems: definition.uniqueItems,
maxLength: definition.maxLength,
maxProperties: definition.maxProperties,
minimum: definition.minimum,
minItems: definition.minItems,
minLength: definition.minLength,
minProperties: definition.minProperties,
multipleOf: definition.multipleOf,
name,
pattern: getPattern(definition.pattern),
imports: [],
enum: [],
enums: [],
properties: [],
template: null,
type: 'any',
uniqueItems: definition.uniqueItems,
};

if (definition.$ref) {
const definitionRef = getType(definition.$ref);
model.export = 'reference';
model.type = definitionRef.type;
model.base = definitionRef.base;
model.template = definitionRef.template;
model.export = 'reference';
model.imports.push(...definitionRef.imports);
model.template = definitionRef.template;
model.type = definitionRef.type;
model.default = getModelDefault(definition, model);
return model;
}
Expand All @@ -64,10 +64,10 @@ export const getModel = (
const enumerators = getEnum(definition.enum);
const extendedEnumerators = extendEnum(enumerators, definition);
if (extendedEnumerators.length) {
model.export = 'enum';
model.type = 'string';
model.base = 'string';
model.enum.push(...extendedEnumerators);
model.export = 'enum';
model.type = 'string';
model.default = getModelDefault(definition, model);
return model;
}
Expand All @@ -76,11 +76,11 @@ export const getModel = (
if (definition.type === 'array' && definition.items) {
if (definition.items.$ref) {
const arrayItems = getType(definition.items.$ref);
model.export = 'array';
model.type = arrayItems.type;
model.base = arrayItems.base;
model.template = arrayItems.template;
model.export = 'array';
model.imports.push(...arrayItems.imports);
model.template = arrayItems.template;
model.type = arrayItems.type;
model.default = getModelDefault(definition, model);
return model;
}
Expand All @@ -93,12 +93,12 @@ export const getModel = (
}

const arrayItems = getModel(openApi, definition.items);
model.export = 'array';
model.type = arrayItems.type;
model.base = arrayItems.base;
model.template = arrayItems.template;
model.link = arrayItems;
model.export = 'array';
model.imports.push(...arrayItems.imports);
model.link = arrayItems;
model.template = arrayItems.template;
model.type = arrayItems.type;
model.default = getModelDefault(definition, model);
return model;
}
Expand All @@ -117,15 +117,15 @@ export const getModel = (

if (definition.type === 'object') {
if (definition.properties) {
model.base = 'any';
model.export = 'interface';
model.type = 'any';
model.base = 'any';
model.default = getModelDefault(definition, model);

const modelProperties = getModelProperties(openApi, definition, getModel, model);
modelProperties.forEach(modelProperty => {
model.imports.push(...modelProperty.imports);
model.enums.push(...modelProperty.enums);
model.imports.push(...modelProperty.imports);
model.properties.push(modelProperty);
if (modelProperty.export === 'enum') {
model.enums.push(modelProperty);
Expand All @@ -144,23 +144,23 @@ export const getModel = (
}

if (definition.const !== undefined) {
model.export = 'const';
const definitionConst = definition.const;
const modelConst = typeof definitionConst === 'string' ? `"${definitionConst}"` : `${definitionConst}`;
model.type = modelConst;
model.base = modelConst;
model.export = 'const';
model.type = modelConst;
return model;
}

// If the schema has a type than it can be a basic or generic type.
if (definition.type) {
const definitionType = getType(definition.type, definition.format);
model.export = 'generic';
model.type = definitionType.type;
model.base = definitionType.base;
model.template = definitionType.template;
model.isNullable = definitionType.isNullable || model.isNullable;
model.export = 'generic';
model.imports.push(...definitionType.imports);
model.isNullable = definitionType.isNullable || model.isNullable;
model.template = definitionType.template;
model.type = definitionType.type;
model.default = getModelDefault(definition, model);
return model;
}
Expand Down
18 changes: 9 additions & 9 deletions src/openApi/v3/parser/getModelComposition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,21 @@ export const getModelComposition = ({
});
} else {
composition.properties.push({
name: 'properties',
export: 'interface',
type: 'any',
base: 'any',
template: null,
link: null,
description: '',
enum: [],
enums: [],
export: 'interface',
imports: [],
isDefinition: false,
isReadOnly: false,
isNullable: false,
isReadOnly: false,
isRequired: false,
imports: [],
enum: [],
enums: [],
link: null,
name: 'properties',
properties,
template: null,
type: 'any',
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/partials/exportInterface.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type {{{name}}} = {
{{/ifdef}}
{{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type parent=../name}};
{{/each}}
};
}{{>isNullable}};
{{#if enums}}
{{#unless @root.useUnionTypes}}

Expand Down
Loading

0 comments on commit c1223ca

Please sign in to comment.