Skip to content

Conversation

@czechboy0
Copy link
Contributor

Motivation

It's a useful pattern to define a single JSON schema for all your (e.g. query) parameters, and handle them as a single object in your code.

In OpenAPI, that'd be expressed like this, for example:

# parameter name: myParams in: query explode: true style: form schema: $ref: '#/components/schemas/QueryObject' # schema QueryObject: type: object properties: myString: type: string myList: type: array items: type: string

Until now, the myList property would not be allowed, and would fail to serialize and parse, as arrays within objects were not allowed for form style parameters (used by query items, by default).

Modifications

This PR extends the support of the form style to handle single nesting in the top level objects. It does not add support for arbitrarily deep nesting.

As part of this work, we also now allow the deepObject style to do the same - use arrays nested in an object.

Result

The useful pattern of having an array within a "params" object works correctly now.

Test Plan

Added unit tests for all 4 components: encoder, decoder, serializer, and parser.

Copy link
Collaborator

@simonjbeaumont simonjbeaumont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! One question about whether we need to break out this type.

@czechboy0 czechboy0 merged commit da2e5b8 into apple:main Oct 3, 2024
@czechboy0 czechboy0 deleted the hd-support-nested-arrays-in-params branch October 3, 2024 15:17
@czechboy0 czechboy0 added the 🔨 semver/patch No public API change. label Oct 18, 2024
czechboy0 added a commit that referenced this pull request Nov 21, 2024
…tyle (#127) ### Motivation As part of apple/swift-openapi-generator#259, adding deepObject parameter style support, the initial PR wasn't complete. And once we dug more into it, turns out the original implementation of the URIDecoder/URIParser didn't really lend themselves well for handling deepObject, and the recent additions of supporting arrays within dictionaries (#120) further confused the implementation. ### Modifications Refactored URIParser/URIDecoder with a clearer understanding of the current requirements. It's now much easier to follow and embraces the fact that each of the 7 variants of URI coding we support (form exploded, form unexploded, simple exploded, simple unexploded, form data exploded, form data unexploded, and now deepObject exploded) are similar, but still different in subtle ways. This new implementation doesn't try as hard to share code between the implementations, so might at first sight appear to duplicate code. The original implementation had many methods with many configuration parameters and utility methods with a high cyclomatic complexity, which made it very hard to reason about. We did away with that. While there, I also made some minor improvements to the serialization path, which allows cleaner round-tripping tests. ### Result A more maintainable and more correct URI decoder/parser implementation. ### Test Plan Added many more unit tests that test the full matrix of supported styles and inputs. --------- Co-authored-by: Si Beaumont <simonjbeaumont@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

2 participants