codegen: Semiauto schema derivation #3671
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This would address two issues with the code currently being generated:
When using the codegen, until now I've had to add in a sourceGenerator hack to replace
import sttp.tapir.generic.auto._
with aimplicit def schemaForAny[T]: sttp.tapir.Schema[T] = sttp.tapir.Schema.any[T]
stub, since I was hitting javac class size limits (also, compilation was taking a while to fail - I guess because the leaf schemas were being regenerated for everything that needed it? I don't really know enough about how the scala compiler handles memoization of implicit resolution to be sure of that though...). ANYWAY:This also means that the schemas for ADTs should be more* correct (automatic derivation didn't know about discriminators or lack thereof).
This pr will create explicit schemas for every class model, and emit them to
TapirGeneratedEndpointsSchemas
(orTapirGeneratedEndpointsSchemas1
,TapirGeneratedEndpointsSchemas2
etc if split over multiple files). The maximum number of schemas per file is configurable via the new optionopenapiMaxSchemasPerFile
. Experimentally, a value of 400 worked fine for me, so that's the default.Generating the correct schemas for objects and maps made me aware of the fact that I'd mucked up circe json serdes for object schemas with fields that were, themselves, inlined object or map declarations; so this pr also fixes that.
* The 'more' qualifier is here because I don't know how to mark an array field as required in the generated JSON schema, so round-tripping the swagger example I've touched fails to retain semantics. I tried adding
@sttp.tapir.Schema.annotations.customise((_: sttp.tapir.Schema[_]).copy(isOptional = false))
to Seq fields but that didn't change the output...