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][axios] Handle boolean enum correctly #9025
Conversation
can't use enum because of TS18033
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good
What is the use case to have an enum with two boolean values? |
I think it's main use case is boolean literal type, not an enum with two boolean value (though I took the example above) type FooResponse = FooSucessResponse | FooFailureResponse
type FooSuccessResponse = {
success: true,
body: {
foo: string
}
}
type FooFailureResponse = {
success: false,
body: {
reason: string
}
} this case needs boolean enum with single boolean value. |
@tusharchutani any plan to merge it..? |
Is it even a valid or reasonable schema in the first place?
So it tells us more about strictness of the validator, rather than correctness of the spec.
In this case we just need to decide whether we give priority to |
This is true the original schema is not valid. If you want to use the enum
properly it should not be 'true' but just true
…On Thu, May 6, 2021, 7:35 PM Alexey Makhrov ***@***.***> wrote:
Is it even a valid or reasonable schema in the first place?
I know it satisfies the validator - but the following one also does.
type: boolean
enum: [true, false, fuzzy]
So it tells us more about strictness of the validator, rather than
correctness of the spec.
type: boolean already unambiguously indicates that the only two possible
values are true and false (but not "true" or "false"!)
In this case we just need to decide whether we give priority to type
attribute or enum attribute. In the former case, the generated code
should not emit an additional type at all - just use boolean for the
model property. In the latter case a string enum, as it's generated today,
sounds correct.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#9025 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADB4XNJKG6HFNHAPAMVVBPTTMMYT7ANCNFSM4ZQIZCGA>
.
|
Technically the base validation is invalid due to the fact that the items
'true' and 'false' are not actually Boolean anyway.
…On Thu, May 6, 2021, 7:36 PM Kenneth Steward ***@***.***> wrote:
This is true the original schema is not valid. If you want to use the enum
properly it should not be 'true' but just true
On Thu, May 6, 2021, 7:35 PM Alexey Makhrov ***@***.***>
wrote:
> Is it even a valid or reasonable schema in the first place?
> I know it satisfies the validator - but the following one also does.
>
> type: boolean
> enum: [true, false, fuzzy]
>
> So it tells us more about strictness of the validator, rather than
> correctness of the spec.
>
> type: boolean already unambiguously indicates that the only two possible
> values are true and false (but not "true" or "false"!)
>
> In this case we just need to decide whether we give priority to type
> attribute or enum attribute. In the former case, the generated code
> should not emit an additional type at all - just use boolean for the
> model property. In the latter case a string enum, as it's generated today,
> sounds correct.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#9025 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ADB4XNJKG6HFNHAPAMVVBPTTMMYT7ANCNFSM4ZQIZCGA>
> .
>
|
as a result of assigning string value to the value of boolean enum, if (response.success === 'true') { } even though you've specified the type {success: boolean} |
@amakhrov does it make sense? I'm wondering what's blocking point. |
@haedaal I still struggle to understand the use case, honestly. Why define a boolean value in the spec as an enum? Why are the enum values are defined as strings in the spec - even though you clearly want them to be boolean in the generated code. |
type GetNextOrderResponse = {success: true, data: Order} | {success: false, data?: unknown} // or any other FailReason type for data in fail case
I've made it an issue in #9024. I've also put a sample schema that generates wrong result. |
that's exactly what I thought is a problem, but if you try it, making schema like this "schemas": {
"BooleanEnum": {
"enum": [true, false],
"type": "boolean"
},
} does not fix the wrong result. it's because we check only for number type and unconditionally wrap it's value with double quote as you can see here. openapi-generator/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java Lines 249 to 255 in 11deb43
Lines 719 to 723 in 5d58afe
|
Ok, I think now I see what you're trying to do (sorry, it took me a while :) ). You don't actually need If so, I would rather try to avoid generating an
(no enum whatsoever) However, I admit this would probably be a bigger change in the templates. So as a quick solution your PR makes total sense to me |
Yes! Exactly! :) |
@@ -64,7 +64,7 @@ | |||
public String defaultValue; | |||
public String arrayModelType; | |||
public boolean isAlias; // Is this effectively an alias of another simple type | |||
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isShort, isUnboundedInteger; | |||
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isShort, isUnboundedInteger, isBoolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following function also needs to be updated as well:
openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java
Line 764 in 5d58afe
public boolean equals(Object o) { |
openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java
Line 854 in 5d58afe
public int hashCode() { |
openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java
Line 872 in 5d58afe
public String toString() { |
I'll update these in another PR after merging this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #9802
cc @OpenAPITools/generator-core-team as the change impacts the codegen model class. |
closes #9024
with following schema
https://gist.github.com/haedaal/a771d6f3df337b624ae8a2c2d176c0db
typescript-fetch currently generates
with this pr
by using 'type' rather than 'enum' we breaks consistency, but TS18033 does not allow this
this pr has 2 changes.
accidentally typescript-angular is fixed without template changes, but many others need their own fixes on template.
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
,5.1.x
,6.0.x
cc @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02)