Open ugocottin opened 1 month ago
Hi @ugocottin,
yes XML is officially supported by the OpenAPI specification, so it'd be reasonable for us to support it here.
Since we already generate Codable
types, I think the main piece of work would be to create an XMLEncoder
and XMLDecoder
that can work with Codable
types, and then we can update the generator.
Once that's in place, your plan above with extending the Converter
, adding a CodingStrategy
is exactly right.
A heads up - this would need to be contributed by you, and we'd help steer the code through pull request review.
If that sounds good, go ahead! 🙏
Oh I missed that you already linked to such encoder/decoder in https://github.com/CoreOffice/XMLCoder.
The only concern there is that it's a dependency that isn't API-stable.
I wonder if we could somehow offer this as explicitly opt-in, where only adopters who want XML would include that dependency.
Maybe some new optional conversion methods on OpenAPIRuntime.Configuration
, able to convert between HTTPBody
and Codable
using a custom encoder/decoder.
Hello @czechboy0
Thanks for the feedback. I've submitted a first pull request to apple/swift-openapi-runtime#102.
As you suggested, I added the possibility to define custom encoding / decoding behaviour through OpenAPIRuntime.Configuration
, with CustomCoder
protocol.
Thanks!
Looks good - feel free to open a PR for the generator as well (even if it won't pass until the runtime change lands and is released), it's easier to review both changes together.
Ok @ugocottin, the runtime changes landed in swift-openapi-runtime 1.4.0, so please bump the dependency of the generator's Package.swift and you should be able to finish the generator work.
As part of the generator PR, it'd be great to also add an example project that shows how to bring your own XML encoder/decoder and plug it all together (see the Examples directory).
Hi @czechboy0, thanks for letting me know. I will make a pr soon
Motivation
I want to be able de generate client and server from an OpenAPI specification, for an API which use XML as content type for request and response body.
Currently,
application/xml
contents for request and response are not generated byswift-openapi-generator
, and onlyOpenAPIRuntime.HTTPBody
enum associated value is available for xml content.A Workaround is to use an external XML encoder and decoder, like CoreOffice XMLCoder, and make the encoding from / decoding to
HTTPBody
.Proposed solution
One quick solution could be to implement a new
CodingStrategy
for xml inswift-openapi-generator
. Forswift-openapi-runtime
, we could add the following methods toConverter
:Encoding / Decoding logic can be implemented into these methods, with an XML encoder / decoder. As
Converter
haveencoder
anddecoder
properties as typeJSONEncoder
andJSONDecoder
, we could:encoder
tojsonEncoder
anddecoder
tojsonDecoder
, and instantiate axmlEncoder
andxmlDecoder
inConverter
init.XMLEncoder
orXMLDecoder
in call of methods above.Example:
Alternatives considered
I found that
Converter
struct is not suited for adding support of new content type. With the proposed solution, theConverter
struct will have types of<#HTTPMediaType#>Encoder
and<#HTTPMediaType#>Decoder
, while not necessarily needed. For XML only API,Converter
still need aJSONEncoder
andJSONDecoder
.I thought that, maybe, and I would like to have your opinion on this, we must implement a
Converter
for a specific http media type, like Vapor is doing withContentEncoder
andContentDecoder
. Each http media type would be assigned to a specific Converter.What's your thoughts about this?
Additional information
No response