Skip to content
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 TimeSpan types in responses? #25

Open
Numpsy opened this issue Aug 15, 2021 · 2 comments
Open

Support for TimeSpan types in responses? #25

Numpsy opened this issue Aug 15, 2021 · 2 comments

Comments

@Numpsy
Copy link
Contributor

Numpsy commented Aug 15, 2021

Hi,

A question\request about possible support for TimeSpan types in generated F# clients.

Say that you have an ASP.Net Core API that returns an object with properties of type System.TimeSpan (e.g. https://github.com/Numpsy/OpenApiExample/blob/timespans/Common/SpecTestController.cs), using NewtonSoft.Json as a serializer, and you use NSwag to generate a schema for the server.

That will generate an OpenApi schema such as this

"schemas": {
  "TimeSpanHolder": {
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "timeSpan": {
        "type": "string",
        "format": "time-span"
      }
    }
  }
}

So the TimeSpan is represented as a string with the custom format time-span.
If you point NSwags own C# client generator at that, it will recognize the custom format and generate

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.5.2.0 (Newtonsoft.Json v11.0.0.0)")]
public partial class TimeSpanHolder 
{
    [Newtonsoft.Json.JsonProperty("timeSpan", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public System.TimeSpan TimeSpan { get; set; }
}

And I was wondering if you'd have any thoughts about doing the same in the Hawaii F# generator?

If you do the same thing but generate the server schema with Swashbuckle then you get this schema

"schemas": {
    "TimeSpanHolder": {
      "type": "object",
      "properties": {
        "timeSpan": {
          "type": "string",
          "format": "date-span"
        }
      },
      "additionalProperties": false
    }
  }

Which is similar except the custom format is date-span rather than time-span, because who needs consistency :-( (NSwags client generator seems to generate plain string properties for this one)

@Numpsy
Copy link
Contributor Author

Numpsy commented Aug 15, 2021

On a related note, at least some versions of the NSwag schema generator will generate a schema that specifies a type of 'string' for such properties when the server is set up to use System.Text.Json as the Json serializer, even though its default behavior is to serialize System.TimeSpan to an object - which causes 'fun' with a client that expects to deserialize a string - but that's a seperate issue (Hawaiis schema patching options are useful there :-) )

@Zaid-Ajaj
Copy link
Owner

Hi @Numpsy I think it is a good idea to add support for date-span (swashbuckle) and time-span (nswag) and convert them into TimeSpan directly. One problem I see here is the Fable serializer doesn't know how to handle the different formats and will require testing.

Speaking of testing, the codebase has become a bit large and I think that before I implement more features, I need to properly do integration testing: setting up a bunch of projects that generate clients from the latest Hawaii source and runs them against backends exposed from nswag/swashbuckle/odata, then assert their responses.

Long story short, it will take a while before I can start implmenting more features 😓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants