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

z.nullable does not use schema references #141

Closed
FelixZY opened this issue May 18, 2023 · 3 comments
Closed

z.nullable does not use schema references #141

FelixZY opened this issue May 18, 2023 · 3 comments

Comments

@FelixZY
Copy link

FelixZY commented May 18, 2023

I have a schema like this:

  parentVenue: z.nullable(ProfileReferenceDTOSchema).openapi({
    description:
      'A venue to which this venue belongs.\n\nFor example, the theoretical venue "Dance Palace" may be the parent venue of the venues "Indoors" and "Outdoors"',
  }),
  childVenues: z.array(ProfileReferenceDTOSchema).openapi({
    description:
      'The venues that belong to this venue.\n\nFor example, the theoretical venues "Indoors" and "Outdoors" may be child venues of the venue "Dance Palace"',
  }),

Which causes an annoying difference when rendered:
Screenshot from 2023-05-18 21-37-27

It seems the reason for this is that the openapi spec generated for z.array uses

"type": "array",
"items": {
  "$ref": "#/components/schemas/profiles.ProfileReferenceDTO"
},

whereas the openapi spec generated for z.nullable does not use $ref (tested for openapi 3.0.3 and 3.1.0):

"type": "object",
"nullable": true,
"properties": {
  "id": { "type": "string", "format": "uuid" },
  "name": { "type": "string" },
  "type": {
    "type": "string",
    "enum": ["organization", "individual", "venue"]
  },
  "images": {
    "$ref": "#/components/schemas/storage.ImageDTO"
  }
}

Would it be possible to get z.nullable to generate a $ref as well? Perhaps using oneOf?

@FelixZY FelixZY changed the title Using z.nullable causes schema reference to not be included z.nullable does not use schema references May 18, 2023
@AGalabov
Copy link
Collaborator

@FelixZY thanks for bringing this up. nullables are tricky since they behave differently based on the chosen OpenApi version (3.0 or 3.1). I'll try and take a look asap, but right now we are hoping to release a new major version and I'll probably have to prioritize the feature requests of the other open issues slightly higher. Sorry for that, and I'll come back to you as soon as I can. In the mean time feel free to give it a go and open a PR yourself.

@AGalabov
Copy link
Collaborator

@FelixZY I took a look at your example. And actually we already support that. The result has allOf. However we've only currently done the required support on schema.nullable() and not on z.nullable(schema). I'll open a PR soon but you can also fix this on your end (functionally there shouldn't be any difference for you).

FelixZY added a commit to dansdata-se/api that referenced this issue May 28, 2023
There is a bug in `asteasolutions/zod-to-openapi` where
`z.nullable(schema)` can cause the full object to be output instead of
the expected `$ref`.

Using `schema.nullable()` was proposed as a workaround:
asteasolutions/zod-to-openapi#141 (comment)
@AGalabov
Copy link
Collaborator

AGalabov commented Sep 9, 2023

This turned out to be impossible with the current way of implementing such methods. Because of that I've just updated the README.md file with a note for it (that schema.nullable() should be used instead)

@AGalabov AGalabov closed this as completed Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants