fix(zod-to-json): handle anyOf with more than 2 items#195
fix(zod-to-json): handle anyOf with more than 2 items#195marcalexiei wants to merge 1 commit intoturkerdev:mainfrom
anyOf with more than 2 items#195Conversation
| Object.assign( | ||
| ctx.jsonSchema, | ||
| // If length is 2 it means there is only one element besides `null` | ||
| notNullableItems[0], | ||
| { nullable: true }, | ||
| ) | ||
| delete ctx.jsonSchema.anyOf |
There was a problem hiding this comment.
With this we ensure that all properties inside the non nullable item are kept (E.g. I have have added a minimum prop via z.number().min(0) in previous test case).
As alternative we could opt to something like this that should produce the same result:
ctx.jsonSchema.nullable = true
ctx.jsonSchema.allOf = notNullableItems
delete ctx.jsonSchema.anyOf|
@Bram-dc what do you think about these changes, do they look more correct to you? |
|
@kibertoad these change are limited to the handling of |
|
Could you test if my PR already covers these issues? |
|
There is no code involving |
|
Yes there is, in the recursive function. But I think it is not correct yet. It now transforms to an array with element: { |
|
This is the output of one of your tests: Which is valid OAS 3.0.3 right? Just not optimal. We could write a check to extract the nullable type from the elements. What other fields need this check?
|
I assume that if pass
Which is basically what it has implemented here. |
|
Would you mind helping me create a PR on top of mine? I am on holiday right now and don't have much time to work on this. |
|
Yesterday, I published a fork of this plugin under my own scope on npm. I also implemented a fix for OpenAPI 3.0 backward compatibility, based on your PR. You can check out the commit here: |
|
Would you mind still creating a PR here? It is easier to track the changes. You published 2 repos right? I think the conversion logic is good in the first repo. I prefer converting to 3.0 at the very last step however since that makes the code easier to maintain I think. the |
Last month I’ve already submitted PRs and opened issues on this repo, but unfortunately they’ve gone without feedback.
No, I published only https://github.com/marcalexiei/fastify-type-provider-zod into https://github.com/marcalexiei/fastify-type-provider-zod-fork is the fork of this repository and still contains the branches of the PRs I opened here.
Since OpenAPI is kind of a json schema I preferred to keep that logic inside
Yes, as I write in my previous comment: |
|
@turkerdev or @kibertoad please guys really needed. |
|
@RadovanPelka are you still gettibg issue with the latest version? this has conflicts and needs to be revised in the light of the latest code |
This improves
anyOfmanagement when overriding zod schemas to produces a OpenAPI schema,2 elements with one nullable
becomes:
{ "items": { "type": "string", }, "nullable": true, "type": "array", },>= 3 elements with one nullable
becomes:
{ "anyOf": [ { "items": { "type": "string", }, "type": "array", }, { "enum": [ "any", ], "type": "string", }, ], "nullable": true, }