swift-openapi-generator icon indicating copy to clipboard operation
swift-openapi-generator copied to clipboard

Client configuration dateTranscoder: .iso8601WithFractionalSeconds is not used with asDecodedServerSentEventsWithJSONData

Open ChristophM2000 opened this issue 11 months ago • 1 comments

Description

We do connect to a server api that sends dates with fractional seconds, so we set the dateTranscoder to .iso8601WithFractionalSeconds on the configuration struct we do pass into the Client. This works perfectly for api calls that directly return a json result.

But some operations use server send events. Here we use the stream.ok.body.text_event_hyphen_stream.asDecodedServerSentEventsWithJSONData(of: Components.Schemas.FooModel.self) function which fails due to an decoding error of the date because of the fractional seconds. If I use a custom json decoder via the (of: Components.Schemas.FooModel.self, decoder: jsonDecoder) with a custom dateDecodingStrategy that uses formatOptions .withFractionalSeconds it also works here.

But shouldn't the asDecodedServerSentEventsWithJSONData function use the dateTranscoder configuration we set on the client by default?

Reproduction

You need a server api that sends server send events with a return type that contains a date formatted with fractional seconds.

let stream = try await underlyingClient.GetMessageStream() for try await event in try stream.ok.body.text_event_hyphen_stream.asDecodedServerSentEventsWithJSONData(of: Components.Schemas.FooModel.self) { // event.data }

Package version(s)

. ├── swift-openapi-generatorhttps://github.com/apple/[email protected] │ ├── swift-algorithmshttps://github.com/apple/[email protected] │ │ └── swift-numericshttps://github.com/apple/[email protected] │ ├── swift-collectionshttps://github.com/apple/[email protected] │ ├── openapikithttps://github.com/mattpolzin/[email protected] │ │ └── yamshttps://github.com/jpsim/[email protected] │ ├── yamshttps://github.com/jpsim/[email protected] │ └── swift-argument-parserhttps://github.com/apple/[email protected] ├── swift-openapi-runtimehttps://github.com/apple/[email protected] │ └── swift-http-typeshttps://github.com/apple/[email protected] └── swift-openapi-urlsessionhttps://github.com/apple/[email protected] ├── swift-openapi-runtimehttps://github.com/apple/[email protected] │ └── swift-http-typeshttps://github.com/apple/[email protected] ├── swift-http-typeshttps://github.com/apple/[email protected] └── swift-collectionshttps://github.com/apple/[email protected]

Expected behavior

asDecodedServerSentEventsWithJSONData function should use the dateTranscoder configuration of the client by default

Environment

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1) Target: arm64-apple-macosx15.0

Additional information

No response

ChristophM2000 avatar Jan 24 '25 09:01 ChristophM2000

The configuration on the client/server isn't available in these helper extensions on AsyncSequence. So the way to customize the decoding is what you found: pass a custom decoder.

I'm open to ideas how to improve this. Some half-baked ones that come to mind:

  1. offer this utility as a method on the configuration struct, and require the input async sequence to be provided as a parameter
  2. attach a copy of the configuration on the HTTPBody somehow
  3. put the current configuration in some task local, unclear what the scope would be
  4. ?

czechboy0 avatar Jan 24 '25 10:01 czechboy0