diff --git a/.changeset/angry-phones-arrive.md b/.changeset/angry-phones-arrive.md new file mode 100644 index 0000000000..2afcd240a9 --- /dev/null +++ b/.changeset/angry-phones-arrive.md @@ -0,0 +1,5 @@ +--- +'@backstage/repo-tools': minor +--- + +Add support for `oneOf` in client generated by `schema openapi generate-client`. diff --git a/NOTICE b/NOTICE index 38e5cdef85..fb23d28ebc 100644 --- a/NOTICE +++ b/NOTICE @@ -2,4 +2,6 @@ Backstage Copyright 2020 The Backstage Authors Portions of this software were developed by third-party software vendors: + - Tech Radar Plugin (https://opensource.zalando.com/tech-radar/), Copyright (c) 2017 Zalando SE +- [OpenAPI Generator Templates](./packages/repo-tools/templates), Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) Copyright 2018 SmartBear Software diff --git a/packages/repo-tools/src/commands/openapi/constants.ts b/packages/repo-tools/src/commands/openapi/constants.ts index 2e7a57522a..4936a5aee7 100644 --- a/packages/repo-tools/src/commands/openapi/constants.ts +++ b/packages/repo-tools/src/commands/openapi/constants.ts @@ -29,6 +29,7 @@ export const OUTPUT_PATH = 'src/generated'; export const OPENAPI_IGNORE_FILES = [ // Get rid of the default files. '*.md', + '*.mustache', // The rest of these have to be explicit, otherwise they get added if this was a *.* 'apis/baseapi.ts', 'apis/exception.ts', diff --git a/packages/repo-tools/templates/typescript-backstage.yaml b/packages/repo-tools/templates/typescript-backstage.yaml index b2b068856d..a2cb61c121 100644 --- a/packages/repo-tools/templates/typescript-backstage.yaml +++ b/packages/repo-tools/templates/typescript-backstage.yaml @@ -8,6 +8,20 @@ files: model.mustache: templateType: Model destinationFilename: .model.ts + modelGeneric.mustache: + templateType: SupportingFiles + modelOneOf.mustache: + templateType: SupportingFiles + modelGenericAdditionalProperties.mustache: + templateType: SupportingFiles + modelGenericEnums.mustache: + templateType: SupportingFiles + modelAlias.mustache: + templateType: SupportingFiles + modelEnum.mustache: + templateType: SupportingFiles + modelTaggedUnion.mustache: + templateType: SupportingFiles models/models_all.mustache: templateType: SupportingFiles destinationFilename: models/index.ts diff --git a/packages/repo-tools/templates/typescript-backstage/licenseInfo.mustache b/packages/repo-tools/templates/typescript-backstage/licenseInfo.mustache new file mode 100644 index 0000000000..afc90f6227 --- /dev/null +++ b/packages/repo-tools/templates/typescript-backstage/licenseInfo.mustache @@ -0,0 +1,7 @@ +// + +/** + * {{{appName}}}{{#version}}@{{{.}}}{{/version}} + * + * NOTE: This class is auto generated, do not edit the class manually. + */ \ No newline at end of file diff --git a/packages/repo-tools/templates/typescript-backstage/model.mustache b/packages/repo-tools/templates/typescript-backstage/model.mustache index 1f288380e0..97d7858cb2 100644 --- a/packages/repo-tools/templates/typescript-backstage/model.mustache +++ b/packages/repo-tools/templates/typescript-backstage/model.mustache @@ -1,43 +1,17 @@ -// - +{{! Sourced from https://github.com/OpenAPITools/openapi-generator/blob/7347daec61b2cb8d3d28e1ed06fe8b5e682090f8/modules/openapi-generator/src/main/resources/typescript-angular/model.mustache#L17. }} +{{>licenseInfo}} {{#models}} {{#model}} {{#tsImports}} -import { {{classname}} } from '{{filename}}.model{{importFileExtension}}'; +import { {{classname}} } from '{{filename}}.model'; {{/tsImports}} + {{#description}} /** -* {{{.}}} -*/ + * {{{.}}} + */ {{/description}} -{{^isEnum}} -export interface {{classname}} { -{{#additionalPropertiesType}} - [key: string]: {{{additionalPropertiesType}}}; -{{/additionalPropertiesType}} -{{#vars}} -{{#description}} - /** - * {{{.}}} - */ -{{/description}} - '{{name}}'{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}}; -{{/vars}} -} - -{{#hasEnums}} - -{{#vars}} -{{#isEnum}} -export type {{classname}}{{enumName}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}}; -{{/isEnum}} -{{/vars}} - -{{/hasEnums}} -{{/isEnum}} -{{#isEnum}} -export type {{classname}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}}; -{{/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}} \ No newline at end of file +{{/models}} diff --git a/packages/repo-tools/templates/typescript-backstage/modelAlias.mustache b/packages/repo-tools/templates/typescript-backstage/modelAlias.mustache new file mode 100644 index 0000000000..351dc37c78 --- /dev/null +++ b/packages/repo-tools/templates/typescript-backstage/modelAlias.mustache @@ -0,0 +1,3 @@ +{{! Sourced from https://github.com/OpenAPITools/openapi-generator/blob/7347daec61b2cb8d3d28e1ed06fe8b5e682090f8/modules/openapi-generator/src/main/resources/typescript-angular/modelAlias.mustache }} + +export type {{classname}} = {{dataType}}; \ No newline at end of file diff --git a/packages/repo-tools/templates/typescript-backstage/modelEnum.mustache b/packages/repo-tools/templates/typescript-backstage/modelEnum.mustache new file mode 100644 index 0000000000..0deab70f02 --- /dev/null +++ b/packages/repo-tools/templates/typescript-backstage/modelEnum.mustache @@ -0,0 +1,22 @@ +{{! Sourced from https://github.com/OpenAPITools/openapi-generator/blob/7347daec61b2cb8d3d28e1ed06fe8b5e682090f8/modules/openapi-generator/src/main/resources/typescript-angular/modelEnum.mustache }} + +{{#stringEnums}} +export enum {{classname}} { +{{#allowableValues}} +{{#enumVars}} + {{name}} = {{{value}}}{{^-last}},{{/-last}} +{{/enumVars}} +{{/allowableValues}} +} +{{/stringEnums}} +{{^stringEnums}} +export type {{classname}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}}; + +export const {{classname}} = { +{{#allowableValues}} +{{#enumVars}} + {{name}}: {{{value}}} as {{classname}}{{^-last}},{{/-last}} +{{/enumVars}} +{{/allowableValues}} +}; +{{/stringEnums}} diff --git a/packages/repo-tools/templates/typescript-backstage/modelGeneric.mustache b/packages/repo-tools/templates/typescript-backstage/modelGeneric.mustache new file mode 100644 index 0000000000..d51eeaed80 --- /dev/null +++ b/packages/repo-tools/templates/typescript-backstage/modelGeneric.mustache @@ -0,0 +1,38 @@ +{{! Sourced from https://github.com/OpenAPITools/openapi-generator/blob/7347daec61b2cb8d3d28e1ed06fe8b5e682090f8/modules/openapi-generator/src/main/resources/typescript-angular/modelGeneric.mustache }} + +{{#models}} +{{#model}} + +{{#description}} +/** +* {{{.}}} +*/ +{{/description}} +{{^isEnum}} +export interface {{classname}} { +{{>modelGenericAdditionalProperties}} +{{#vars}} +{{#description}} + /** + * {{{.}}} + */ +{{/description}} + '{{name}}'{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}}; +{{/vars}} +} + +{{#hasEnums}} + +{{#vars}} +{{#isEnum}} +export type {{classname}}{{enumName}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}}; +{{/isEnum}} +{{/vars}} + +{{/hasEnums}} +{{/isEnum}} +{{#isEnum}} +export type {{classname}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}}; +{{/isEnum}} +{{/model}} +{{/models}} \ No newline at end of file diff --git a/packages/repo-tools/templates/typescript-backstage/modelGenericAdditionalProperties.mustache b/packages/repo-tools/templates/typescript-backstage/modelGenericAdditionalProperties.mustache new file mode 100644 index 0000000000..b8d7d1ceb7 --- /dev/null +++ b/packages/repo-tools/templates/typescript-backstage/modelGenericAdditionalProperties.mustache @@ -0,0 +1,7 @@ +{{! Sourced from https://github.com/OpenAPITools/openapi-generator/blob/7347daec61b2cb8d3d28e1ed06fe8b5e682090f8/modules/openapi-generator/src/main/resources/typescript-angular/modelGenericAdditionalProperties.mustache }} + +{{#additionalPropertiesType}} + + [key: string]: {{{additionalPropertiesType}}}{{#hasVars}} | any{{/hasVars}}; + +{{/additionalPropertiesType}} \ No newline at end of file diff --git a/packages/repo-tools/templates/typescript-backstage/modelGenericEnums.mustache b/packages/repo-tools/templates/typescript-backstage/modelGenericEnums.mustache new file mode 100644 index 0000000000..8d7b49c9c3 --- /dev/null +++ b/packages/repo-tools/templates/typescript-backstage/modelGenericEnums.mustache @@ -0,0 +1,32 @@ +{{! Sourced from https://github.com/OpenAPITools/openapi-generator/blob/7347daec61b2cb8d3d28e1ed06fe8b5e682090f8/modules/openapi-generator/src/main/resources/typescript-angular/modelGenericEnums.mustache }} + +{{#hasEnums}} + +{{^stringEnums}} +export namespace {{classname}} { +{{/stringEnums}} +{{#vars}} + {{#isEnum}} +{{#stringEnums}} +export enum {{classname}}{{enumName}} { +{{#allowableValues}} +{{#enumVars}} + {{name}} = {{{value}}}{{^-last}},{{/-last}} +{{/enumVars}} +{{/allowableValues}} +}; +{{/stringEnums}} +{{^stringEnums}} + export type {{enumName}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}}; + export const {{enumName}} = { + {{#allowableValues}} + {{#enumVars}} + {{name}}: {{{value}}} as {{enumName}}{{^-last}},{{/-last}} + {{/enumVars}} + {{/allowableValues}} + }; +{{/stringEnums}} + {{/isEnum}} +{{/vars}} +{{^stringEnums}}}{{/stringEnums}} +{{/hasEnums}} \ No newline at end of file diff --git a/packages/repo-tools/templates/typescript-backstage/modelOneOf.mustache b/packages/repo-tools/templates/typescript-backstage/modelOneOf.mustache new file mode 100644 index 0000000000..b261789c3d --- /dev/null +++ b/packages/repo-tools/templates/typescript-backstage/modelOneOf.mustache @@ -0,0 +1,16 @@ +{{! Sourced from https://github.com/OpenAPITools/openapi-generator/blob/7347daec61b2cb8d3d28e1ed06fe8b5e682090f8/modules/openapi-generator/src/main/resources/typescript-angular/modelOneOf.mustache }} + +{{#hasImports}} +import { + {{#imports}} + {{{.}}}, + {{/imports}} +} from './'; + +{{/hasImports}} +{{#description}} + /** + * {{{.}}} + */ +{{/description}} +export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}; diff --git a/packages/repo-tools/templates/typescript-backstage/modelTaggedUnion.mustache b/packages/repo-tools/templates/typescript-backstage/modelTaggedUnion.mustache new file mode 100644 index 0000000000..a3f5e00904 --- /dev/null +++ b/packages/repo-tools/templates/typescript-backstage/modelTaggedUnion.mustache @@ -0,0 +1,23 @@ +{{! Sourced from https://github.com/OpenAPITools/openapi-generator/blob/7347daec61b2cb8d3d28e1ed06fe8b5e682090f8/modules/openapi-generator/src/main/resources/typescript-angular/modelTaggedUnion.mustache }} + +{{#discriminator}} +export type {{classname}} = {{#children}}{{^-first}} | {{/-first}}{{classname}}{{/children}}; +{{/discriminator}} +{{^discriminator}} +{{#parent}} +export interface {{classname}} { {{>modelGenericAdditionalProperties}} +{{#allVars}} + {{#description}} + /** + * {{{.}}} + */ + {{/description}} + {{name}}{{^required}}?{{/required}}: {{#discriminatorValue}}'{{.}}'{{/discriminatorValue}}{{^discriminatorValue}}{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{/discriminatorValue}}{{#isNullable}} | null{{/isNullable}}; +{{/allVars}} +} +{{>modelGenericEnums}} +{{/parent}} +{{^parent}} +{{>modelGeneric}} +{{/parent}} +{{/discriminator}} \ No newline at end of file