JKHeadley / rest-hapi

🚀 A RESTful API generator for Node.js

Home Page:https://resthapi.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for polymorphism (discriminators)

adupre opened this issue · comments

I need to be able to leverage the polymorphism features of Mongoose (discriminators) to create schemas that inherit from one another (example: Contract, SalesContract, PurchaseContract)

Enhance the model and api generators to support discriminators. Leverage Swagger's support of OneOf for generation of api and documentation.

There are unfortunately no acceptable alternatives at the moment.

Btw, thank you for this awesome library, it's a true time saver.
Cheers,
Anthony

Thanks! Glad to hear it!

I will try to look into this soon and see how feasible it is.

@adupre I added some initial support for discriminators. You can test it out with this repo:
https://github.com/JKHeadley/rest-hapi-demo/tree/feature/discriminators

Some notes:

  • Does not use swagger OneOf since it is not supported in hapi-swagger, however most of the documentation should be taken care of by rest-hapi directly.
  • The discriminatorKey property is automatically queryable, which could result in useful filtering of parent models. Ex: with parent model: contractand child models: salesContract and purchaseContract, the following query would return all child model documents but not parent model documents (assuming the discriminatorKey is kind):
    http://localhost:8080/contract?kind=salesContract&kind=purchaseContract

Please let me know what you think.

@adupre please let me know if you have any input here. I will try to integrate this feature with the next minor version.