Directly generate ToSchema
instance from .proto
AST
#63
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.
The motivation for this change is to fix the mismatch between the Swagger API
declared in
ToSchema
instances and the actual API served by theFromJSONPB
and
ToJSONPB
instances.The difference between the two was in casing conventions: the JSONPB API was
preserving the original case of the fields from the
.proto
API, whereas theSwagger API was camel-casing the declared fields.
The reason they differ was that they were going through two separate code
pathways. In the case of JSONPB the
FromJSONPB
andToJSONPB
instances werebeing generated directly from the
.proto
AST without any intermediate steps:... and that code generation step preserved the original case.
However, the
ToSchema
instance was generated indirectly via GHC generics:... and the code generation step for the Haskell data type was not preserving
case.
After this change the code generation directly generates the
ToSchema
instance:
Some alternative approaches that I considered and rejected:
Change the Haskell data type generation to preserve the original case instead
of camel-casing Haskell field names
I rejected this approach because it is highly disruptive to downstream code
and also generates ugly field names
Customize the
ToSchema
instances derived viaGeneric
s to convert camelcase to snake case
I rejected this approach because it is not correct if the original
.proto
field names were already camel-cased. This would then lead to the opposite
problem: declaring a snake-cased field which was actually camel-cased
I tested this downstream on our large internal
.proto
files. The onlydifference in the generated Swagger is that the casing for declared fields is
fixed and the generated Haskell code still compiles without any changes to the
client packages.
I also added some simple golden tests for simple visual inspection of what the
generated Swagger looks like.