-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[typescript-fetch] oneOf models now consider primitives when converting #21464
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
Conversation
|
@DavidGrath can you resolve the merge conflict? |
|
@macjohnny, yes, please give me till early Saturday or sooner |
|
@macjohnny, resolved |
|
@macjohnny thank you |
|
@DavidGrath thank you for the fix! |
|
@DavidGrath there was one other thing I noticed about the serialization / deserialization code which I thought I would check with you about. Does it make sense to distinguish between In the sample OAS you provided for the bug two of the - type: string
format: date
- type: string
format: date-timeWhich results in this code in the deserialization method (which is fine but the second // is getTime supposed to distinguish between `isDateType` & `isDateTimeType`?
if (!(isNaN(new Date(json).getTime()))) {
return json == null ? undefined : (new Date(json));
}
if (!(isNaN(new Date(json).getTime()))) {
return json == null ? undefined : (new Date(json));
}But the serialization method might cause an issue: if (value instanceof Date) {
return ((value).toISOString().substring(0,10));
}
if (value instanceof Date) {
return value == null ? undefined : ((value).toISOString());
}Since the If we reverse the checks for Does that seem reasonable to you? |
|
@btpnlsl , sorry for the delayed response. You're right. I mainly copied and modified the date and time logic from somewhere else and so I didn't think too deeply about the need to distinguish between the two or the need for extra precision. I think if I make another PR for a separate issue, I can include this improvement as well |
|
I am currently looking into the if(typeof json === 'string' && (json === 'fixed-value-a' || json === 'fixed-value-b' || json === 'fixed-value-c')) {
return json;
}
if(typeof json === 'number' && (json === 10 || json === 20 || json === 30)) {
return json;
}
if (Array.isArray(json)) {
if (json.every(item => typeof item === 'number')) {
return json;
}
}
if (Array.isArray(json)) {
if (json.every(item => typeof item === 'string')) {
return json;
}
}
return {} as any;It isn't transforming the json, but if the conditions aren't met, it is returning |
|
@WIStudent It's validation code that is attempting to ensure that the json matches one of the possible types that compose the Looking at the code snippet I would assume that the schema for that type looks something like oneOf:
- type: string
enum:
- "fixed-value-a"
- "fixed-value-b"
- "fixed-value-c"
- type: integer
format: int64
enum: [10, 20, 30]
- type: array
items:
type: number
- type: array
items:
type: stringAnd the code that you referenced is attempting to validate the json. One thing I have noticed is that the Open API parser / generators may not be doing a great job at either failing to parse a spec with an ambiguous oneOf schema. For instance: oneOf:
- type: number
- type: integer
enum: [10, 20, 30]How should this be evaluated? Is it just that any number is valid? Should this just evaluate to the most primitive possible representation for the language that is being generated? Is it possible by doing so that there may be some edge cases where the contractual representation of the same The OAS spec has very little to say about how
Which implies that having a Then there is the question about what the generator should do if the incoming Json does not match against any of the possible |
…ng. Issue OpenAPITools#21259 (OpenAPITools#21464) * [typescript-fetch] number, string, and Date now considered in oneOf models. Issue OpenAPITools#21259 * Generated samples
…ipt-fetch oneOf logic OpenAPITools#21057 OpenAPITools#21464 (OpenAPITools#21638) * Add unit tests for current oneOf logic OpenAPITools#21057 OpenAPITools#21464 * Remove comment from issue_21259.yaml --------- Co-authored-by: Chris Gual <[email protected]>
PR checklist
Commit all changed files.
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*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)The output looks like this:
@joscha @macjohnny , please help review for TypeScript