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

[typescript-fetch]Improved error when specifying primitive type with oneOf #18740

Open
wants to merge 4 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 @@ -19,6 +19,7 @@

import com.google.common.collect.ImmutableMap;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
Expand Down Expand Up @@ -1480,6 +1481,16 @@ public boolean isDateTimeType() {
return isDateTime && "Date".equals(dataType);
}

// for oneOf in issues-12256
public Mustache.Lambda skipPrimitiveImports;
public Mustache.Lambda generatePrimitiveFunctions;
public Mustache.Lambda addTypeGuardsForPrimitives;
public Mustache.Lambda transformArrayTypeName;
public Mustache.Lambda generateArrayFunctions;
public Mustache.Lambda extractArrayInnerType;
public Mustache.Lambda importNonPrimitiveArrays;
public Mustache.Lambda checkArrayInnerType;

public ExtendedCodegenModel(CodegenModel cm) {
super();

Expand Down Expand Up @@ -1564,6 +1575,69 @@ public ExtendedCodegenModel(CodegenModel cm) {
this.setItems(cm.getItems());
this.setAdditionalProperties(cm.getAdditionalProperties());
this.setIsModel(cm.getIsModel());
this.skipPrimitiveImports = (fragment, writer) -> {
String content = fragment.execute().trim();
if (Arrays.stream(primitiveTypes).anyMatch(type -> content.contains("from './" + type + "'")) || content.contains("Array<")) {
return;
}
writer.write(content);
};
this.generatePrimitiveFunctions = (fragment, writer) -> {
String content = fragment.execute();
if (Arrays.stream(primitiveTypes).anyMatch(s -> content.contains("function " + s + "FromJSONTyped"))) {
writer.write(content);
}
};
this.addTypeGuardsForPrimitives = (fragment, writer) -> {
String content = fragment.execute().trim();
if (Arrays.stream(primitiveTypes).noneMatch(s -> content.contains("instanceOf" + s))) {
String primitiveGuard = oneOf.stream().filter(s -> Arrays.stream(primitiveTypes).anyMatch(s::equals)).map(s -> "typeof value !== '" + s + "' && ").collect(Collectors.joining());
writer.write(primitiveGuard + content);
}else{
writer.write(content);
}
};
this.transformArrayTypeName = (fragment, writer) -> {
String content = fragment.execute().trim();

if (content.startsWith("Array<")) {
String changedName = content.replace("Array<", "").replace(">", "") + "Array";
writer.write(changedName);
} else {
writer.write(content);
}
};
this.generateArrayFunctions = (fragment, writer) -> {
String content = fragment.execute();
if (content.contains("Array<")) {
writer.write(content);
}
};
this.extractArrayInnerType = (fragment, writer) -> {
String content = fragment.execute().trim();
if (content.startsWith("Array<")) {
String changedName = content.replace("Array<", "").replace(">", "");
writer.write(changedName);
}
};
this.importNonPrimitiveArrays = (fragment, writer) -> {
String content = fragment.execute();
if (content.contains("Array<") && Arrays.stream(primitiveTypes).noneMatch(s -> content.contains("Array<" + s))) {
String replaced = content.replace("Array<", "").replace(">", "");
writer.write(replaced);
}
};
this.checkArrayInnerType = (fragment, writer) -> {
String content = fragment.execute();
if (content.contains("Array<")) {
String replaced = content.replace("Array<", "").replace(">", "");
if (Arrays.stream(primitiveTypes).anyMatch(replaced::equals)) {
writer.write("typeof element === '" + replaced + "'");
} else {
writer.write("instanceOf" + replaced + "(element)");
}
}
};
}

@Override
Expand Down Expand Up @@ -1600,5 +1674,8 @@ public String toString() {
sb.append(", hasReturnPassthroughVoid=").append(hasReturnPassthroughVoid);
return sb.toString();
}

private final String[] primitiveTypes = new String[] { "string", "number", "boolean" };

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

{{/hasImports}}
{{#oneOf}}
{{#importNonPrimitiveArrays}}
import type { {{{.}}} } from './{{{.}}}{{importFileExtension}}';
import {
instanceOf{{{.}}},
{{{.}}}FromJSON,
{{{.}}}FromJSONTyped,
{{{.}}}ToJSON,
} from './{{{.}}}{{importFileExtension}}';
{{/importNonPrimitiveArrays}}
{{/oneOf}}

{{>modelOneOfInterfaces}}

export function {{classname}}FromJSON(json: any): {{classname}} {
Expand All @@ -32,8 +45,8 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
{{/discriminator}}
{{^discriminator}}
{{#oneOf}}
if (instanceOf{{{.}}}(json)) {
return {{{.}}}FromJSONTyped(json, true);
if (instanceOf{{#transformArrayTypeName}}{{{.}}}{{/transformArrayTypeName}}(json)) {
return {{#transformArrayTypeName}}{{{.}}}{{/transformArrayTypeName}}FromJSONTyped(json, true);
}
{{/oneOf}}
{{/discriminator}}
Expand All @@ -56,11 +69,53 @@ export function {{classname}}ToJSON(value?: {{classname}} | null): any {

{{^discriminator}}
{{#oneOf}}
if (instanceOf{{{.}}}(value)) {
return {{{.}}}ToJSON(value as {{{.}}});
if ({{#addTypeGuardsForPrimitives}}instanceOf{{#transformArrayTypeName}}{{{.}}}{{/transformArrayTypeName}}{{/addTypeGuardsForPrimitives}}(value)) {
return {{#transformArrayTypeName}}{{{.}}}{{/transformArrayTypeName}}ToJSON(value as {{{.}}});
}
{{/oneOf}}

return {};
{{/discriminator}}
}

{{#oneOf}}
{{#generatePrimitiveFunctions}}
function {{{.}}}FromJSONTyped(json: any, ignoreDiscriminator: boolean): {{{.}}} | null {
return typeof json === '{{{.}}}' ? json : null;
}

function instanceOf{{{.}}}(value: any): boolean {
return typeof value === '{{{.}}}';
}

function {{{.}}}ToJSON(value: {{{.}}}): any {
return value;
}

{{#-last}}
export function instanceOf{{classname}}(value: any): boolean {
return {{#oneOf}}instanceOf{{#transformArrayTypeName}}{{{.}}}{{/transformArrayTypeName}}(value){{^-last}} || {{/-last}}{{#-last}};{{/-last}}{{/oneOf}}
}
{{/-last}}
{{/generatePrimitiveFunctions}}
{{/oneOf}}

{{#oneOf}}
{{#generateArrayFunctions}}
function instanceOf{{#extractArrayInnerType}}{{{.}}}{{/extractArrayInnerType}}Array(value: any): boolean {
return Array.isArray(value) && value.every(element => {{#checkArrayInnerType}}{{{.}}}{{/checkArrayInnerType}});
}

function {{#extractArrayInnerType}}{{{.}}}{{/extractArrayInnerType}}ArrayFromJSONTyped(json: any, ignoreDiscriminator: boolean): {{{.}}} | null {
if (!instanceOf{{#extractArrayInnerType}}{{{.}}}{{/extractArrayInnerType}}Array(json)) {
return null;
}
return json;
}

function {{#extractArrayInnerType}}{{{.}}}{{/extractArrayInnerType}}ArrayToJSON(value: {{{.}}}): any {
return value;
}

{{/generateArrayFunctions}}
{{/oneOf}}
Loading