Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/services/__tests__/models/Schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,58 @@ describe('Models', () => {
`);
});
describe('enum values', () => {
function createSchemaModel(schema: Record<string, unknown>) {
const parser = new OpenAPIParser(
{
openapi: '3.0.0',
info: { title: 'Enum extension test', version: '1.0.0' },
paths: {},
} as any,
undefined,
opts,
);
return new SchemaModel(parser, schema as any, '#/components/schemas/Test', opts);
}

test('uses x-extensible-enum values as display enums', () => {
const model = createSchemaModel({
type: 'string',
'x-extensible-enum': ['open', 'closed'],
});

expect(model.enum).toEqual(['open', 'closed']);
expect(printSchema(model, enumDetailsPrinter)).toContain('enum: [open,closed]');
});

test('uses other x-*-enum extension values', () => {
const model = createSchemaModel({
type: 'string',
'x-acme-status-enum': ['ready', 'closed'],
});

expect(model.enum).toEqual(['ready', 'closed']);
expect(printSchema(model, enumDetailsPrinter)).toContain('enum: [ready,closed]');
});

test('uses enum extensions declared on array items', () => {
const model = createSchemaModel({
type: 'array',
items: { type: 'string', 'x-extensible-enum': ['fast', 'slow'] },
});

expect(model.enum).toEqual(['fast', 'slow']);
expect(printSchema(model, enumDetailsPrinter)).toContain('enum: [fast,slow]');
});

test('prefers the standard enum when both forms are present', () => {
const model = createSchemaModel({
type: 'string',
enum: ['stable'],
'x-extensible-enum': ['preview'],
});

expect(model.enum).toEqual(['stable']);
});
test('should get correct fields enum fields without duplication', () => {
const spec = parseYaml(outdent`
openapi: 3.0.0
Expand Down
16 changes: 15 additions & 1 deletion src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class SchemaModel {
this.description = schema.description || '';
this.type = schema.type || detectType(schema);
this.format = schema.format;
this.enum = schema.enum || [];
this.enum = getSchemaEnum(schema);
this['x-enumDescriptions'] = schema['x-enumDescriptions'];
this.example = schema.example;
this.examples = schema.examples;
Expand Down Expand Up @@ -599,6 +599,20 @@ function buildAdditionalItems({
return [];
}

function getSchemaEnum(schema: OpenAPISchema): any[] {
if (schema.enum !== undefined) {
return schema.enum;
}

const enumExtensions = Object.entries(schema).filter(
([name, value]) => name.startsWith('x-') && name.endsWith('-enum') && Array.isArray(value),
);
const extensibleEnum = enumExtensions.find(([name]) => name === 'x-extensible-enum')?.[1];
const values = extensibleEnum ?? enumExtensions[0]?.[1];

return Array.isArray(values) ? values : [];
}

function getDiscriminator(schema: OpenAPISchema): OpenAPISchema['discriminator'] {
return schema.discriminator || schema['x-discriminator'];
}
Expand Down