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

Add custom json formatter for format: date #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable) as this =

let pField, pProp = generateProperty propName pTy

//
if (schemaObj.Type, schemaObj.Format) = ("string", "date") then
pProp.AddCustomAttribute(RuntimeHelpers.getDateTimeOffsetFullDateConverterAttribute())

if not <| String.IsNullOrWhiteSpace propSchema.Description then
pProp.AddXmlDoc propSchema.Description

Expand Down
23 changes: 23 additions & 0 deletions src/SwaggerProvider.Runtime/RuntimeHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Swagger.Internal

open System
open System.Net.Http
open System.Text.Json
open System.Text.Json.Serialization
open System.Threading.Tasks

Expand All @@ -18,6 +19,19 @@ module MediaTypes =
[<Literal>]
let MultipartFormData = "multipart/form-data"

type DateTimeOffsetFullDateConverter() =

inherit JsonConverter<DateTimeOffset>()

override this.Read(reader: byref<Utf8JsonReader>, _typ: Type, options: JsonSerializerOptions) =
DateTimeOffset.Parse(reader.GetString())

override this.Write(writer: Utf8JsonWriter, value: DateTimeOffset, options: JsonSerializerOptions) =
writer.WriteStringValue(
// full-date: date-fullyear "-" date-month "-" date-mday according to https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
// this format is required for strings with format 'date' according to https://swagger.io/docs/specification/data-models/data-types/
value.ToUniversalTime().ToString("yyyy-MM-dd")
)

type AsyncExtensions() =
static member cast<'t> asyncOp =
Expand Down Expand Up @@ -123,6 +137,15 @@ module RuntimeHelpers =

member _.NamedArguments = [||] :> Collections.Generic.IList<_> }

let getDateTimeOffsetFullDateConverterAttribute() =
{ new Reflection.CustomAttributeData() with
member _.Constructor = typeof<JsonConverterAttribute>.GetConstructor [| typeof<Type> |]

member _.ConstructorArguments =
[| Reflection.CustomAttributeTypedArgument(typeof<Type>, typeof<DateTimeOffsetFullDateConverter>) |] :> Collections.Generic.IList<_>

member _.NamedArguments = [||] :> Collections.Generic.IList<_> }

let toStringContent(valueStr: string) =
new StringContent(valueStr, Text.Encoding.UTF8, "application/json")

Expand Down
Loading