Skip to content

Additional unit tests for OpenAPIObjectContainer #24

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

Merged
merged 2 commits into from
Aug 3, 2023

Conversation

rboyce
Copy link
Contributor

@rboyce rboyce commented Jul 13, 2023

Motivation

Trying to encode an OpenAPIValueContainer would previously throw an EncodingError if the untyped container contained nested dictionary or array structures. (e.g. {"dict": {"foo": "bar"}}) Data could be decoded into this format, but it could not be re-encoded successfully.

Modifications

When encoding the value of a nested JSON array or object in OpenAPIValueContainer, we now handle arrays and dicts by wrapping the inner value in OpenAPIValueContainer before passing it to the encoder.
(note: Based on the rest of the encode() implementation, it seems that this may have been expected to happen previously during decoding. Let me know if making changes on the decode side of things is preferable.)

Result

Additional unit tests added to validate that encoder generates correct output for nested data structures in OpenAPIValueContainer, and that these data structures can be decoded and successfully re-endcoded afterward.

Test Plan

Run Test_CodableExtensions tests, see all tests pass.

@simonjbeaumont
Copy link
Collaborator

@swift-server-bot add to allowlist

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.

@rboyce Thanks for the patch—and for adding some tests for it.

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.

Oops, meant to mark as "Request changes", with some minor fixups in https://github.com/apple/swift-openapi-runtime/pull/24/files#r1262219996.

czechboy0 added a commit that referenced this pull request Jul 17, 2023
Fix OpenAPIValueContainer serialization of nested values

### Motivation

Credit goes to @rboyce for discovering this bug!

When I saw the PR #24, I realized we didn't have any unit tests for the `OpenAPI*Container` types, which was an oversight.

This PR adds those missing unit tests, and fixes the bug in casting that motivated #24.

### Modifications

Adds unit tests and fixes the casting bug that expected wrapped, instead of raw stdlib values.

### Result

Now all the new unit tests pass and serializing nested values in OpenAPIValueContainer and friends works correctly.

### Test Plan

Most of this PR is new unit tests, verified they pass locally.


Reviewed by: simonjbeaumont

Builds:
     ✔︎ pull request validation (5.8) - Build finished. 
     ✔︎ pull request validation (5.9) - Build finished. 
     ✔︎ pull request validation (docc test) - Build finished. 
     ✔︎ pull request validation (integration test) - Build finished. 
     ✔︎ pull request validation (nightly) - Build finished. 
     ✔︎ pull request validation (soundness) - Build finished. 
     ✖︎ pull request validation (api breakage) - Build finished. 

#25
@czechboy0
Copy link
Contributor

@rboyce Okay I landed the base unit tests - if you'd like, you could port your additional unit tests to the new place (Test_OpenAPIValue.swift). 🙏 Thanks

@rboyce rboyce force-pushed the anyValueContainer branch from 538c735 to eadd49a Compare July 17, 2023 23:35
@rboyce rboyce changed the title Support nested values in OpenAPIObjectContainer Additional unit tests for OpenAPIObjectContainer Jul 17, 2023
@rboyce rboyce force-pushed the anyValueContainer branch from eadd49a to 9297cc4 Compare July 17, 2023 23:37
@rboyce
Copy link
Contributor Author

rboyce commented Jul 17, 2023

Rebased past #25. This PR now just contains the additional unit tests for mixed nested values; thanks for the fix @czechboy0!

@rboyce rboyce requested a review from simonjbeaumont July 18, 2023 02:10
let encoder: JSONEncoder = .init()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let data = try encoder.encode(value)
XCTAssertEqual(
Copy link
Contributor

Choose a reason for hiding this comment

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

Please make it consistent with the other tests by using the utility functions that does the encoding/decoding and equality checks.

var dict: OpenAPIObjectContainer = .init()
}

do {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this extra do scope here?

var dict: OpenAPIObjectContainer = .init()
}

do {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here about the do scope.

@czechboy0
Copy link
Contributor

Thanks, please also update the PR description to reflect the remaining changes.

Copy link
Contributor

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

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

Looks good overall, I'll open another PR bringing it to our style in a bit.

@czechboy0 czechboy0 removed the request for review from simonjbeaumont August 3, 2023 20:49
@czechboy0 czechboy0 merged commit b9019d9 into apple:main Aug 3, 2023
@czechboy0
Copy link
Contributor

Fixing up the style: #33

@czechboy0 czechboy0 added the semver/none No version bump required. label Aug 4, 2023
@rboyce rboyce deleted the anyValueContainer branch September 3, 2023 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/none No version bump required.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants