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

type: date format is not formatted correctly in c# #491

Closed
danzel opened this issue Oct 2, 2017 · 6 comments
Closed

type: date format is not formatted correctly in c# #491

danzel opened this issue Oct 2, 2017 · 6 comments

Comments

@danzel
Copy link
Contributor

danzel commented Oct 2, 2017

When a "type": "date" property is present in a schema we can generate a DateTime or DateTimeOffset field.
However, when when serializing this field it is not turned in to a date, it is turned in to a dateTime.

ref: https://swagger.io/specification/#document-structure-12
date should be a full-date, which is yyyy-MM-dd in c#.

To fix this, you can add [Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))] to the Property and define that converter as:

	public class DateFormatConverter : Newtonsoft.Json.Converters.IsoDateTimeConverter
	{
		public DateFormatConverter()
		{
			DateTimeFormat = "yyyy-MM-dd";
		}
	}

Trying to figure out how to fix this myself, but... there is a lot of code in here :)

@danzel
Copy link
Contributor Author

danzel commented Oct 2, 2017

Figured out the typescript version, but I'm not sure how to implement it for c#, some help or advice here would be greatly appreciated. Thanks!

@RicoSuter
Copy link
Owner

I dont like to add more helper classes to the output (i.e. DateFormatConverter) but it seems that this is the only solution. The question is whether we generate this as a global type (potential name collisions) or as inline time in each class (more code, repetitions).

@danzel
Copy link
Contributor Author

danzel commented Oct 2, 2017 via email

@RicoSuter
Copy link
Owner

RicoSuter commented Oct 2, 2017

Yes, this is exactly the problem (I think its a rare case, because it only happens when generating multiple clients from multiple swagger specs).

I still prefer the "single utility class" option because we already have this problem with the JsonInheritanceConverter. However we should add the option GenerateDateFormatConverter (default: true) so that you can disable the generation for all but one generation (we also have GenerateJsonInheritanceConverter).

This way it works the same for all these utility classes.

What do you think?

@danzel
Copy link
Contributor Author

danzel commented Oct 2, 2017

Ah yeah, that seems like a good way to do it.
I'll try take a look at this one tomorrow.

(Reminder to self: NJsonSchema.CodeGeneration.CSharp/Templates/ClassTemplate.tt)

@RicoSuter
Copy link
Owner

The actual class is rendered in FileTemplate.tt

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

No branches or pull requests

2 participants