Skip to content

Commit e03add7

Browse files
Rewrite OneOfClass to standalone helper TypeMatcher
1 parent 234643d commit e03add7

49 files changed

Lines changed: 793 additions & 331 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public TypeScriptClientCodegen() {
174174
// models
175175
setModelPackage("models");
176176
supportingFiles.add(new SupportingFile("model" + File.separator + "ObjectSerializer.mustache", "models", "ObjectSerializer.ts"));
177-
supportingFiles.add(new SupportingFile("model" + File.separator + "OneOfClass.mustache", "models", "OneOfClass.ts"));
177+
supportingFiles.add(new SupportingFile("model" + File.separator + "TypeMatcher.mustache", "models", "TypeMatcher.ts"));
178178

179179
modelTemplateFiles.put("model" + File.separator + "model.mustache", ".ts");
180180

modules/openapi-generator/src/main/resources/typescript/model/OneOfClass.mustache

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Validates if data contains all required attributes from the attributeTypeMap.
3+
*
4+
* @param data - The data object to validate
5+
* @param attributeTypeMap - Array of attribute metadata including required flag
6+
* @returns true if all required attributes are present in data, false otherwise
7+
*/
8+
export function instanceOfType(data: any, attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string, required: boolean}>): boolean {
9+
for (const attribute of attributeTypeMap) {
10+
if (attribute.required) {
11+
// Check both that the property exists AND that it's not undefined.
12+
// This is important because `data[attribute.baseName] === undefined` alone
13+
// would be true for both missing properties and properties explicitly set to undefined,
14+
// while `!(attribute.baseName in data)` distinguishes between these cases.
15+
// For proper OpenAPI validation, required fields must actually be present in the data.
16+
if (!(attribute.baseName in data) || data[attribute.baseName] === undefined) {
17+
return false;
18+
}
19+
}
20+
}
21+
22+
return true;
23+
}
24+
25+
/**
26+
* Attempts to find a matching type from an array of possible types by validating
27+
* the data against each type's attribute requirements.
28+
*
29+
* @param data - The data object to match
30+
* @param types - Array of possible type constructors
31+
* @returns The name of the matching type, or undefined if no match found
32+
*/
33+
export function findMatchingType(data: any, types: Array<any>): string | undefined {
34+
for (const type of types) {
35+
if (instanceOfType(data, type.getAttributeTypeMap())) {
36+
return type.name;
37+
}
38+
}
39+
40+
return undefined;
41+
}

modules/openapi-generator/src/main/resources/typescript/model/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { {{classname}} } from '{{filename}}{{importFileExtension}}';
66
{{/tsImports}}
77
{{#oneOf}}
88
{{#-first}}
9-
import { OneOfClass } from '../models/OneOfClass{{importFileExtension}}';
9+
import { findMatchingType } from '../models/TypeMatcher{{importFileExtension}}';
1010
{{/-first}}
1111
{{/oneOf}}
1212
{{^oneOf}}

modules/openapi-generator/src/main/resources/typescript/model/modelOneOf.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type {{classname}} = {{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}};
1010
* {{{.}}}{{/description}}
1111
* @export
1212
*/
13-
export class {{classname}}Class extends OneOfClass {
13+
export class {{classname}}Class {
1414
{{#discriminator}}
1515
static readonly discriminator: string | undefined = "{{discriminatorName}}";
1616
{{/discriminator}}
@@ -32,13 +32,13 @@ export class {{classname}}Class extends OneOfClass {
3232

3333
private static readonly arrayOfTypes: Array<{{#oneOf}}typeof {{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}> = [{{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}];
3434

35+
/**
36+
* Determines which oneOf schema matches the provided data.
37+
*
38+
* @param data - The data object to match against oneOf schemas
39+
* @returns The name of the matching type, or undefined if no unique match is found
40+
*/
3541
public static findMatchingType(data: any): string | undefined {
36-
for (const type of this.arrayOfTypes) {
37-
if (this.instanceOf(data, type.getAttributeTypeMap())) {
38-
return type.name;
39-
}
40-
}
41-
42-
return undefined;
42+
return findMatchingType(data, this.arrayOfTypes);
4343
}
4444
}

samples/client/echo_api/typescript/build/.openapi-generator/FILES

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/echo_api/typescript/build/models/TypeMatcher.ts

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/others/typescript/builds/array-of-lists/.openapi-generator/FILES

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/others/typescript/builds/array-of-lists/models/TypeMatcher.ts

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/others/typescript/builds/enum-single-value/.openapi-generator/FILES

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)