Proposal (needs some cleaning up) for uniquely naming codecs #60
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.
I occasionally have Autodocodec definitions like this:
but the problem is, each one of those schemas is going to have the name
"Alice"
. That is,Alice Int
is going to have the same schema name asAlice Char
, namely they're both going to be called"Alice"
.So then I use
servant-openapi
to generate the OpenAPI JSON.But the problem is, if seems like
servant-openapi
gets two types with the same name, it just overwrites the latter with the former, which means all the references to the former are replaced by the latter, resulting in the wrong schema definitions and any generated code from this OpenAPI not matching the ToJSON/FromJSON serialiser definitions.Admittedly I think this behaviour of
servant-openapi
just silently ignoring and overwriting clashing names is far from ideal, but in anycase, even ifservant-openapi
did behaviour better in these cases and actually error, one would still need to differentiate between these different types.So what resolve this issue is something like this:
(note the
nameOfHasCodec @a
)in this PR I have given a proposed implementation of
nameOfCodec
, andnameOfHasCodec
, which takes a type parameter instead of an actual codec. What I'm looking for is:object
) to be respected.So I've came up with a quick implementation in this PR. The actual naming convention is customisable via a record, but I've given an example convention. I imagine in most real scenarios one will find an
ObjectOfCodec
orReferenceCodec
somewhere in the codec chain which will give an explicit name, but we need to cover all cases. Also in these cases laziness means we don't even calculate a generated name (asfromMaybe
is lazy).Let me know what you think, and whether you think sort of thing is appropriate for the Autodocodec library, or whether you'd rather have it separate? I know this PR needs some work but I'd just thought I'd sound you out on your thoughts before going further.