-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[TypeScript] Implement oneOf type resolution without discriminator #22201
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
0e6998f
451f488
74351c7
fe10060
234643d
e03add7
49e768a
97a775d
eee422e
81acbbc
8e7a128
de4e09e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export class OneOfClass { | ||
ksvirkou-hubspot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public static instanceOf(data: any, attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string, required: boolean}>): boolean { | ||
ksvirkou-hubspot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for(const attribute of attributeTypeMap) { | ||
ksvirkou-hubspot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (attribute.required) { | ||
| if (!(attribute.baseName in data) || data[attribute.baseName] === undefined) { | ||
ksvirkou-hubspot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,14 @@ | |
| {{#tsImports}} | ||
| import { {{classname}} } from '{{filename}}{{importFileExtension}}'; | ||
| {{/tsImports}} | ||
| {{#oneOf}} | ||
| {{#-first}} | ||
| import { OneOfClass } from '../models/OneOfClass{{importFileExtension}}'; | ||
|
||
| {{/-first}} | ||
| {{/oneOf}} | ||
| {{^oneOf}} | ||
| import { HttpFile } from '../http/http{{importFileExtension}}'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we sure this is not used in the oneOf case? |
||
| {{/oneOf}} | ||
|
|
||
| {{#description}} | ||
| /** | ||
|
|
@@ -46,13 +53,14 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{ | |
| {{/hasDiscriminatorWithNonEmptyMapping}} | ||
|
|
||
| {{^isArray}} | ||
| static {{#parent}}override {{/parent}}readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [ | ||
| static {{#parent}}override {{/parent}}readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string, required: boolean}> = [ | ||
| {{#vars}} | ||
| { | ||
| "name": "{{name}}", | ||
| "baseName": "{{baseName}}", | ||
| "type": "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}", | ||
| "format": "{{dataFormat}}" | ||
| "format": "{{dataFormat}}", | ||
| "required": {{required}} | ||
| }{{^-last}}, | ||
| {{/-last}} | ||
| {{/vars}} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,3 @@ | ||
| {{#hasImports}} | ||
| import { | ||
| {{#imports}} | ||
| {{{.}}}{{importFileExtension}}, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this not needed anymore? |
||
| {{/imports}} | ||
| } from './'; | ||
|
|
||
| {{/hasImports}} | ||
| /** | ||
| * @type {{classname}} | ||
| * Type | ||
|
|
@@ -18,7 +10,7 @@ export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}; | |
| * {{{.}}}{{/description}} | ||
| * @export | ||
| */ | ||
| export class {{classname}}Class { | ||
| export class {{classname}}Class extends OneOfClass { | ||
|
||
| {{#discriminator}} | ||
| static readonly discriminator: string | undefined = "{{discriminatorName}}"; | ||
| {{/discriminator}} | ||
|
|
@@ -37,4 +29,16 @@ export class {{classname}}Class { | |
|
|
||
| static readonly mapping: {[index: string]: string} | undefined = undefined; | ||
| {{/hasDiscriminatorWithNonEmptyMapping}} | ||
|
|
||
| static readonly arrayOfTypes: Array<{{#oneOf}}typeof {{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}> = [{{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}]; | ||
ksvirkou-hubspot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static findMatchingType(data:any): string | undefined { | ||
ksvirkou-hubspot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ksvirkou-hubspot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for(const type of this.arrayOfTypes) { | ||
ksvirkou-hubspot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (this.instanceOf(data, type.getAttributeTypeMap())) { | ||
| return type.name; | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| openapi: 3.0.0 | ||
| info: | ||
| version: 1.0.0 | ||
| title: testing oneOf | ||
| servers: | ||
| - url: http://localhost:3000 | ||
| paths: | ||
| /test: | ||
| get: | ||
| operationId: testWithoutDiscriminator | ||
| responses: | ||
| 200: | ||
| description: OK | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: '#/components/schemas/PetResponse' | ||
| /test-discriminator: | ||
| get: | ||
| operationId: testDiscriminator | ||
| responses: | ||
| 200: | ||
| description: OK | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: '#/components/schemas/PetDiscriminatorResponse' | ||
| components: | ||
| schemas: | ||
| PetDiscriminatorResponse: | ||
| discriminator: | ||
| propertyName: petType | ||
| mapping: | ||
| cat: "#/components/schemas/Cat" | ||
| dog: "#/components/schemas/Dog" | ||
| oneOf: | ||
| - $ref: "#/components/schemas/Cat" | ||
| - $ref: "#/components/schemas/Dog" | ||
| PetResponse: | ||
| oneOf: | ||
| - $ref: "#/components/schemas/Cat" | ||
| - $ref: "#/components/schemas/Dog" | ||
| Cat: | ||
| type: object | ||
| properties: | ||
| name: | ||
| type: string | ||
| petType: | ||
| type: string | ||
| required: | ||
| - name | ||
| - petType | ||
| Dog: | ||
| type: object | ||
| properties: | ||
| bark: | ||
| type: string | ||
| petType: | ||
| type: string | ||
| required: | ||
| - bark | ||
| - petType |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.