Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add discriminator support for OpenAPI v3 and Swagger v2 #1281

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 22 additions & 2 deletions src/interpreter/InterpretAllOf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Logger } from '../utils';
import { CommonModel, AsyncapiV2Schema } from '../models';
import {
CommonModel,
AsyncapiV2Schema,
SwaggerV2Schema,
OpenapiV3Schema
} from '../models';
import {
Interpreter,
InterpreterOptions,
Expand Down Expand Up @@ -33,13 +38,28 @@ export default function interpretAllOf(
}

for (const allOfSchema of schema.allOf) {
if (allOfSchema instanceof AsyncapiV2Schema && allOfSchema.discriminator) {
if (
(allOfSchema instanceof AsyncapiV2Schema ||
allOfSchema instanceof SwaggerV2Schema) &&
allOfSchema.discriminator
) {
interpreterOptions = {
...interpreterOptions,
discriminator: allOfSchema.discriminator
};

model.discriminator = allOfSchema.discriminator;
} else if (
allOfSchema instanceof OpenapiV3Schema &&
allOfSchema.discriminator &&
allOfSchema.discriminator.propertyName
) {
interpreterOptions = {
...interpreterOptions,
discriminator: allOfSchema.discriminator.propertyName
};

model.discriminator = allOfSchema.discriminator.propertyName;
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/interpreter/InterpretConst.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Draft4Schema, CommonModel, AsyncapiV2Schema } from '../models';
import {
Draft4Schema,
CommonModel,
AsyncapiV2Schema,
SwaggerV2Schema,
OpenapiV3Schema
} from '../models';
import {
Interpreter,
InterpreterOptions,
Expand Down Expand Up @@ -26,7 +32,12 @@ export default function interpretConst(
return;
}

if (schema instanceof AsyncapiV2Schema && interpreterOptions.discriminator) {
if (
(schema instanceof AsyncapiV2Schema ||
schema instanceof SwaggerV2Schema ||
schema instanceof OpenapiV3Schema) &&
interpreterOptions.discriminator
) {
model.enum = [schema.const];
}

Expand Down
23 changes: 21 additions & 2 deletions src/interpreter/InterpretOneOf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { AsyncapiV2Schema, CommonModel } from '../models';
import {
AsyncapiV2Schema,
CommonModel,
OpenapiV3Schema,
SwaggerV2Schema
} from '../models';
import {
Interpreter,
InterpreterOptions,
Expand Down Expand Up @@ -30,13 +35,27 @@ export default function interpretOneOf(
return;
}

if (schema instanceof AsyncapiV2Schema && schema.discriminator) {
if (
(schema instanceof AsyncapiV2Schema || schema instanceof SwaggerV2Schema) &&
schema.discriminator
) {
interpreterOptions = {
...interpreterOptions,
discriminator: schema.discriminator
};

model.discriminator = schema.discriminator;
} else if (
schema instanceof OpenapiV3Schema &&
schema.discriminator &&
schema.discriminator.propertyName
) {
interpreterOptions = {
...interpreterOptions,
discriminator: schema.discriminator.propertyName
};

model.discriminator = schema.discriminator.propertyName;
}

for (const oneOfSchema of schema.oneOf) {
Expand Down
24 changes: 22 additions & 2 deletions src/interpreter/InterpretOneOfWithAllOf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CommonModel, AsyncapiV2Schema } from '../models';
import {
CommonModel,
AsyncapiV2Schema,
OpenapiV3Schema,
SwaggerV2Schema
} from '../models';
import {
Interpreter,
InterpreterOptions,
Expand Down Expand Up @@ -32,13 +37,28 @@ export default function interpretOneOfWithAllOf(
}

for (const allOfSchema of schema.allOf) {
if (allOfSchema instanceof AsyncapiV2Schema && allOfSchema.discriminator) {
if (
(allOfSchema instanceof AsyncapiV2Schema ||
allOfSchema instanceof SwaggerV2Schema) &&
allOfSchema.discriminator
) {
interpreterOptions = {
...interpreterOptions,
discriminator: allOfSchema.discriminator
};

model.discriminator = allOfSchema.discriminator;
} else if (
allOfSchema instanceof OpenapiV3Schema &&
allOfSchema.discriminator &&
allOfSchema.discriminator.propertyName
) {
interpreterOptions = {
...interpreterOptions,
discriminator: allOfSchema.discriminator.propertyName
};

model.discriminator = allOfSchema.discriminator.propertyName;
}
}

Expand Down
23 changes: 21 additions & 2 deletions src/interpreter/InterpretOneOfWithProperties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CommonModel, AsyncapiV2Schema } from '../models';
import {
CommonModel,
AsyncapiV2Schema,
OpenapiV3Schema,
SwaggerV2Schema
} from '../models';
import {
Interpreter,
InterpreterOptions,
Expand Down Expand Up @@ -30,13 +35,27 @@ export default function interpretOneOfWithProperties(
return;
}

if (schema instanceof AsyncapiV2Schema && schema.discriminator) {
if (
(schema instanceof AsyncapiV2Schema || schema instanceof SwaggerV2Schema) &&
schema.discriminator
) {
interpreterOptions = {
...interpreterOptions,
discriminator: schema.discriminator
};

model.discriminator = schema.discriminator;
} else if (
schema instanceof OpenapiV3Schema &&
schema.discriminator &&
schema.discriminator.propertyName
) {
interpreterOptions = {
...interpreterOptions,
discriminator: schema.discriminator.propertyName
};

model.discriminator = schema.discriminator.propertyName;
}

for (const oneOfSchema of schema.oneOf) {
Expand Down
6 changes: 4 additions & 2 deletions src/interpreter/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
AsyncapiV2Schema,
Draft7Schema,
MergingOptions,
defaultMergingOptions
defaultMergingOptions,
OpenapiV3Schema
} from '../models';
import { interpretName } from './Utils';
import interpretProperties from './InterpretProperties';
Expand Down Expand Up @@ -46,7 +47,7 @@ export type InterpreterOptions = {
*/
ignoreAdditionalItems?: boolean;
/**
* When interpreting a schema with discriminator set, this property will be set bet by the individual interpreters to make sure the discriminator becomes an enum.
* When interpreting a schema with discriminator set, this property will be set best by the individual interpreters to make sure the discriminator becomes an enum.
*/
discriminator?: string;
};
Expand All @@ -55,6 +56,7 @@ export type InterpreterSchemas =
| Draft4Schema
| Draft7Schema
| SwaggerV2Schema
| OpenapiV3Schema
| AsyncapiV2Schema;
export type InterpreterSchemaType = InterpreterSchemas | boolean;

Expand Down
1 change: 1 addition & 0 deletions src/processors/JsonSchemaInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
| Draft6Schema
| Draft7Schema
| SwaggerV2Schema
| OpenapiV3Schema
| AsyncapiV2Schema
| boolean,
options?: ProcessorOptions
Expand Down
8 changes: 4 additions & 4 deletions src/processors/OpenAPIInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ export class OpenAPIInputProcessor extends AbstractInputProcessor {
schema: OpenAPIV3.SchemaObject,
name: string
): OpenapiV3Schema {
let internalSchema = OpenapiV3Schema.toSchema(schema as any);
internalSchema = JsonSchemaInputProcessor.reflectSchemaNames(
internalSchema,
const namedSchema = JsonSchemaInputProcessor.reflectSchemaNames(
schema,
{},
name,
true
);
return internalSchema;

return OpenapiV3Schema.toSchema(namedSchema);
}

/**
Expand Down
108 changes: 107 additions & 1 deletion test/generators/java/presets/JacksonPreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('JAVA_JACKSON_PRESET', () => {
});

describe('union', () => {
test('handle oneOf with discriminator with Jackson', async () => {
test('handle oneOf with AsyncAPI discriminator with Jackson', async () => {
const asyncapiDoc = {
asyncapi: '2.6.0',
info: {
Expand Down Expand Up @@ -91,6 +91,112 @@ describe('JAVA_JACKSON_PRESET', () => {
expect(models.map((model) => model.result)).toMatchSnapshot();
});

test('handle oneOf with Swagger v2 discriminator with Jackson', async () => {
const openapiDoc = {
swagger: '2.0',
info: {
title: 'Vehicle',
version: '1.0.0'
},
paths: {
'/vehicles': {
get: {
responses: {
200: {
description: 'successful operation',
schema: {
type: 'object',
title: 'Vehicle',
discriminator: 'vehicleType',
oneOf: [
{ $ref: '#/components/schemas/Car' },
{ $ref: '#/components/schemas/Truck' }
]
}
}
}
}
}
},
components: {
schemas: {
Car: {
title: 'Car',
type: 'object',
properties: {
vehicleType: { type: 'string' }
}
},
Truck: {
title: 'Truck',
type: 'object',
properties: {
vehicleType: { type: 'string' }
}
}
}
}
};

const models = await generator.generate(openapiDoc);
expect(models.map((model) => model.result)).toMatchSnapshot();
});

test('handle oneOf with OpenAPI v3 discriminator with Jackson', async () => {
const openapiDoc = {
openapi: '3.0.3',
info: {
title: 'Vehicle',
version: '1.0.0'
},
paths: {
'/vehicles': {
get: {
responses: {
200: {
description: 'successful operation',
content: {
'application/json': {
schema: {
type: 'object',
title: 'Vehicle',
discriminator: { propertyName: 'vehicleType' },
oneOf: [
{ $ref: '#/components/schemas/Car' },
{ $ref: '#/components/schemas/Truck' }
]
}
}
}
}
}
}
}
},
components: {
schemas: {
Car: {
title: 'Car',
type: 'object',
properties: {
vehicleType: { type: 'string' }
}
},
Truck: {
title: 'Truck',
type: 'object',
properties: {
vehicleType: { type: 'string' }
}
}
}
}
};

const models = await generator.generate(openapiDoc);
expect(models.map((model) => model.result)).toMatchSnapshot();
});

test('handle oneOf without discriminator with Jackson deduction', async () => {
const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
Expand Down
Loading