Skip to content
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

[bugfix] [typescript-fetch] add oneOf string union & array support (#19909) #20193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;

public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperties {
/**
Expand Down Expand Up @@ -960,6 +955,19 @@ public void setIsEnum(boolean isEnum) {
this.isEnum = isEnum;
}

@SuppressWarnings("unchecked")
public List<String> getAllowableValuesList() {
return Optional.ofNullable(this.getAllowableValues())
.map(allowableValues -> allowableValues.get("values"))
.flatMap(valuesList -> {
try {
return Optional.ofNullable((List<String>)valuesList);
} catch (ClassCastException e) {
return Optional.empty();
}
})
.orElse(Collections.emptyList());
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,11 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S

}
} else if (composed.getOneOf() != null) {
if (ModelUtils.isStringSchema(interfaceSchema) && ModelUtils.isEnumSchema(interfaceSchema)) {
// use string union literals
languageType = getEnumStringLiteralUnionType(interfaceSchema);
}

if (m.oneOf.contains(languageType)) {
LOGGER.debug("{} (oneOf schema) already has `{}` defined and therefore it's skipped.", m.name, languageType);
} else {
Expand Down Expand Up @@ -2819,6 +2824,13 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S
// end of code block for composed schema
}

@SuppressWarnings("unchecked")
private static String getEnumStringLiteralUnionType(Schema interfaceSchema) {
return (String) interfaceSchema.getEnum().stream()
.map(enumName -> "\"" + (String)enumName + "\"")
.collect(Collectors.joining(" | "));
Copy link
Member

@wing328 wing328 Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that the same as https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java#L1123 ?

if overriding toOneOfName won't meet your requirement, can you please elaborate?

cc @OpenAPITools/generator-core-team

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 Yea, they are pretty similar. The core difference is that toOneOfName seems to build the whole oneOf type, while mine only builds the string literal part. It would be nice to refactor them both into one method.

The Problem is, toOneOfName seems to be completely unused in the oneOf model generation.
Currently, the different oneOf union parts are just put in an array and are appended together inside the mustache, not using toOneOfName at all.

Refactoring it to build the full oneOf type inside the toOneOfName instead of inside the mustache seems to be a major change to how the code generation works and i need some input if this is the correct way, or if i misunderstood something, as i am new to the codebase

}

/**
* Combines all previously-detected type entries for a schema with newly-discovered ones, to ensure
* that schema for items like enum include all possible values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,16 +780,39 @@ private ExtendedCodegenModel processCodeGenModel(ExtendedCodegenModel cm) {
}
}
}

cm.hasStringOneOf = cm.oneOf.contains("string");
cm.hasStringArrayOneOf = cm.oneOf.contains("Array<string>");

List<CodegenProperty> oneOfsList = Optional.ofNullable(cm.getComposedSchemas())
.map(CodegenComposedSchemas::getOneOf)
.orElse(Collections.emptyList());

cm.oneOfModels = oneOfsList.stream()
.filter(CodegenProperty::getIsModel)
.map(CodegenProperty::getBaseType)
.filter(Objects::nonNull)
.collect(Collectors.toCollection(TreeSet::new));

cm.oneOfStringEnums = oneOfsList.stream()
.filter(CodegenProperty::getIsEnum)
.map(CodegenProperty::getAllowableValuesList)
.flatMap(Collection::stream)
.collect(Collectors.toCollection(TreeSet::new));

cm.oneOfArrays = oneOfsList.stream()
.filter(CodegenProperty::getIsArray)
.map(CodegenProperty::getComplexType)
.filter(Objects::nonNull)
.collect(Collectors.toCollection(TreeSet::new));

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;
cm.imports = cm.imports.stream()
.filter(im -> cm.oneOfModels.contains(im) || cm.oneOfArrays.contains(im))
.collect(Collectors.toCollection(TreeSet::new));
}

return cm;
}

Expand Down Expand Up @@ -1472,6 +1495,16 @@ public String toString() {
public class ExtendedCodegenModel extends CodegenModel {
@Getter @Setter
public Set<String> modelImports = new TreeSet<String>();

@Getter @Setter
public Set<String> oneOfModels = new TreeSet<>();
@Getter @Setter
public Set<String> oneOfStringEnums = new TreeSet<>();
@Getter @Setter
public Set<String> oneOfArrays = new TreeSet<>();

public boolean hasStringOneOf;
public boolean hasStringArrayOneOf;
public boolean isEntity; // Is a model containing an "id" property marked as isUniqueId
public String returnPassthrough;
public boolean hasReturnPassthroughVoid;
Expand Down Expand Up @@ -1568,6 +1601,7 @@ public ExtendedCodegenModel(CodegenModel cm) {
this.setItems(cm.getItems());
this.setAdditionalProperties(cm.getAdditionalProperties());
this.setIsModel(cm.getIsModel());
this.setComposedSchemas(cm.getComposedSchemas());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class TypeScriptReduxQueryClientCodegen extends AbstractTypeScriptClientCodegen {

Expand Down Expand Up @@ -145,13 +146,9 @@ 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;
cm.imports = cm.imports.stream()
.filter(cm.oneOf::contains)
.collect(Collectors.toCollection(TreeSet::new));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{{#hasImports}}
{{#oneOf}}
{{#imports}}
import type { {{{.}}} } from './{{.}}{{importFileExtension}}';
import {
instanceOf{{{.}}},
{{{.}}}FromJSON,
{{{.}}}FromJSONTyped,
{{{.}}}ToJSON,
} from './{{.}}{{importFileExtension}}';
{{/oneOf}}
{{/imports}}

{{/hasImports}}
{{>modelOneOfInterfaces}}
Expand All @@ -31,11 +31,40 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
}
{{/discriminator}}
{{^discriminator}}
{{#oneOf}}
{{#oneOfStringEnums}}
if (json === "{{{.}}}") {
return json;
}
{{/oneOfStringEnums}}
{{#hasStringOneOf}}
if (json instanceof String) {
return json as {{classname}};
}
{{/hasStringOneOf}}
if (!(json instanceof Object)){
return {} as any;
}
{{#hasStringArrayOneOf}}
if (Array.isArray(json) && json.every(item => item instanceof String)) {
return json as Array<string>;
}
{{/hasStringArrayOneOf}}
{{#oneOfModels}}
if (instanceOf{{{.}}}(json)) {
return {{{.}}}FromJSONTyped(json, true);
}
{{/oneOf}}
{{/oneOfModels}}
{{#oneOfArrays}}
{{#-first}}
if (Array.isArray(json) && json.every(item => item instanceof Object)) {
{{/-first}}
if (json.every(item => instanceOf{{{.}}}(item as Object))) {
return json.map(value => {{{.}}}FromJSONTyped(value, true));
}
{{#-last}}
}
{{/-last}}
{{/oneOfArrays}}

return {} as any;
{{/discriminator}}
Expand All @@ -61,11 +90,40 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis
{{/discriminator}}

{{^discriminator}}
{{#oneOf}}
{{#oneOfStringEnums}}
if (value === "{{{.}}}") {
return value;
}
{{/oneOfStringEnums}}
{{#hasStringOneOf}}
if (value instanceof String) {
return value;
}
{{/hasStringOneOf}}
if (!(value instanceof Object)){
return {};
}
{{#hasStringArrayOneOf}}
if (Array.isArray(value) && value.every(item => item instanceof String)) {
return value;
}
{{/hasStringArrayOneOf}}
{{#oneOfModels}}
if (instanceOf{{{.}}}(value)) {
return {{{.}}}ToJSON(value as {{{.}}});
}
{{/oneOf}}
{{/oneOfModels}}
{{#oneOfArrays}}
{{#-first}}
if (Array.isArray(value) && value.every(item => item instanceof Object)) {
{{/-first}}
if (value.every(item => instanceOf{{{.}}}(item as Object))) {
return value.map(value => {{{.}}}ToJSON(value as {{{.}}}));
}
{{#-last}}
}
{{/-last}}
{{/oneOfArrays}}

return {};
{{/discriminator}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,38 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/TestResponse'
/test-array:
get:
operationId: testArray
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/TestArrayResponse'
components:
schemas:
TestArrayResponse:
oneOf:
- type: array
items:
$ref: "#/components/schemas/TestA"
- type: array
items:
$ref: "#/components/schemas/TestB"
- type: array
items:
type: string
TestResponse:
oneOf:
- $ref: "#/components/schemas/TestA"
- $ref: "#/components/schemas/TestB"
- type: string
enum:
- StringEnum1
- StringEnum2
- type: string
TestA:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apis/DefaultApi.ts
apis/index.ts
index.ts
models/TestA.ts
models/TestArrayResponse.ts
models/TestB.ts
models/TestResponse.ts
models/index.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

import * as runtime from '../runtime';
import type {
TestArrayResponse,
TestResponse,
} from '../models/index';
import {
TestArrayResponseFromJSON,
TestArrayResponseToJSON,
TestResponseFromJSON,
TestResponseToJSON,
} from '../models/index';
Expand Down Expand Up @@ -51,4 +54,28 @@ export class DefaultApi extends runtime.BaseAPI {
return await response.value();
}

/**
*/
async testArrayRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<TestArrayResponse>> {
const queryParameters: any = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be at least:

Suggested change
const queryParameters: any = {};
const queryParameters: Record<string, any> = {};

?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in fact, it can even be runtime.HTTPQuery

But i think this is outside of the scope of this PR as I did not make any changes to the api mustache, i just added a new testcase.
This PR is only about fixing the model mustache, not improving the api mustache, so I would recommend opening a new PR for this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, sorry, I assumed because it was a newly generated block, it originated from your change, too.


const headerParameters: runtime.HTTPHeaders = {};

const response = await this.request({
path: `/test-array`,
method: 'GET',
headers: headerParameters,
query: queryParameters,
}, initOverrides);

return new runtime.JSONApiResponse(response, (jsonValue) => TestArrayResponseFromJSON(jsonValue));
}

/**
*/
async testArray(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<TestArrayResponse> {
const response = await this.testArrayRaw(initOverrides);
return await response.value();
}

}
Loading