-
Notifications
You must be signed in to change notification settings - Fork 149
Description
Today, one can only use _StringConvertible types in places like query parameters, headers, and so on.
This works well for native primitive types, like String, Int, Double, and Bool; and we also made it work for string-based generated enums (which are essentially strings with an extra constraint).
However, when used with, for example, a oneOf that is either a string or an int, this doesn't work, as we generate an enum with associated values, and even though all the underlying cases are themselves _StringConvertible, the oneOf itself isn't. That prevents adopters from doing things that are legal in OpenAPI, and can be pretty useful (an example is the Weather.gov OpenAPI doc described in #135).
A solution to this could look as follows:
- if all subschemas of a oneOf/allOf/anyOf are _StringConvertible (we have util functions in the generator already to check this), add the
RawRepresentableand_AutoLosslessStringConvertibleconformances on the type itself as well - this will require generating a custom
init(rawValue:)initializer and arawValueproperty getter, but we also already did that for string-based enums, so we can take inspiration there - note: in the
rawValueproperty getter, we should dynamically check theundocumentedcontents of theOpenAPIRuntime.OpenAPIValueContainer, and if it's itself convertible to string, return it, but otherwise throw an error – this is for cases when e.g. an object was provided, we don't want to stringify it
Once this is in place, compositions of _StringConvertible types will also be _StringConvertible, and will be usable in those contexts, especially in query and header parameters.