-
Notifications
You must be signed in to change notification settings - Fork 129
Empty Dictionary fails to encode #525
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
Comments
Seems to be coming from the underlying Foundation.JSONEncoder, so might need to be filed on Foundation unless we identify that the generated code is doing something wrong. |
@czechboy0 I don't believe so, the following works fine: let empty = [String: [String]]()
let data = try! JSONEncoder().encode(empty)
print(String(decoding: data, as: UTF8.self)) // prints {} Could be related to something going on here? |
Actually I wonder if I've done something wrong in my spec definition as instead of transforming into a dictionary ( What is the correct way to have a Swift Dictionary type generated? |
A freeform JSON-based object? That'd be just: type: object If you'd like a dictionary of a specific type, for example integers, then: type: object
additionalProperties:
type: integer |
@czechboy0 That doesn't generate a dictionary property though, it gets put inside a wrapper struct which would make working with those types very clumsy. I must be doing something wrong, but this is what I'm seeing. For example this: Test:
type: object
properties:
dict:
type: object
additionalProperties:
type: integer Gets generated into this (simplified): struct Test {
struct dictPayload {
var additionalProperties: [String: Swift.Int]
}
var dict: Components.Schemas.Test.dictPayload?
} Rather than what I would expect (and what all other OpenAPI generators seem to do): struct Test {
var dict: [String: Swift.Int]?
} |
This is expected behavior. If you defined a property in addition to the additionalProperties in the schema for |
@grahamburgsma Does this address your issue or do you believe that there is something we need to change in the generator? |
@czechboy0 I understand why it generates the code as it does now, thanks for explaining that. The original issue as described still exists though. An empty dictionary response fails to encode. |
Gotcha, here's a fix, @grahamburgsma: apple/swift-openapi-runtime#103 |
### Motivation Fixes apple/swift-openapi-generator#525. Turns out that the mere act of creating a decoding container is meaningful and we skipped it as an optimization, causing JSONDecoder to fail for empty dictionaries when used in additional properties. ### Modifications Remove the extra guards that skipped creating a container, even when we already know there are no elements. ### Result No more failures when encoding empty dictionaries in additionalProperties. ### Test Plan Tested manually as this requirement seems to be coming out of JSONDecoder.
Description
Returning an empty dictionary as the response body fails to encode.
Error thrown:
EncodingError: invalidValue jsonPayload(additionalProperties: [:]) - at : Top-level jsonPayload did not encode any values.
Reproduction
Package version(s)
OpenAPIKit 3.1.2
swift-openapi-generator 1.2.0
swift-openapi-runtime 1.3.2
swift-openapi-vapor 1.0.0
swift-openapi-context 1.0.0
Expected behavior
The response should be encoded as:
Environment
The text was updated successfully, but these errors were encountered: