You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The protobuf oneof is used to specify that exactly one field should be set in any given set of fields, whereas OpenAPI doesn’t have a native way to express exactly one field MUST be set. Instead, OpenAPI’s oneOf describes alternative schemas, not mutually exclusive properties.
The JSON representation of a User looks like the following (notice that the phone_number doesn’t actually appear anywhere given the JSON representation):
There’s nothing in the schema that can help us enforce that exactly one of the properties is set, and you end up with a poorly generated TypeScript SDK that doesn’t enforce it (leading to a large number of bad request errors).
Unfortunately, the gRPC community is forced to accept this -- all OpenAPI converters simply map the oneof to its flattened set of fields rather than leaning on discriminated unions and the REST SDKs suffer.
Proposal
Fern could support a custom OpenAPI extension to group together particular fields in a oneOf to match the Protobuf oneof semantics.
Given that Fern already controls the JSON serialization layer, the generated interface could adapt the serializer to ensure that the correct JSON representation is used, i.e. it would omit the surrounding phone_number field and the inner type discriminator field:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Re: google/gnostic#251
Problem
The protobuf
oneofis used to specify that exactly one field should be set in any given set of fields, whereas OpenAPI doesn’t have a native way to express exactly one field MUST be set. Instead, OpenAPI’soneOfdescribes alternative schemas, not mutually exclusive properties.For example,
The JSON representation of a
Userlooks like the following (notice that thephone_numberdoesn’t actually appear anywhere given the JSON representation):{ "home_phone_number": "555-555-5555", }The OpenAPI ends up looking like the following:
There’s nothing in the schema that can help us enforce that exactly one of the properties is set, and you end up with a poorly generated TypeScript SDK that doesn’t enforce it (leading to a large number of bad request errors).
Unfortunately, the gRPC community is forced to accept this -- all OpenAPI converters simply map the
oneofto its flattened set of fields rather than leaning on discriminated unions and the REST SDKs suffer.Proposal
Fern could support a custom OpenAPI extension to group together particular fields in a
oneOfto match the Protobufoneofsemantics.Imagine something like the following:
With this, Fern could generate
Userinterface could be generated like the following:Given that Fern already controls the JSON serialization layer, the generated interface could adapt the serializer to ensure that the correct JSON representation is used, i.e. it would omit the surrounding
phone_numberfield and the inner type discriminator field:{ "home_phone_number": "555-555-5555" }Beta Was this translation helpful? Give feedback.
All reactions