Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Kraft committed Mar 9, 2024
1 parent 11f6ac5 commit 5ab17a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
54 changes: 35 additions & 19 deletions lib/services/schema-object-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,24 @@ export class SchemaObjectFactory {

const schemaCombinators = ['oneOf', 'anyOf', 'allOf'];
let keyOfCombinators = '';
if (schemaCombinators.some((_key) => { keyOfCombinators = _key; return _key in property; })) {
if (((property as SchemaObjectMetadata)?.type === 'array' || (property as SchemaObjectMetadata).isArray) && keyOfCombinators) {
(property as SchemaObjectMetadata).items = {};
(property as SchemaObjectMetadata).items[keyOfCombinators] = property[keyOfCombinators];
delete property[keyOfCombinators];
} else {
delete (property as SchemaObjectMetadata).type;
}
if (
schemaCombinators.some((_key) => {
keyOfCombinators = _key;
return _key in property;
})
) {
if (
((property as SchemaObjectMetadata)?.type === 'array' ||
(property as SchemaObjectMetadata).isArray) &&
keyOfCombinators
) {
(property as SchemaObjectMetadata).items = {};
(property as SchemaObjectMetadata).items[keyOfCombinators] =
property[keyOfCombinators];
delete property[keyOfCombinators];
} else {
delete (property as SchemaObjectMetadata).type;
}
}
return property as ParameterObject;
});
Expand Down Expand Up @@ -203,7 +213,8 @@ export class SchemaObjectFactory {
if (!propertiesWithType) {
return '';
}
const extensionProperties = Reflect.getMetadata(DECORATORS.API_EXTENSION, type) || {};
const extensionProperties =
Reflect.getMetadata(DECORATORS.API_EXTENSION, type) || {};
const typeDefinition: SchemaObject = {
type: 'object',
properties: mapValues(keyBy(propertiesWithType, 'name'), (property) =>
Expand Down Expand Up @@ -273,7 +284,10 @@ export class SchemaObjectFactory {
: undefined;

schemas[enumName] = {
type: (param.isArray ? param.schema?.['items']?.['type'] : param.schema?.['type']) ?? 'string',
type:
(param.isArray
? param.schema?.['items']?.['type']
: param.schema?.['type']) ?? 'string',
enum: _enum
};
}
Expand Down Expand Up @@ -301,15 +315,17 @@ export class SchemaObjectFactory {
const $ref = getSchemaPath(enumName);

// Allow given fields to be part of the referenced enum schema
const additionalParams = ['description', 'deprecated', 'default']
const additionalFields = additionalParams.reduce((acc, param) =>
({...acc, ...(metadata[param] && { [param]: metadata[param] })}), {});

const enumType: string = (
metadata.isArray
? metadata.items['type']
: metadata.type
) ?? 'string';
const additionalParams = ['description', 'deprecated', 'default'];
const additionalFields = additionalParams.reduce(
(acc, param) => ({
...acc,
...(metadata[param] && { [param]: metadata[param] })
}),
{}
);

const enumType: string =
(metadata.isArray ? metadata.items['type'] : metadata.type) ?? 'string';

schemas[enumName] = {
type: enumType,
Expand Down
23 changes: 13 additions & 10 deletions test/services/schema-object-factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ApiExtension, ApiProperty } from '../../lib/decorators';
import { BaseParameterObject, SchemasObject } from '../../lib/interfaces/open-api-spec.interface';
import {
BaseParameterObject,
SchemasObject
} from '../../lib/interfaces/open-api-spec.interface';
import { ModelPropertiesAccessor } from '../../lib/services/model-properties-accessor';
import { SchemaObjectFactory } from '../../lib/services/schema-object-factory';
import { SwaggerTypesMapper } from '../../lib/services/swagger-types-mapper';
Expand Down Expand Up @@ -337,15 +340,15 @@ describe('SchemaObjectFactory', () => {
isArray: false,
enumName: 'MyEnum',
enum: ['a', 'b', 'c']
}
};
const schemas = {};
schemaObjectFactory.createEnumParam(params, schemas)
schemaObjectFactory.createEnumParam(params, schemas);

expect(schemas['MyEnum']).toEqual({
enum: ['a', 'b', 'c'],
type: 'string'
})
})
});
});

it('should create an enum schema definition for an array', () => {
const params: ParamWithTypeMetadata & BaseParameterObject = {
Expand All @@ -359,14 +362,14 @@ describe('SchemaObjectFactory', () => {
enum: ['a', 'b', 'c']
}
}
}
};
const schemas = {};
schemaObjectFactory.createEnumParam(params, schemas)
schemaObjectFactory.createEnumParam(params, schemas);

expect(schemas['MyEnum']).toEqual({
enum: ['a', 'b', 'c'],
type: 'string'
})
})
})
});
});
});
});

0 comments on commit 5ab17a9

Please sign in to comment.