Open AnotiaWang opened 1 month ago
Here are the code search results. I'm now analyzing these search results to write the PR.
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java
In the `postProcessModels` method, set the `enumType` property on enum CodegenProperties to make the enum type available in the template.
---
+++
@@ -9,6 +9,7 @@
for (CodegenProperty var : cm.vars) {
if (Boolean.TRUE.equals(var.isEnum)) {
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + var.enumName);
+ var.enumType = cm.classname + var.enumName;
}
}
if (cm.parent != null) {
@@ -16,6 +17,7 @@
if (Boolean.TRUE.equals(var.isEnum)) {
var.datatypeWithEnum = var.datatypeWithEnum
.replace(var.enumName, cm.classname + var.enumName);
+ var.enumType = cm.classname + var.enumName;
}
}
}
modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache
Use the `enumType` property passed from TypeScriptClientCodegen.java instead of `Object.values({{datatypeWithEnum}})`.
---
+++
@@ -1,5 +1,5 @@
{{#vars}}
{{#required}}
- if (!('{{name}}' in value) || value['{{name}}'] === undefined) return false;
+ if (!('{{name}}' in value){{#isEnum}} || value['{{name}}'] !== {{enumType}}[0]{{/isEnum}}) return false;
{{/required}}
{{/vars}}
Your changes have been successfully made to the branch sweep/typescript_fetch_generated_code_with_on
. I have validated these changes using a syntax checker and a linter.
[!TIP] To recreate the pull request, edit the issue title or description.
This is an automated message generated by Sweep AI.
I'm using the TypeScript + fetch template. When I am using
oneOf
to define models, the generated code does not check the enum property to judge which model instance it is.For example, for the schema file below:
Exmaple of generated code:
So in this case,
Payload2
andPayload3
is never matched, which causes bugs in production.Expected behavior
The
instanceOfXXX()
function should check if property type is exactly the enum value.Additional Information
An example of fixing this is:
modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache: