ng-openapi-gen
Version:
An OpenAPI 3 codegen for Angular 12+
107 lines • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Model = void 0;
const enum_value_1 = require("./enum-value");
const gen_type_1 = require("./gen-type");
const gen_utils_1 = require("./gen-utils");
const property_1 = require("./property");
const lodash_1 = require("lodash");
/**
* Context to generate a model
*/
class Model extends gen_type_1.GenType {
constructor(openApi, name, schema, options) {
super(name, gen_utils_1.unqualifiedName, options);
this.openApi = openApi;
this.schema = schema;
const description = schema.description || '';
this.tsComments = (0, gen_utils_1.tsComments)(description, 0, schema.deprecated);
const type = schema.type || 'any';
// Handle enums
if ((schema.enum || []).length > 0 && ['string', 'number', 'integer'].includes(type)) {
this.enumArrayName = (0, lodash_1.upperCase)(this.typeName).replace(/\s+/g, '_');
this.enumArrayFileName = (0, gen_utils_1.fileName)(this.typeName + '-array');
const names = schema['x-enumNames'] || [];
const descriptions = schema['x-enumDescriptions'] || [];
const values = schema.enum || [];
this.enumValues = [];
for (let i = 0; i < values.length; i++) {
const enumValue = new enum_value_1.EnumValue(type, names[i], descriptions[i], values[i], options);
this.enumValues.push(enumValue);
}
// When enumStyle is 'alias' it is handled as a simple type.
this.isEnum = options.enumStyle !== 'alias';
}
const hasAllOf = schema.allOf && schema.allOf.length > 0;
const hasOneOf = schema.oneOf && schema.oneOf.length > 0;
this.isObject = (type === 'object' || !!schema.properties) && !schema.nullable && !hasAllOf && !hasOneOf;
this.isSimple = !this.isObject && !this.isEnum;
if (this.isObject) {
// Object
const propertiesByName = new Map();
this.collectObject(schema, propertiesByName);
const sortedNames = [...propertiesByName.keys()];
sortedNames.sort();
this.properties = sortedNames.map(propName => propertiesByName.get(propName));
}
else {
// Simple / array / enum / union / intersection
this.simpleType = (0, gen_utils_1.tsType)(schema, options, openApi, this);
}
this.collectImports(schema);
this.updateImports();
}
initPathToRoot() {
if (this.namespace) {
// for each namespace level go one directory up
// plus the "models" directory
return this.namespace.split('/').map(() => '../').join('').concat('../');
}
return '../';
}
skipImport(name) {
// Don't import own type
return this.name === name;
}
collectObject(schema, propertiesByName) {
if (schema.type === 'object' || !!schema.properties) {
// An object definition
const properties = schema.properties || {};
const required = schema.required || [];
const propNames = Object.keys(properties);
// When there are additional properties, we need an union of all types for it.
// See https://github.com/cyclosproject/ng-openapi-gen/issues/68
const propTypes = new Set();
const appendType = (type) => {
if (type.startsWith('null | ')) {
propTypes.add('null');
propTypes.add(type.substring('null | '.length));
}
else {
propTypes.add(type);
}
};
for (const propName of propNames) {
const prop = new property_1.Property(this, propName, properties[propName], required.includes(propName), this.options, this.openApi);
propertiesByName.set(propName, prop);
appendType(prop.type);
if (!prop.required) {
propTypes.add('undefined');
}
}
if (schema.additionalProperties === true) {
this.additionalPropertiesType = 'any';
}
else if (schema.additionalProperties) {
const propType = (0, gen_utils_1.tsType)(schema.additionalProperties, this.options, this.openApi);
appendType(propType);
this.additionalPropertiesType = [...propTypes].sort().join(' | ');
}
}
if (schema.allOf) {
schema.allOf.forEach(s => this.collectObject(s, propertiesByName));
}
}
}
exports.Model = Model;
//# sourceMappingURL=model.js.map