mnahkies / openapi-code-generator

A code generation tool for openapi 3 / 3.1 specifications written in typescript, primarily aimed at generating typescript clients and server stubs. Other target languages may be added in future.
https://openapi-code-generator.nahkies.co.nz/
MIT License
20 stars 2 forks source link

OpenApi `anyJson` type results in an `EmptyJson` which doesn't respond with data #200

Closed codemedian closed 4 months ago

codemedian commented 4 months ago

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.

mnahkies commented 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?

mnahkies commented 4 months ago

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

codemedian commented 4 months ago

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.

mnahkies commented 4 months ago

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:

mnahkies commented 4 months ago

Released in v0.10.0

codemedian commented 4 months ago

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!