aj-foster / open-api-generator

Open API code generator for Elixir
MIT License
97 stars 13 forks source link

Add @enforce_keys with required schema fields #16

Closed xadhoom closed 2 months ago

xadhoom commented 1 year ago

Right now the generated schemas ignore the fact that a field can be required or not.

This can be easily reflected into the generated structs in order to force the library user to properly populate schemas on APIs payloads.

While is ok from a client perspective, I see one drawback on response schemas: while the API server may declare certain response fields as required in the schema, nothing assures that it'll really populate them, thus the client implementor should in someway handle the situation when casting the response payload to a response struct.

Indeed is a bug on the API server, but in someway we should consider this scenario? What do you think?

aj-foster commented 1 year ago

After thinking about this for a while, I think we should allow clients to ignore the issue of "missing required field in the response from the API server." If we add @enforce_keys then the generated client will have no choice but to throw an error if something is missing, which may not be the right action.

Do you think that is reasonable? Perhaps we can give more information to the schema_use module so that clients can add @enforce_keys if they would like to.

Good work finding the union of the required keys. It is easy to forget that the schema may have multiple merged sets of fields.

aj-foster commented 1 year ago

I'd also love your feedback on #20 if you have the time.

aj-foster commented 2 months ago

Closing this for now. Feel free to open it again or start a discussion topic if you'd like to discuss more!