[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
TreeSet<String> oneOfRefs = new TreeSet<String>();
TreeSet<String> oneOfRefs = new TreeSet<>();
for (String im : cm.imports) {
if (cm.oneOf.contains(im)) {
oneOfRefs.add(im);

View File

@ -11,6 +11,6 @@ import { {{classname}} } from './{{filename}}';
* {{{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}}
{{/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}}
{{#models}}
{{#model}}
{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}
{{^oneOf}}
{{#description}}
/**
* {{{description}}}
@ -35,5 +37,6 @@ export type {{{enumName}}} = {{#allowableValues}}{{#values}}'{{{.}}}'{{^-last}}
{{/isEnum}}
{{/vars}}
{{/hasEnums}}
{{/oneOf}}
{{/model}}
{{/models}}

View File

@ -9,7 +9,7 @@ import globalAxios, { AxiosPromise, AxiosInstance } from 'axios';
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from './base';
{{#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}}
{{#apiInfo}}{{#apis}}
{{>apiInner}}

View File

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

View File

@ -1,64 +1,36 @@
{
"openapi":"3.0.1",
"info":{
"title":"fruity",
"version":"0.0.1"
},
"paths":{
"/":{
"get":{
"responses":{
"200":{
"description":"desc",
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/fruit"
}
}
}
}
}
}
}
},
"components":{
"schemas":{
"fruit":{
"title":"fruit",
"type":"object",
"properties":{
"color":{
"type":"string"
}
},
"oneOf":[
{
"$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"
}
}
}
}
}
}
openapi: 3.0.1
info:
title: fruity
version: 0.0.1
paths:
/:
get:
responses:
'200':
description: desc
content:
application/json:
schema:
$ref: '#/components/schemas/fruit'
components:
schemas:
fruit:
title: fruit
properties:
color:
type: string
oneOf:
- $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.
*/
/**
* Describes the result of uploading an image resource
*/
@ -19,6 +20,7 @@ export interface ApiResponse {
message?: string;
}
/**
* A category for a pet
*/
@ -27,6 +29,7 @@ export interface Category {
name?: string;
}
/**
* An order for a pets from the pet store
*/
@ -47,6 +50,7 @@ export interface Order {
*/
export type OrderStatusEnum = 'placed' | 'approved' | 'delivered';
/**
* A pet for sale in the pet store
*/
@ -67,6 +71,7 @@ export interface Pet {
*/
export type PetStatusEnum = 'available' | 'pending' | 'sold';
/**
* A tag for a pet
*/
@ -75,6 +80,7 @@ export interface Tag {
name?: string;
}
/**
* A User who is purchasing from the pet store
*/