CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.11k stars 174 forks source link

DateTime is not represented properly in OpenAPI #260

Closed nahojd closed 2 years ago

nahojd commented 4 years ago

If I have a DateTime property in my Response class, it is not represented correctly in the generated openapi-document.

Since it is serialized as a string, it should be represented as

 { "type": "string", "format": "date-time" }

Instead, a very detailed schema is generated with all the internal properties of DateTime.

It would be nice if DateTime was automatically represented as type string and format date-time, as specified in https://spec.openapis.org/oas/v3.0.3#data-types, however a more flexible solution might be just to add a type-property to ApiSchemaAttributes so that it is possible to override the automatic schema generation.

Come to think of it, it would be very helpful if most of the properties on the schema object (https://spec.openapis.org/oas/v3.0.3#schema-object) were available in the ApiSchema attribute.

Dreamwalker666 commented 3 years ago

@nahojd Getting the info correct in the schema is fine but its going to leave you to fix the serialiser your using I guess? it's not going to obey the format rule automatically.

nahojd commented 3 years ago

@Dreamwalker666 Sure, but at least the default System.Text.Json serialiser serialises a DateTime to proper ISO 8601 string by default.

Dreamwalker666 commented 3 years ago

@nahojd good to know hadn't checked that.

I did have a look at the code briefly as I will hit this issue my self. Adding DateTime into the SchemaElement.IsSimple method does actually output the correct info if you add the ApiSchema attribute to give it a format. But it should default to "date-time" automatically though you sorta expect that.

One side affect though for some reason TimeSpan appears in the schema objects! :D

I might have a better look when I have time but at least it shouldn't be too complicated to sort out.

jchannon commented 2 years ago

Carter 6 will use OpenApi in .NET6