Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ public TypeScriptClientCodegen() {
// models
setModelPackage("models");
supportingFiles.add(new SupportingFile("model" + File.separator + "ObjectSerializer.mustache", "models", "ObjectSerializer.ts"));
supportingFiles.add(new SupportingFile("model" + File.separator + "OneOfClass.mustache", "models", "OneOfClass.ts"));

modelTemplateFiles.put("model" + File.separator + "model.mustache", ".ts");

// api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,15 @@ export class ObjectSerializer {
// Check the discriminator
let discriminatorProperty = typeMap[expectedType].discriminator;
if (discriminatorProperty == null) {
return expectedType; // the type does not have a discriminator. use it.
if (this.hasFindMatchingTypeMethod(typeMap[expectedType])) {
const foundType = typeMap[expectedType].findMatchingType(data);
if (foundType == undefined) {
throw new Error("Unable to determine a unique type for the provided object: oneOf type resolution failed. The object does not match exactly one schema. Consider adding a discriminator or making schemas mutually exclusive.");
}

return foundType;
}
return expectedType; // the type does not have a discriminator and findMatchingType method. use it.
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
Expand All @@ -147,6 +155,13 @@ export class ObjectSerializer {
}
}

public static hasFindMatchingTypeMethod(klass: any): boolean {
if (typeof klass.findMatchingType === 'function') {
return true;
}
return false;
}

public static serialize(data: any, type: string, format: string): any {
if (data == undefined) {
return data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class OneOfClass {
public static instanceOf(data: any, attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string, required: boolean}>): boolean {
for(const attribute of attributeTypeMap) {
if (attribute.required) {
if (!(attribute.baseName in data) || data[attribute.baseName] === undefined) {
return false;
}
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
{{#tsImports}}
import { {{classname}} } from '{{filename}}{{importFileExtension}}';
{{/tsImports}}
{{#oneOf}}
{{#-first}}
import { OneOfClass } from '../models/OneOfClass{{importFileExtension}}';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this import does not seem to be used here, or is it?

{{/-first}}
{{/oneOf}}
{{^oneOf}}
import { HttpFile } from '../http/http{{importFileExtension}}';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure this is not used in the oneOf case?

{{/oneOf}}

{{#description}}
/**
Expand Down Expand Up @@ -46,13 +53,14 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
{{/hasDiscriminatorWithNonEmptyMapping}}

{{^isArray}}
static {{#parent}}override {{/parent}}readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
static {{#parent}}override {{/parent}}readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string, required: boolean}> = [
{{#vars}}
{
"name": "{{name}}",
"baseName": "{{baseName}}",
"type": "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}",
"format": "{{dataFormat}}"
"format": "{{dataFormat}}",
"required": {{required}}
}{{^-last}},
{{/-last}}
{{/vars}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
{{#hasImports}}
import {
{{#imports}}
{{{.}}}{{importFileExtension}},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this not needed anymore?

{{/imports}}
} from './';

{{/hasImports}}
/**
* @type {{classname}}
* Type
Expand All @@ -18,7 +10,7 @@ export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}};
* {{{.}}}{{/description}}
* @export
*/
export class {{classname}}Class {
export class {{classname}}Class extends OneOfClass {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OneOfClass is not imported in in this file

{{#discriminator}}
static readonly discriminator: string | undefined = "{{discriminatorName}}";
{{/discriminator}}
Expand All @@ -37,4 +29,16 @@ export class {{classname}}Class {

static readonly mapping: {[index: string]: string} | undefined = undefined;
{{/hasDiscriminatorWithNonEmptyMapping}}

static readonly arrayOfTypes: Array<{{#oneOf}}typeof {{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}> = [{{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}];

public static findMatchingType(data:any): string | undefined {
for(const type of this.arrayOfTypes) {
if (this.instanceOf(data, type.getAttributeTypeMap())) {
return type.name;
}
}

return undefined;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
openapi: 3.0.0
info:
version: 1.0.0
title: testing oneOf
servers:
- url: http://localhost:3000
paths:
/test:
get:
operationId: testWithoutDiscriminator
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/PetResponse'
/test-discriminator:
get:
operationId: testDiscriminator
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/PetDiscriminatorResponse'
components:
schemas:
PetDiscriminatorResponse:
discriminator:
propertyName: petType
mapping:
cat: "#/components/schemas/Cat"
dog: "#/components/schemas/Dog"
oneOf:
- $ref: "#/components/schemas/Cat"
- $ref: "#/components/schemas/Dog"
PetResponse:
oneOf:
- $ref: "#/components/schemas/Cat"
- $ref: "#/components/schemas/Dog"
Cat:
type: object
properties:
name:
type: string
petType:
type: string
required:
- name
- petType
Dog:
type: object
properties:
bark:
type: string
petType:
type: string
required:
- bark
- petType

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions samples/client/echo_api/typescript/build/models/Bird.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions samples/client/echo_api/typescript/build/models/Category.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions samples/client/echo_api/typescript/build/models/DataQuery.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 17 additions & 9 deletions samples/client/echo_api/typescript/build/models/DefaultValue.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading