-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for XML request body and response body #556
Comments
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 Once that's in place, your plan above with extending the 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 |
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 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. |
### Motivation See [apple/swift-openapi-generator#556](apple/swift-openapi-generator#556) for more. ### Modifications Add converter methods for encoding and decoding XML request and response body. Add `CustomCoder` protocol, allows to use other `Encoder` and `Decoder` for other `content-type` body. User can define custom coder, and assign a custom coder to a specific content-type within `Configuration.customCoders` dictionary. ### Result It's now possible to define custom encoder and decoder for supported content-type. ### Test Plan Added converter methods are tested with a mock custom coder for `application/xml` content-type. To avoid adding a dependency to a XMLCoder like [CoreOffice/XMLCoder](https://github.com/CoreOffice/XMLCoder), mock custom coder uses JSONEncoder and JSONDecoder. Encoding and decoding to XML are out of scope of the tests, because encoding and decoding logic must be provided by user through custom coder implementation. --------- Signed-off-by: Ugo Cottin <[email protected]> Co-authored-by: Honza Dvorsky <[email protected]>
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 |
Hi @ugocottin, do you expect to still be able to finish this support, or should I unassign it for now and let someone else pick it up? 🙂 |
Hi @ugocottin, pinging one more time - are you planning to finish the generator implementation, or should we unassign it and have someone else pick it up? 🙂 |
Hi @czechboy0, sorry for the delay. I will finish the implementation this week |
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
.For
swift-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
The text was updated successfully, but these errors were encountered: