Skip to content

Commit 2827fac

Browse files
Fix enums "hardcoded" in components
1 parent ba93335 commit 2827fac

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/services/parsers/open-api-v3/components-parser.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export class ComponentsParserService extends ParserBaseService {
2727
if (rawModel.type === 'object') {
2828
const modelInstance = new ModelModel(modelName);
2929
elementRef = modelInstance;
30-
modelInstance.addAttributes(this.parseAttributes(rawModel));
30+
modelInstance.addAttributes(
31+
this.parseAttributes(rawModel, modelInstance.name),
32+
);
3133
this.modelStore.add(modelInstance);
3234
} else if (this.isEnumObject(rawModel)) {
3335
console.debug(`${modelName} is ENUM of type ${rawModel.type}`);

src/services/parsers/open-api-v3/parser-base.service.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { capital } from 'case';
22
import { OpenAPIV3 } from 'openapi-types';
3+
import { EnumModel } from '../../../models/enum.model';
34
import { ModelAttributessModel } from '../../../models/model-attributes.model';
45
import { ModelModel } from '../../../models/model.model';
56
import { StoreI } from '../../../stores/entities.store';
@@ -78,13 +79,13 @@ export abstract class ParserBaseService {
7879
* Get an Schema and get the REF
7980
* If is a custom model (scheme), it will create a new model
8081
* @param schema
81-
* @param defaultName Name for the model if is not a "ref"
82+
* @param modelName Name for the model if is not a "ref"
8283
*/
8384
protected parseParameters(
8485
parameters: (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[],
85-
defaultName: string,
86+
modelName: string,
8687
): ModelAttributessModel {
87-
const newModel = new ModelModel(defaultName);
88+
const newModel = new ModelModel(modelName);
8889
for (const rawParameter of parameters) {
8990
if (this.isRefObject(rawParameter)) {
9091
throw 'Not implemented REF OBJECT for parameterParser';
@@ -95,7 +96,7 @@ export abstract class ParserBaseService {
9596
const parameter = new ModelAttributessModel(rawParameter.name);
9697
parameter.typeURI = this.parseSchema(
9798
rawParameter.schema,
98-
capital(`${defaultName} ${rawParameter.name}`, '', true),
99+
capital(`${modelName} ${rawParameter.name}`, '', true),
99100
)?.typeURI;
100101
parameter.description = rawParameter.description;
101102
parameter.deprecated = rawParameter.deprecated;
@@ -120,7 +121,7 @@ export abstract class ParserBaseService {
120121
defaultName: string,
121122
mediaType: string = null,
122123
): ModelAttributessModel {
123-
if (!schema) {
124+
if (!schema) {
124125
console.warn('WARNING: No schema defined! Any will be use instead');
125126
console.warn('TIP: Don\'t fill "content" for responses if void');
126127
const instance = new ModelAttributessModel(null);
@@ -138,14 +139,15 @@ export abstract class ParserBaseService {
138139
return instance;
139140
}
140141

141-
if (mediaType === 'text/html' || schema?.type !== 'object') {
142+
if (mediaType === 'text/html' || schema?.type !== 'object') {
142143
const instance = new ModelAttributessModel(null);
143144
instance.typeURI = schema.type;
144145
return instance;
145146
}
146147

147148
const newModel = new ModelModel(defaultName);
148-
newModel.addAttributes(this.parseAttributes(schema));
149+
newModel.addAttributes(this.parseAttributes(schema, defaultName));
150+
149151
this.store.models.add(newModel);
150152

151153
const instance = new ModelAttributessModel(null);
@@ -158,6 +160,7 @@ export abstract class ParserBaseService {
158160

159161
protected parseAttributes(
160162
rawModel: OpenAPIV3.NonArraySchemaObject,
163+
parentName: string,
161164
): ModelAttributessModel[] {
162165
const attributes: ModelAttributessModel[] = [];
163166
console.group('Parsing attributes');
@@ -168,7 +171,11 @@ export abstract class ParserBaseService {
168171
attrName,
169172
rawModel.required,
170173
);
171-
this.fillAttribute(attribute, rawAttribute);
174+
this.fillAttribute(
175+
attribute,
176+
rawAttribute,
177+
capital(`${parentName} ${attrName}`, '', true),
178+
);
172179
attributes.push(attribute);
173180
}
174181
console.groupEnd();
@@ -182,7 +189,11 @@ export abstract class ParserBaseService {
182189
return requiredList?.indexOf(attrName) > -1;
183190
}
184191

185-
protected fillAttribute(attribute: ModelAttributessModel, rawAttribute) {
192+
protected fillAttribute(
193+
attribute: ModelAttributessModel,
194+
rawAttribute,
195+
defaultName: string,
196+
) {
186197
const attrName = attribute.name;
187198
if (this.isRefObject(rawAttribute)) {
188199
console.debug(`${attrName} is ref of ${rawAttribute.$ref}`);
@@ -198,8 +209,16 @@ export abstract class ParserBaseService {
198209
if (rawAttribute.type === 'array') {
199210
console.group(`${attrName} is an array`);
200211
attribute.isArray = true;
201-
this.fillAttribute(attribute, rawAttribute.items);
212+
this.fillAttribute(attribute, rawAttribute.items, defaultName);
202213
console.groupEnd();
214+
} else if (rawAttribute.enum) {
215+
// (this.isEnumObject(schema)) -> error TS2339:, so "pure if" is used instead
216+
console.debug(`${attrName} is ENUM of type ${rawAttribute.type}`);
217+
const newModel = new EnumModel(`${defaultName}ENUM`);
218+
newModel.type = rawAttribute.type;
219+
newModel.values = rawAttribute.enum;
220+
this.store.models.add(newModel);
221+
attribute.typeURI = newModel.uri;
203222
} else {
204223
console.debug(`${attrName} of type ${rawAttribute.type}`);
205224
attribute.typeURI = rawAttribute.type;

0 commit comments

Comments
 (0)