Closed C-ra-ZY closed 7 months ago
@C-ra-ZY Thanks for finding the flaw in handling the .passthrough()
for empty objects, but I don't this the proposed fix is a valid solution as it should not remove any required
fields from the user spec.
As it's specified in OpenAPI spec
allOf
takes an array of object definitions that are validated independently but together compose a single object.
The visual doc in https://editor.swagger.io/ has a misleading feature that it tries to merge allOf
objects as I mentioned in issue 257 and it actually fails to display required
but unknown
property(that seems just a bug)
A better fix might be to have an ad hoc case handling of required
only objects by removing the z.object({})
part and generating z.object({ name: z.unknown() })
directly?
Describe the bug We are attempting to modify the behavior of openapi-zod-client in pull request 258, with the expectation of correctly handling schemas within the allOf array that only contain the required field. The goal is to generate the correct type annotations for required fields and the corresponding Zod schema, like https://editor.swagger.io/ does(It can even automatically eliminate properties that cannot be resolved or located, show in the pic: abc).![image](https://github.com/astahmer/openapi-zod-client/assets/12962297/674843aa-89e4-4ad8-9beb-bc0c771bcfc7)
However, after testing, it has been observed that the current code is not yet able to fully address this issue. and more of that, pull request 258 didn't take types generating into account. which should be supplemented.![image](https://github.com/astahmer/openapi-zod-client/assets/12962297/0c1a357e-200d-477b-9920-6ea3717d4dcb)
Minimal reproduction A simplified schema definition to reproduce this is like:
The generated client code:
but the code will trigger an error:
and the schema fail to require correctly property(even when delete the unnecessary .passthrough()):
![image](https://github.com/astahmer/openapi-zod-client/assets/12962297/ee084cdb-e814-40aa-831e-5cf2b4978474)
Expected behavior we can generate types and schemas correctly like https://editor.swagger.io/.
Additional context guys can easily check issue reproduction at https://github.com/C-ra-ZY/openapi-zod-client/tree/test-on-first-fix run
and see the generated test.client.ts