Closed codemedian closed 4 months ago
Thanks for raising this. It's quite likely that I've misunderstood the specification here, and will need to fix.
If you control the schema you're using, you could try using additional properties in the meantime:
SomeSchema:
additionalProperties: true
Should give you something like:
const s_some_schema = z.record(z.any()).optional()
const t_some_schema = {
[key: string]: unknown | undefined
}
Does that match what you'd expect the {}
schema to produce?
Reading some more, if I'm understanding it correctly then {}
should actually be equivalent to "any valid json value", so something like:
type JSON = {[key: string]: JSON} | Array<JSON> | string | number | null
Though to be honest I'd be tempted to just make this an any
or unknown
Hey,
thanks for the swift response. I'm generating the OpenAPI spec from Smithy so it's a little less flexible in the sense of adding arbitrary stuff. Could hack it in but for now I've resorted to just returning a string and changing the Content-Type header to application/json
which also does the trick.
I personally would be fine with any
, don't see a strong reason to make it more restrictive.
PS: Great project you're putting together, been looking for something like this and it's the only one I found that comes close to the quality one would expect comparing it to say Java or Kotlin.
I nearly have a fix for this ready in #204 - just have a few more tests to write, and documentation to update (should get done this weekend).
In addition to changing the interpretation of {}
, I've gone with adding a new CLI flag --ts-allow-any
that switches between using unknown
(default), or any
.
Should result in something like this:
Schema | Any | Unknown |
---|---|---|
{} |
any |
unknown |
{additionalProperties: true} |
any |
unknown |
{additionalProperties: false} |
EmptyObject |
EmptyObject |
{type: "object", additionalProperties: true} |
{[key: string]: any} |
{[key: string]: unknown} |
{type: "array", items: {}} |
any[] |
unknown[] |
If you have any feedback / spot anything I've missed please feel free to leave a comment on the PR or here and I'll try to address :pray:
Hey. Apologies, I've been away on a holiday and only just back. Have tested the fix and it works as I'd expect. Thanks for the quick turnaround time!
In OpenAPI, the Empty JSON type
{}
is considered a type that allows for any arbitrary JSON.This type gets converted to an
EmtpyObject
responder which always responds with an{}
element as opposed to the json version of the object passed to it.