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

fix: oneof implementation #814

Closed
wants to merge 2 commits into from
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
3 changes: 3 additions & 0 deletions src/generators/typescript/TypeScriptRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export abstract class TypeScriptRenderer extends AbstractRenderer<TypeScriptOpti
if (Array.isArray(model.type)) {
return [... new Set(model.type.map(t => this.toTsType(t, model)))].join(' | ');
}
if (Array.isArray(model.items) && model.originalInput.oneOf !== undefined) {
return model.items.map(t => this.renderType(t)).join(' | ');
}
return this.toTsType(model.type, model);
}

Expand Down
21 changes: 21 additions & 0 deletions src/interpreter/InterpretOneOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CommonModel } from '../models/CommonModel';
import { Interpreter, InterpreterOptions, InterpreterSchemaType } from './Interpreter';

/**
* Interpreter function for oneOf keyword.
*
* It puts the schema reference into the items field.
*
* @param schema
* @param model
* @param interpreter
* @param interpreterOptions to control the interpret process
*/
export default function interpretOneOf(schema: InterpreterSchemaType, model: CommonModel, interpreter : Interpreter, interpreterOptions: InterpreterOptions = Interpreter.defaultInterpreterOptions): void {
if (typeof schema === 'boolean' || schema.oneOf === undefined) {return;}
for (const [index, oneOfSchema] of schema.oneOf.entries()) {
const oneOfModel = interpreter.interpret(oneOfSchema, interpreterOptions);
if (oneOfModel === undefined) {continue;}
model.addItemTuple(oneOfModel, schema, index);
}
}
3 changes: 2 additions & 1 deletion src/interpreter/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CommonModel, Draft6Schema, Draft4Schema, SwaggerV2Schema, AsyncapiV2Sch
import { interpretName, isEnum, isModelObject } from './Utils';
import interpretProperties from './InterpretProperties';
import interpretAllOf from './InterpretAllOf';
import interpretOneOf from './InterpretOneOf';
import interpretConst from './InterpretConst';
import interpretEnum from './InterpretEnum';
import interpretAdditionalProperties from './InterpretAdditionalProperties';
Expand Down Expand Up @@ -78,11 +79,11 @@ export class Interpreter {
interpretItems(schema, model, this, interpreterOptions);
interpretProperties(schema, model, this, interpreterOptions);
interpretAllOf(schema, model, this, interpreterOptions);
interpretOneOf(schema, model, this, interpreterOptions);
interpretDependencies(schema, model, this, interpreterOptions);
interpretConst(schema, model);
interpretEnum(schema, model);

this.interpretAndCombineMultipleSchemas(schema.oneOf, model, schema, interpreterOptions);
this.interpretAndCombineMultipleSchemas(schema.anyOf, model, schema, interpreterOptions);
if (!(schema instanceof Draft4Schema) && !(schema instanceof Draft6Schema)) {
this.interpretAndCombineSchema(schema.then, model, schema, interpreterOptions);
Expand Down
2 changes: 1 addition & 1 deletion test/generators/go/GoGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Address struct {
State string
HouseNumber float64
Marriage bool
Members []interface{}
Members interface{}
TupleType []interface{}
ArrayType []string
AdditionalProperties map[string]string
Expand Down
4 changes: 2 additions & 2 deletions test/generators/go/__snapshots__/GoGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Address struct {
State string
HouseNumber float64
Marriage bool
Members []interface{}
Members interface{}
ArrayType []interface{}
OtherModel *OtherModel
AdditionalProperties map[string][]interface{}
Expand Down Expand Up @@ -45,7 +45,7 @@ type Address struct {
State string
HouseNumber float64
Marriage bool
Members []interface{}
Members interface{}
ArrayType []interface{}
OtherModel *OtherModel
AdditionalProperties map[string][]interface{}
Expand Down