Is there a way to define fields in the schema should be excluded from docs? (feature flagging fields) #172
Replies: 2 comments 1 reply
-
|
Hi @narutosstudent , thanks for using the library and the suggestion. I'm converting this thread to a discussion since it's not really an issue. I agree that there are cases where you don't want to expose docs for a particular field. At the same time, I'm wondering about the behavior when the field is required. For example, in your screenshot the field So there are two cases I see here for hiding fields:
However, for the second case I believe a better approach (that also handles required fields) is to dynamically switch the object itself instead of just hiding some of its properties. Something like this: const ShareInputSchema = z.object({
name: z.string()
});
const ShareWithDescriptionSchema = ShareInputSchema.extend({
description: z.string()
})
export const CreateShareRequestSchema = defineRequestSchema.with({
body: z.object({
data: z.object({
share: ( featureFlagEnabled ? ShareWithDescriptionSchema : ShareInputSchema ).openapi({ description: 'The share to create' })
})
})
})This way the input validator/parser is also correctly expecting a description when the flag is enabled and ignoring it when it's disabled. |
Beta Was this translation helpful? Give feedback.
-
|
Hi there! First of all, thank you for this project, it's working really well! I'm reopening the discussion because, while the proposed solution works, it doesn't fully address the need for a truly "hidden" feature, and I believe it could be a valuable addition to the project. I'm facing a similar issue where I have some internal optional fields that shouldn't be exposed or appear in the documentation. The ternary solution you suggested works, but it adds complexity to the code and makes it harder to read. A heavy validator can also be difficult to maintain, and I don't think it fully aligns with the concept of hidden fields. Regarding your first concern:
Would a solution like zod-to-openapi accepting a hidden or skipDocs field, and only hiding it if it's optional in the Zod schema, otherwise not hiding the field, be an acceptable compromise for you? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What are we trying to achieve?
We're trying to implement feature flagging into our API.
The fields and endpoints should exist (behavior & functionality).
However, if something is feature flagged, it should be excluded from the documentation, whether it'd be fields or an entire endpoint.
We figured out how to exclude entire endpoints, this focuses on excluding fields of an endpoint.
Question
Is there a way to define fields in the schema should be excluded from documentation when generating the docs?
We use
OpenAPIRegistryto register what should be documented.OpenApiGeneratorV31to generate the docs.Don't worry about
defineRequestSchemain the image below, we do some minor custom stuff.Is there a way to maybe have
skipas one of the options inopenapi({ })? Something likeskipDocs? Then the field would be excluded from the docs during generation and isn't to be found in the generatedopenapi.json.Thoughts
Something else you would propose for dealing with this? 😄
Maybe
skipDocsor some similar option could be a new feature.I'm happy to contribute to the project too! 🙌
Beta Was this translation helpful? Give feedback.
All reactions