Skip to content

Commit 5ab17a9

Browse files
author
Artur Kraft
committedMar 9, 2024
prettier
1 parent 11f6ac5 commit 5ab17a9

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed
 

‎lib/services/schema-object-factory.ts

+35-19
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,24 @@ export class SchemaObjectFactory {
167167

168168
const schemaCombinators = ['oneOf', 'anyOf', 'allOf'];
169169
let keyOfCombinators = '';
170-
if (schemaCombinators.some((_key) => { keyOfCombinators = _key; return _key in property; })) {
171-
if (((property as SchemaObjectMetadata)?.type === 'array' || (property as SchemaObjectMetadata).isArray) && keyOfCombinators) {
172-
(property as SchemaObjectMetadata).items = {};
173-
(property as SchemaObjectMetadata).items[keyOfCombinators] = property[keyOfCombinators];
174-
delete property[keyOfCombinators];
175-
} else {
176-
delete (property as SchemaObjectMetadata).type;
177-
}
170+
if (
171+
schemaCombinators.some((_key) => {
172+
keyOfCombinators = _key;
173+
return _key in property;
174+
})
175+
) {
176+
if (
177+
((property as SchemaObjectMetadata)?.type === 'array' ||
178+
(property as SchemaObjectMetadata).isArray) &&
179+
keyOfCombinators
180+
) {
181+
(property as SchemaObjectMetadata).items = {};
182+
(property as SchemaObjectMetadata).items[keyOfCombinators] =
183+
property[keyOfCombinators];
184+
delete property[keyOfCombinators];
185+
} else {
186+
delete (property as SchemaObjectMetadata).type;
187+
}
178188
}
179189
return property as ParameterObject;
180190
});
@@ -203,7 +213,8 @@ export class SchemaObjectFactory {
203213
if (!propertiesWithType) {
204214
return '';
205215
}
206-
const extensionProperties = Reflect.getMetadata(DECORATORS.API_EXTENSION, type) || {};
216+
const extensionProperties =
217+
Reflect.getMetadata(DECORATORS.API_EXTENSION, type) || {};
207218
const typeDefinition: SchemaObject = {
208219
type: 'object',
209220
properties: mapValues(keyBy(propertiesWithType, 'name'), (property) =>
@@ -273,7 +284,10 @@ export class SchemaObjectFactory {
273284
: undefined;
274285

275286
schemas[enumName] = {
276-
type: (param.isArray ? param.schema?.['items']?.['type'] : param.schema?.['type']) ?? 'string',
287+
type:
288+
(param.isArray
289+
? param.schema?.['items']?.['type']
290+
: param.schema?.['type']) ?? 'string',
277291
enum: _enum
278292
};
279293
}
@@ -301,15 +315,17 @@ export class SchemaObjectFactory {
301315
const $ref = getSchemaPath(enumName);
302316

303317
// Allow given fields to be part of the referenced enum schema
304-
const additionalParams = ['description', 'deprecated', 'default']
305-
const additionalFields = additionalParams.reduce((acc, param) =>
306-
({...acc, ...(metadata[param] && { [param]: metadata[param] })}), {});
307-
308-
const enumType: string = (
309-
metadata.isArray
310-
? metadata.items['type']
311-
: metadata.type
312-
) ?? 'string';
318+
const additionalParams = ['description', 'deprecated', 'default'];
319+
const additionalFields = additionalParams.reduce(
320+
(acc, param) => ({
321+
...acc,
322+
...(metadata[param] && { [param]: metadata[param] })
323+
}),
324+
{}
325+
);
326+
327+
const enumType: string =
328+
(metadata.isArray ? metadata.items['type'] : metadata.type) ?? 'string';
313329

314330
schemas[enumName] = {
315331
type: enumType,

‎test/services/schema-object-factory.spec.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { ApiExtension, ApiProperty } from '../../lib/decorators';
2-
import { BaseParameterObject, SchemasObject } from '../../lib/interfaces/open-api-spec.interface';
2+
import {
3+
BaseParameterObject,
4+
SchemasObject
5+
} from '../../lib/interfaces/open-api-spec.interface';
36
import { ModelPropertiesAccessor } from '../../lib/services/model-properties-accessor';
47
import { SchemaObjectFactory } from '../../lib/services/schema-object-factory';
58
import { SwaggerTypesMapper } from '../../lib/services/swagger-types-mapper';
@@ -337,15 +340,15 @@ describe('SchemaObjectFactory', () => {
337340
isArray: false,
338341
enumName: 'MyEnum',
339342
enum: ['a', 'b', 'c']
340-
}
343+
};
341344
const schemas = {};
342-
schemaObjectFactory.createEnumParam(params, schemas)
345+
schemaObjectFactory.createEnumParam(params, schemas);
343346

344347
expect(schemas['MyEnum']).toEqual({
345348
enum: ['a', 'b', 'c'],
346349
type: 'string'
347-
})
348-
})
350+
});
351+
});
349352

350353
it('should create an enum schema definition for an array', () => {
351354
const params: ParamWithTypeMetadata & BaseParameterObject = {
@@ -359,14 +362,14 @@ describe('SchemaObjectFactory', () => {
359362
enum: ['a', 'b', 'c']
360363
}
361364
}
362-
}
365+
};
363366
const schemas = {};
364-
schemaObjectFactory.createEnumParam(params, schemas)
367+
schemaObjectFactory.createEnumParam(params, schemas);
365368

366369
expect(schemas['MyEnum']).toEqual({
367370
enum: ['a', 'b', 'c'],
368371
type: 'string'
369-
})
370-
})
371-
})
372+
});
373+
});
374+
});
372375
});

0 commit comments

Comments
 (0)
Please sign in to comment.