microsoft / kiota-abstractions-dotnet

Abstractions library for the Kiota generated SDKs in dotnet
https://aka.ms/kiota/docs
MIT License
24 stars 21 forks source link

Serialization/Deserialization of Date format #178

Closed hayama600 closed 4 months ago

hayama600 commented 5 months ago

I have Request class with Date datatype property. When sending in request Date api expects it in format DD.MM.YYYY, and sending back Date in that format. Is it possible to do that without changing generated code?

    var client = new PostsClient(httpClientRequestAdapter);

    var body = new Request()
    {
        PromjenaOd = new Microsoft.Kiota.Abstractions.Date(2020, 01, 01),
    };
    var response =  await client.Shema.GetStates.PostAsync(body,null);
    response.Dump();
    }
andrueastman commented 5 months ago

Thanks for raising this @hayama600,

Just to confirm, are you observing an issue during serialization or deserialization or both? I believe the format is currently yyyy-MM-dd which should mainly be an issue on serializing as the parser should be more lenient on deserialization.

You can probably post the correct date format in the meantime using the additionalData

var body = new Request();
body.AdditionalData["promjenaOd"] = new Date(2020, 01, 01).DateTime.ToString("DD.MM.YYYY");

If the client is generated from Kiota, are you able to share the openApi description used to generate the client, I suspect we should be able to use the date format in the description to help generate something that works better in the long run.

baywet commented 5 months ago

@hayama600 I suggest you change your description to use another format name (like us-date or so) The date format is reserved to RFC3339 full date so YYYY-MM-DD

hayama600 commented 5 months ago

@hayama600 I suggest you change your description to use another format name (like us-date or so) The date format is reserved to RFC3339 full date so YYYY-MM-DD

I received a YAML file from a Croatian company, and they've used Croatian formats for dates and numbers. Unfortunately, I can't modify the expected date format in the API. I'm aware that I can manually replace each date with a string in the Kiota-generated model, but it would be much more convenient if I could provide custom serialization and deserialization for date and number formats.

andrueastman commented 5 months ago

I think we could probably provide a means of overriding the ToString method by allowing the passing of an extra format parameter to the Date class(constructor or another ToString overload) and that would allow serializing the date in a custom fashion. Thoughts @baywet?

However, as highlighted, as the date type is reserved, a valid description should probably not use the type, and the generated type by Kiota would probably not be the Date type but a string type with a format. Any chance you can share what the description has for the type, does it have a format property as well @hayama600?

baywet commented 5 months ago

@andrueastman is there anything preventing the client application from deriving from the type today and doing that already?

andrueastman commented 5 months ago

I had a similar thought, but the Date type is a struct so it really can't be inherited from.

baywet commented 5 months ago

Yep, that's pretty much the only option here. And it should work across all languages. @hayama600 do you think you'd be willing to send a pull request for that?

  1. add an optional format parameter here, default the value to what we have here
  2. assign the parameter to a new "Format" property.
  3. add the format optional parameter to this constructor as well and pass it to the other one.
  4. use the Format property in the toString method (throw if null or empty)
  5. add a couple of unit tests in the tests project
  6. do the same for the Time struct

Let us know if you have any question!

hayama600 commented 5 months ago

Yep, that's pretty much the only option here. And it should work across all languages. @hayama600 do you think you'd be willing to send a pull request for that?

  1. add an optional format parameter here, default the value to what we have here
  2. assign the parameter to a new "Format" property.
  3. add the format optional parameter to this constructor as well and pass it to the other one.
  4. use the Format property in the toString method (throw if null or empty)
  5. add a couple of unit tests in the tests project
  6. do the same for the Time struct

Let us know if you have any question!

I will create a pull request next week; I'll reach out if I have any questions.

andrueastman commented 5 months ago

@hayama600 Any chance you can still take a look at this?

microsoft-github-policy-service[bot] commented 5 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

microsoft-github-policy-service[bot] commented 4 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

microsoft-github-policy-service[bot] commented 4 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.