Skip to content
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

fix(zod): treat additionalProperties keyword #1443

Merged
merged 4 commits into from
Jun 11, 2024

Conversation

ixth
Copy link
Contributor

@ixth ixth commented Jun 8, 2024

Status

READY

Description

additionalProperties: true isn't treated the right way during schema parsing, which results in runtime error during generation. Since #972 is stalled, decided to dig into it myself.

Related PRs

Previous attempt to fix this:

branch PR
CxGarcia:fix/zod-additional-properties link

@ixth
Copy link
Contributor Author

ixth commented Jun 8, 2024

Also, couldn't help but fix few typos here and there.

strict,
),
generateZodValidationSchemaDefinition(
isBoolean(schema.additionalProperties)
Copy link
Contributor Author

@ixth ixth Jun 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actual fix which I'm not sure about: since additionalProperties: true is just an alias for additionalProperties: {}, I decided to treat it as latter. Should definition be faithful to original scheme?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to understand that if it is simply true, it is determined that the additional property does not exist?

Copy link

@ixth-ind ixth-ind Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understood from the OpenAPI docs, it's the opposite:

If the dictionary values can be of any type (aka free-form object), use additionalProperties: true:

https://swagger.io/docs/specification/data-models/dictionaries/#free-form

So, additionalProperties: true is equivalent to additionalProperties: {}, which means that any type of property values allowed. Basically, it means it's Record<string, any>. additionalProperties: {} works fine, btw, it's just additionalProperties: true that's broken.

UPD. Oops, used work account. Comment is still relevant, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I understood.

@ixth ixth force-pushed the fix/zod-additional-properties branch from 90f72ac to b4692cc Compare June 9, 2024 05:50
@melloware melloware added the zod Zod related issue label Jun 9, 2024
@melloware melloware added this to the 6.31.0 milestone Jun 9, 2024
@melloware
Copy link
Collaborator

@TommoLeedsy can you review this also? Its related to other recent zod changes

@melloware melloware requested a review from soartec-lab June 9, 2024 11:24
Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ixth
I have asked a simple question, please answer.

strict,
),
generateZodValidationSchemaDefinition(
isBoolean(schema.additionalProperties)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to understand that if it is simply true, it is determined that the additional property does not exist?

@melloware
Copy link
Collaborator

@ixth in this other PR that has been open for a long time: #972 he used zod.passthrough()? not sure if your solution is better than what he was trying to do?

@ixth
Copy link
Contributor Author

ixth commented Jun 10, 2024

@melloware
Can't tell right away, have to check it more thoroughly, but I assume these two points:

  1. additionalProperties: {} works just fine, additionalProperties: true is an alias for it, and must behave the same way, i.e. generate z.record(z.string(), z.any())
  2. z.passthrough() is a parsing directive, and it doesn't affect inferred types, so it won't work in cases when I want to use z.infer on generated type. I might add a test case for that.

@melloware
Copy link
Collaborator

Awesome thanks for the answers @ixth

@soartec-lab
Copy link
Member

soartec-lab commented Jun 11, 2024

@melloware @ixth

Hey, it seemed to me that this response wasn't exactly the same as what you're trying to do with #972.
In other words, it is possible to do both. Here we will modify it so that additionalProperties works, but in #972 we will use zod to make it easier to handle properties, right? What do you think?

@soartec-lab soartec-lab self-assigned this Jun 11, 2024
@ixth
Copy link
Contributor Author

ixth commented Jun 11, 2024

In other words, it is possible to do both.

As soon as additionalProperties: true will result in z.record(z.string(), z.any()), there will be no need to add .passthrough(): any fields with string key will be allowed and passed through.

I might not get the whole picture. What cases am I missing?

@soartec-lab
Copy link
Member

@ixth
I got it. It looks good. There is no specific case, it's just that I have doubts, so it's okay.

Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your response. I think it's a good change, but I'd like to wait for some comments by #972, so I'll merge it in a few days.

@melloware
Copy link
Collaborator

@soartec-lab i am going to merge as this is the correct solution and @ixth pointed out why the previous PR was actually incorrect.

@melloware melloware merged commit 257a21f into orval-labs:master Jun 11, 2024
0 of 2 checks passed
@soartec-lab
Copy link
Member

Gotcha!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
zod Zod related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants