Skip to content

[Typescript] Generate oneOf schemas as type unions #19494

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

Merged
merged 19 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
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
Loading