-
-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Polymorphic fields in contracts #530
Comments
To enable a "oneOf" type functionality in the docs you can just register "returning" with each variant in turn. |
See: |
The "oneOf implementation" only works on the root level, as in a fully different object returned from the handler. My example provided shows a different use case, as in a field being polymorphic. If I'm missing anything that would solve my example I'd be happy to hear. |
Ah - sorry. There might be a way to implement this with some type of annotation (on the field) which then gets picked up and processed by the renderer. But it's going to be pretty complex to write - you'd need to reimplement AutoJsonToJsonSchema. |
As an update, we now have the ability to discriminate between multiple examples by providing a "prefix". This is limited in that it adds this prefix to all objects created in this tree (and hence there are duplicates in the schema - one for each prefixed type tree), but it does allow some expression of polymorphic examples in the generated APIs |
@henrist did you resolve this somehow? would you have a full example how you solved this? |
I don't think we did anything specifically to solve this other than just adhering to the constraints and limiting our schema. Unfortunately I no longer have access to the code base that had this case. |
As far as I can understand the current implementation of contracts has these limitations:
We have a use case where we use sealed classes to represent different models for a given field. But this cannot be used in the API since we cannot produce a correct contract for it. Our current workaround is to implement it as different fields, but this causes confusing example data since in reality the example is not a valid model (businesswise) - as the fields are exclusive.
It would be nice if http4k contracts could supported polymorphic fields, at least via sealed classes, by using
oneOf
/anyOf
from the OpenAPI spec.Example of what I want to achieve:
The example also uses
JsonTypeInfo
to produce a discriminator field named "type", which is not supported by http4k. Ideally it should be mapped as a discriminator field in the schema.The schema created by the current example under 3.277 (which incorrectly only models Company as the owner type):
Expand
The text was updated successfully, but these errors were encountered: