tapir
tapir copied to clipboard
[FEATURE] Support customizing the generated OpenAPI JSON schema
After a discussion on softwaremill.community (https://softwaremill.community/t/differentiating-between-undefined-and-null/154/8), @adamw suggested opening an issue.
In short, this is a feature request for some way to customize what the generated OpenAPI JSON schema will be for a type.
In my particular use case, I needed the option to set "nullable" to true for a specific type, not for all optional types (that is already supported).
It is possible to manually, eg. with quicklens, modify the OpenAPI returned from OpenAPIDocsInterpreter(). toOpenAPI(...)
, but it's not very elegant.
Adam's suggestion from the discussion:
As for a more user-friendly fix, I think we should have an option to customise the final, generated JSON Schema (which is part of the OpenAPI object). We could set an attribute on the tapir-schema, with a function which would be called by the OpenAPI interpreter.
hi,
I think I have a similar issue, I'll check the suggested workaround, but I also would like to mention my case here, to add it to the context:
I have a Scala type List[Option[T]]
let's say it's List[Option[Int]]
, which in JSON becomes [1, 2, null, 4]
At the moment when deriving Schema[List[Option[Int]]]
- it gets compiled into OpenAPI just as array<integer>
, which then gets incorrectly interpreted by clients and results into runtime errors.
T.b.h. I'm not even 100% sure what's the right way to express it in the OpenAPI schema, but I think it's probably something like this:
UPDATE:
I've achieved the expected result by injecting a key-word into the Schema[List[Option[T]]]
(I used title
for it) and then capturing this key-word in the generated OpenAPI
and updating the schema with schema.items.nullable = true
:
UPDATE 2:
I've found out that there is OpenAPIDocsOptions.markOptionsAsNullable
which already does the right job when switched to true.