Skip to content

Commit

Permalink
[Typescript] Generate oneOf schemas as type unions (OpenAPITools#19494)
Browse files Browse the repository at this point in the history
* Add oneOf model for Typescript generator

* Update import procces: For oneOfs only import $refs within the oneOf

* Remove new line after description

* Update samples

* Typescript: Update model.mustache

* Typescript: Remove emun from oneOf models

* Update samples

* Typescript oneOf: add discriminator and update deserialize procces

* Typescript: update tests

* Typescript oneOf: move type mapping to models

* Typescript: Update samples

* Typescript: Update type of mappig

* Typescript: Update type of mappig

* Typescript oneOf: update deserializing logic

* Typescript: Update samples

* Revert "[typescript] fix: `enum` can not receive stringified class name (OpenAPITools#19481)"

This reverts commit 4238f17.

* [typescript] chore: update fixtures

---------

Co-authored-by: ksvirkou-hubspot <[email protected]>
  • Loading branch information
joscha and ksvirkou-hubspot authored Aug 30, 2024
1 parent 1518237 commit 7510e6b
Show file tree
Hide file tree
Showing 91 changed files with 231 additions and 1,723 deletions.
9 changes: 0 additions & 9 deletions bin/configs/typescript-consolidated-enums.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,16 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}
}
}
if (!cm.oneOf.isEmpty()) {
// For oneOfs only import $refs within the oneOf
TreeSet<String> oneOfRefs = new TreeSet<>();
for (String im : cm.imports) {
if (cm.oneOf.contains(im)) {
oneOfRefs.add(im);
}
}
cm.imports = oneOfRefs;
}
}
for (ModelMap mo : models) {
CodegenModel cm = mo.getModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export * from '{{{ importPath }}}{{importFileExtension}}';

{{#models}}
{{#model}}
import { {{classname}}{{#hasEnums}}{{#vars}}{{#isEnum}}, {{classname}}{{enumName}} {{/isEnum}} {{/vars}}{{/hasEnums}} } from '{{{ importPath }}}{{importFileExtension}}';
import { {{classname}}{{#oneOf}}{{#-first}}Class{{/-first}}{{/oneOf}}{{^oneOf}}{{#hasEnums}}{{#vars}}{{#isEnum}}, {{classname}}{{enumName}} {{/isEnum}} {{/vars}}{{/hasEnums}}{{/oneOf}} } from '{{{ importPath }}}{{importFileExtension}}';
{{/model}}
{{/models}}

Expand Down Expand Up @@ -43,7 +43,7 @@ let typeMap: {[index: string]: any} = {
{{#models}}
{{#model}}
{{^isEnum}}
"{{classname}}": {{classname}},
"{{classname}}": {{classname}}{{#oneOf}}{{#-first}}Class{{/-first}}{{/oneOf}},
{{/isEnum}}
{{/model}}
{{/models}}
Expand Down Expand Up @@ -125,8 +125,11 @@ export class ObjectSerializer {
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
let mapping = typeMap[expectedType].mapping;
if (mapping != undefined && mapping[discriminatorType]) {
return mapping[discriminatorType]; // use the type given in the discriminator
} else if(typeMap[discriminatorType]) {
return discriminatorType;
} else {
return expectedType; // discriminator did not map to a type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { HttpFile } from '../http/http{{importFileExtension}}';
*/
{{/description}}
{{^isEnum}}
{{#oneOf}}
{{#-first}}{{>model/modelOneOf}}{{/-first}}
{{/oneOf}}
{{^oneOf}}
export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
{{#vars}}
{{#description}}
Expand All @@ -28,6 +32,18 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
{{^discriminator}}
static readonly discriminator: string | undefined = undefined;
{{/discriminator}}
{{#hasDiscriminatorWithNonEmptyMapping}}

static readonly mapping: {[index: string]: string} | undefined = {
{{#discriminator.mappedModels}}
"{{mappingName}}": "{{modelName}}",
{{/discriminator.mappedModels}}
};
{{/hasDiscriminatorWithNonEmptyMapping}}
{{^hasDiscriminatorWithNonEmptyMapping}}

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

{{^isArray}}
static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
Expand Down Expand Up @@ -62,13 +78,10 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
{{/discriminatorValue}}
{{/allVars}}
{{#discriminatorName}}
{{^discriminator.isEnum}}
this.{{discriminatorName}} = "{{classname}}";
{{/discriminator.isEnum}}
{{/discriminatorName}}
}
}

{{#hasEnums}}

{{#vars}}
Expand All @@ -84,6 +97,7 @@ export enum {{classname}}{{enumName}} {
{{/vars}}

{{/hasEnums}}
{{/oneOf}}
{{/isEnum}}
{{#isEnum}}
export enum {{classname}} {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{#hasImports}}
import {
{{#imports}}
{{{.}}}{{importFileExtension}},
{{/imports}}
} from './';

{{/hasImports}}
/**
* @type {{classname}}
* Type
* @export
*/
export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}};

/**
* @type {{classname}}Class{{#description}}
* {{{.}}}{{/description}}
* @export
*/
export class {{classname}}Class {
{{#discriminator}}
static readonly discriminator: string | undefined = "{{discriminatorName}}";
{{/discriminator}}
{{^discriminator}}
static readonly discriminator: string | undefined = undefined;
{{/discriminator}}
{{#hasDiscriminatorWithNonEmptyMapping}}

static readonly mapping: {[index: string]: string} | undefined = {
{{#discriminator.mappedModels}}
"{{mappingName}}": "{{modelName}}",
{{/discriminator.mappedModels}}
};
{{/hasDiscriminatorWithNonEmptyMapping}}
{{^hasDiscriminatorWithNonEmptyMapping}}

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ export class ObjectSerializer {
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
let mapping = typeMap[expectedType].mapping;
if (mapping != undefined && mapping[discriminatorType]) {
return mapping[discriminatorType]; // use the type given in the discriminator
} else if(typeMap[discriminatorType]) {
return discriminatorType;
} else {
return expectedType; // discriminator did not map to a type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class Response {

static readonly discriminator: string | undefined = undefined;

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

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
"name": "nonUniqueArray",
Expand All @@ -39,4 +41,3 @@ export class Response {
public constructor() {
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class ApiResponse {

static readonly discriminator: string | undefined = undefined;

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

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
"name": "code",
Expand Down Expand Up @@ -49,4 +51,3 @@ export class ApiResponse {
public constructor() {
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class Category {

static readonly discriminator: string | undefined = undefined;

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

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
"name": "id",
Expand All @@ -42,4 +44,3 @@ export class Category {
public constructor() {
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ export class ObjectSerializer {
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
let mapping = typeMap[expectedType].mapping;
if (mapping != undefined && mapping[discriminatorType]) {
return mapping[discriminatorType]; // use the type given in the discriminator
} else if(typeMap[discriminatorType]) {
return discriminatorType;
} else {
return expectedType; // discriminator did not map to a type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class Order {

static readonly discriminator: string | undefined = undefined;

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

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
"name": "id",
Expand Down Expand Up @@ -74,7 +76,6 @@ export class Order {
}
}


export enum OrderStatusEnum {
Placed = 'placed',
Approved = 'approved',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export class Pet {

static readonly discriminator: string | undefined = undefined;

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

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
"name": "id",
Expand Down Expand Up @@ -76,7 +78,6 @@ export class Pet {
}
}


export enum PetStatusEnum {
Available = 'available',
Pending = 'pending',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class Tag {

static readonly discriminator: string | undefined = undefined;

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

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
"name": "id",
Expand All @@ -42,4 +44,3 @@ export class Tag {
public constructor() {
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export class User {

static readonly discriminator: string | undefined = undefined;

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

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
"name": "id",
Expand Down Expand Up @@ -87,4 +89,3 @@ export class User {
public constructor() {
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class Cat {

static readonly discriminator: string | undefined = undefined;

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

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
"name": "hunts",
Expand All @@ -39,4 +41,3 @@ export class Cat {
public constructor() {
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class Dog {

static readonly discriminator: string | undefined = undefined;

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

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
"name": "bark",
Expand All @@ -40,7 +42,6 @@ export class Dog {
}
}


export enum DogBreedEnum {
Dingo = 'Dingo',
Husky = 'Husky',
Expand Down
Loading

0 comments on commit 7510e6b

Please sign in to comment.