-
Notifications
You must be signed in to change notification settings - Fork 227
Handle union types in parameters #888
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
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.
Pull Request Overview
This PR adds support for handling union types in OpenAPI 3.1.x parameters by selecting the first type from an array of types. OpenAPI 3.1.x allows parameters to have multiple types (e.g., ["string", "integer"]
), and this change ensures the converter handles such cases gracefully.
Key changes:
- Added logic to detect union types in parameter schemas and select the first type
- Added comprehensive test coverage for union types in path, query, and header parameters
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
libV2/schemaUtils.js | Added union type handling logic in createProperties function |
test/unit/convertV2WithTypes.test.js | Added comprehensive test case for union type parameter handling |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
libV2/schemaUtils.js
Outdated
const { schema } = param; | ||
|
||
// Handle union types (OpenAPI 3.1.x supports arrays of types like ["string", "integer"]) | ||
// Pick the first type if it's a union of types |
Copilot
AI
Oct 13, 2025
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 comment should clarify what happens when the type array is empty or contains invalid types, and mention that this is a simplistic approach that may not handle all union type scenarios.
// Pick the first type if it's a union of types | |
// This simplistic approach picks the first type if it's a union of types. | |
// If the type array is empty or contains invalid types, resolvedType will be undefined. | |
// Note: This may not handle all union type scenarios supported by OpenAPI 3.1.x. |
Copilot uses AI. Check for mistakes.
libV2/schemaUtils.js
Outdated
// Pick the first type if it's a union of types | ||
const resolvedType = Array.isArray(schema.type) ? schema.type[0] : schema.type; |
Copilot
AI
Oct 13, 2025
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 code doesn't handle the case where schema.type
is an empty array, which would result in resolvedType
being undefined
. Consider adding a check for array length or providing a fallback type.
// Pick the first type if it's a union of types | |
const resolvedType = Array.isArray(schema.type) ? schema.type[0] : schema.type; | |
// Pick the first type if it's a union of types, fallback to 'string' if array is empty | |
const resolvedType = Array.isArray(schema.type) | |
? (schema.type.length > 0 ? schema.type[0] : 'string') | |
: schema.type; |
Copilot uses AI. Check for mistakes.
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.
const resolvedType = Array.isArray(schema.type) ? schema.type[0] : schema.type;
@animeshjajoopostman can you check this .
This code doesn't handle the case where schema.type is an empty array,
libV2/schemaUtils.js
Outdated
|
||
// Handle union types (OpenAPI 3.1.x supports arrays of types like ["string", "integer"]) | ||
// Pick the first type if it's a union of types | ||
const resolvedType = Array.isArray(schema.type) ? schema.type[0] : schema.type; |
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.
These changes look fine for parameters , but won't we face this same issue for responseHeaders as well.
We create separate properties for them at Line: 2499. ?
Also, as per the slack thread, this works fine for request/response bodies right. ?
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.
Also, can you try to think if it will cause any issue once we plan to do 2-way-sync for OAS 3.1 ?
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.
Missed adding it to response headers. I'll add them.
Yes, works fine for request and response bodies.
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.
Also, can you try to think if it will cause any issue once we plan to do 2-way-sync for OAS 3.1 ?
Reverse syncing could overwrite the union of types defined in the specification with the first type picked for the collection.
For example: [string, integer] defined in the spec -> [string] after collection generation -> [string] in the spec post reverse syncing
Not specific to 2 way sync, but I'd assume an API consumer would want validations to kick in for all the types defined in the specification, and not just the first type.
This is just a fix/workaround, a proper solution needs time and needs to be thought through.
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.
I've made the required changes, and I've updated the package version on beta.
You can test this change using this spec
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.
So, this will cause problem later on when we plan on implementing 2 way Sync for OAS3.1 as the types would get updated in the spec without any change which is not expected.
a482e0c
to
f6fc421
Compare
libV2/schemaUtils.js
Outdated
schema.type; | ||
|
||
return { | ||
const properties = { |
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.
any reason to change this ?
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.
No, had changed it when trying something out. Let me revert
Handle union types in parameters
JIRA: https://postmanlabs.atlassian.net/browse/AB-1295
Before:

After:
