paperclip icon indicating copy to clipboard operation
paperclip copied to clipboard

Implement actix schema generator for tagged enums

Open Siphalor opened this issue 3 years ago • 4 comments

Resolves #97.

About

This pr implements schema generation for complex enums with serde's tag system.

Supported are all types of tagging (see here), except for the enum tuples. Tuple schema generation is currently not implemented correctly anyway, so that should go into another pr.

This pr currently uses anyOf instead of oneOf to cover some possible edge cases, where serde would just use the first variant that matches (especially visible in untagged enums).

To be able to implement this, this pr adds any_of and const to the internal schema representation.

Up for discussion

  • Usage of anyOf vs. oneOf as described above.
  • Currently the old enum system is kept for enums that use the default tagging (external) and that only have unit variants. This makes it effectively impossible to add descriptions for the enum variants. Possible workarounds:
    • Check for documentation on each variant and if present, go for the new any_of system.
    • Get rid of the old enum system and just use any_of for everything.

Other notes

  • The files that I worked on in this pr were incredibly huge, making editing more painful than it needs to be.
  • Let me know if there's any more documentation needed :)

Siphalor avatar Feb 12 '22 23:02 Siphalor

Hey @Siphalor, please correct me if I'm wrong, but anyOf is not part of openapi version 2?

tiagolobocastro avatar Feb 13 '22 13:02 tiagolobocastro

Sadly you seem to be right.

It seems like I was over-enthusiastic and got lost in the JSON Schema spec without noticing that OpenAPIv2 doesn't support the latest draft.

Well, maybe this is at least useful as a reference for the v3 implementation then :)

Siphalor avatar Feb 13 '22 15:02 Siphalor

Yes this is not all for naught, it is very useful indeed because we want to eventually output proper openapi v3. I added the initial dumb v2-v3 conversion but sadly for lack of time did not get to the next step.

tiagolobocastro avatar Feb 13 '22 23:02 tiagolobocastro

If you were so inclined, how about adding a new ApiV3Schema trait (eg in core/v3/schema.rs) containing these new additions?

tiagolobocastro avatar Feb 13 '22 23:02 tiagolobocastro