[TypeScript] Generate oneOf schemas as type unions (#2647)

* [TypeScript] Generate oneOf schemas as type unions

* [TypeScript] Generate oneOf schemas as type unions

* [TypeScript] Generate oneOf schemas as type unions

update aurelia sample
This commit is contained in:
Vincent Devos 2019-04-30 18:46:05 +02:00 committed by William Cheng
parent d3f418166b
commit a5235f25b4
16 changed files with 113 additions and 77 deletions

View File

@ -166,9 +166,9 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
} }
} }
} }
if (cm.oneOf.size() > 0) { if (!cm.oneOf.isEmpty()) {
// For oneOfs only import $refs within the oneOf // For oneOfs only import $refs within the oneOf
TreeSet<String> oneOfRefs = new TreeSet<String>(); TreeSet<String> oneOfRefs = new TreeSet<>();
for (String im : cm.imports) { for (String im : cm.imports) {
if (cm.oneOf.contains(im)) { if (cm.oneOf.contains(im)) {
oneOfRefs.add(im); oneOfRefs.add(im);

View File

@ -11,6 +11,6 @@ import { {{classname}} } from './{{filename}}';
* {{{description}}} * {{{description}}}
*/ */
{{/description}} {{/description}}
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#isAlias}}{{>modelAlias}}{{/isAlias}}{{^isAlias}}{{#taggedUnions}}{{>modelTaggedUnion}}{{/taggedUnions}}{{^taggedUnions}}{{>modelGeneric}}{{/taggedUnions}}{{/isAlias}}{{/isEnum}} {{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#isAlias}}{{>modelAlias}}{{/isAlias}}{{^isAlias}}{{#taggedUnions}}{{>modelTaggedUnion}}{{/taggedUnions}}{{^taggedUnions}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/taggedUnions}}{{/isAlias}}{{/isEnum}}
{{/model}} {{/model}}
{{/models}} {{/models}}

View File

@ -0,0 +1,14 @@
{{#hasImports}}
import {
{{#imports}}
{{{.}}},
{{/imports}}
} from './';
{{/hasImports}}
/**
* @type {{classname}}{{#description}}
* {{{description}}}{{/description}}
* @export
*/
export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}};

View File

@ -0,0 +1,6 @@
/**
* @type {{classname}}{{#description}}
* {{{description}}}{{/description}}
* @export
*/
export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}};

View File

@ -1,6 +1,8 @@
{{>licenseInfo}} {{>licenseInfo}}
{{#models}} {{#models}}
{{#model}} {{#model}}
{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}
{{^oneOf}}
{{#description}} {{#description}}
/** /**
* {{{description}}} * {{{description}}}
@ -35,5 +37,6 @@ export type {{{enumName}}} = {{#allowableValues}}{{#values}}'{{{.}}}'{{^-last}}
{{/isEnum}} {{/isEnum}}
{{/vars}} {{/vars}}
{{/hasEnums}} {{/hasEnums}}
{{/oneOf}}
{{/model}} {{/model}}
{{/models}} {{/models}}

View File

@ -9,7 +9,7 @@ import globalAxios, { AxiosPromise, AxiosInstance } from 'axios';
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from './base'; import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from './base';
{{#models}} {{#models}}
{{#model}}{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>modelGeneric}}{{/isEnum}}{{/model}} {{#model}}{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^isEnum}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}}{{/model}}
{{/models}} {{/models}}
{{#apiInfo}}{{#apis}} {{#apiInfo}}{{#apis}}
{{>apiInner}} {{>apiInner}}

View File

@ -4,6 +4,6 @@
{{#withSeparateModelsAndApi}}{{#imports}} {{#withSeparateModelsAndApi}}{{#imports}}
import { {{class}} } from './{{filename}}';{{/imports}}{{/withSeparateModelsAndApi}} import { {{class}} } from './{{filename}}';{{/imports}}{{/withSeparateModelsAndApi}}
{{#models}}{{#model}} {{#models}}{{#model}}
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>modelGeneric}}{{/isEnum}} {{#isEnum}}{{>modelEnum}}{{/isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^isEnum}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}}
{{/model}}{{/models}} {{/model}}{{/models}}

View File

@ -0,0 +1,6 @@
/**
* @type {{classname}}{{#description}}
* {{{description}}}{{/description}}
* @export
*/
export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}};

View File

@ -11,6 +11,6 @@ import { {{classname}} } from './{{filename}}';
* {{{description}}} * {{{description}}}
*/ */
{{/description}} {{/description}}
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#isAlias}}{{>modelAlias}}{{/isAlias}}{{^isAlias}}{{#taggedUnions}}{{>modelTaggedUnion}}{{/taggedUnions}}{{^taggedUnions}}{{>modelGeneric}}{{/taggedUnions}}{{/isAlias}}{{/isEnum}} {{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#isAlias}}{{>modelAlias}}{{/isAlias}}{{^isAlias}}{{#taggedUnions}}{{>modelTaggedUnion}}{{/taggedUnions}}{{^taggedUnions}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/taggedUnions}}{{/isAlias}}{{/isEnum}}
{{/model}} {{/model}}
{{/models}} {{/models}}

View File

@ -0,0 +1,14 @@
{{#hasImports}}
import {
{{#imports}}
{{{.}}},
{{/imports}}
} from './';
{{/hasImports}}
/**
* @type {{classname}}{{#description}}
* {{{description}}}{{/description}}
* @export
*/
export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}};

View File

@ -8,6 +8,6 @@ import * as models from './models';
* {{{description}}} * {{{description}}}
*/ */
{{/description}} {{/description}}
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>modelGeneric}}{{/isEnum}} {{#isEnum}}{{>modelEnum}}{{/isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^isEnum}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}}
{{/model}} {{/model}}
{{/models}} {{/models}}

View File

@ -0,0 +1,6 @@
/**
* @type {{classname}}{{#description}}
* {{{description}}}{{/description}}
* @export
*/
export type {{classname}} = {{#oneOf}}models.{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}};

View File

@ -0,0 +1,14 @@
{{#hasImports}}
import {
{{#imports}}
{{{.}}},
{{/imports}}
} from './';
{{/hasImports}}
/**
* @type {{classname}}{{#description}}
* {{{description}}}{{/description}}
* @export
*/
export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}};

View File

@ -2,11 +2,6 @@
{{>licenseInfo}} {{>licenseInfo}}
{{#models}} {{#models}}
{{#model}} {{#model}}
{{#isEnum}} {{#isEnum}}{{>modelEnum}}{{/isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^isEnum}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}}
{{>modelEnum}}
{{/isEnum}}
{{^isEnum}}
{{>modelGeneric}}
{{/isEnum}}
{{/model}} {{/model}}
{{/models}} {{/models}}

View File

@ -1,64 +1,36 @@
{ openapi: 3.0.1
"openapi":"3.0.1", info:
"info":{ title: fruity
"title":"fruity", version: 0.0.1
"version":"0.0.1" paths:
}, /:
"paths":{ get:
"/":{ responses:
"get":{ '200':
"responses":{ description: desc
"200":{ content:
"description":"desc", application/json:
"content":{ schema:
"application/json":{ $ref: '#/components/schemas/fruit'
"schema":{ components:
"$ref":"#/components/schemas/fruit" schemas:
} fruit:
} title: fruit
} properties:
} color:
} type: string
} oneOf:
} - $ref: '#/components/schemas/apple'
}, - $ref: '#/components/schemas/banana'
"components":{ apple:
"schemas":{ title: apple
"fruit":{ type: object
"title":"fruit", properties:
"type":"object", kind:
"properties":{ type: string
"color":{ banana:
"type":"string" title: banana
} type: object
}, properties:
"oneOf":[ count:
{ type: number
"$ref":"#/components/schemas/apple"
},
{
"$ref":"#/components/schemas/banana"
}
]
},
"apple":{
"title":"apple",
"type":"object",
"properties":{
"kind":{
"type":"string"
}
}
},
"banana":{
"title":"banana",
"type":"object",
"properties":{
"count":{
"type":"number"
}
}
}
}
}
}

View File

@ -10,6 +10,7 @@
* Do not edit the class manually. * Do not edit the class manually.
*/ */
/** /**
* Describes the result of uploading an image resource * Describes the result of uploading an image resource
*/ */
@ -19,6 +20,7 @@ export interface ApiResponse {
message?: string; message?: string;
} }
/** /**
* A category for a pet * A category for a pet
*/ */
@ -27,6 +29,7 @@ export interface Category {
name?: string; name?: string;
} }
/** /**
* An order for a pets from the pet store * An order for a pets from the pet store
*/ */
@ -47,6 +50,7 @@ export interface Order {
*/ */
export type OrderStatusEnum = 'placed' | 'approved' | 'delivered'; export type OrderStatusEnum = 'placed' | 'approved' | 'delivered';
/** /**
* A pet for sale in the pet store * A pet for sale in the pet store
*/ */
@ -67,6 +71,7 @@ export interface Pet {
*/ */
export type PetStatusEnum = 'available' | 'pending' | 'sold'; export type PetStatusEnum = 'available' | 'pending' | 'sold';
/** /**
* A tag for a pet * A tag for a pet
*/ */
@ -75,6 +80,7 @@ export interface Tag {
name?: string; name?: string;
} }
/** /**
* A User who is purchasing from the pet store * A User who is purchasing from the pet store
*/ */