Skip to content

add TimeSpanObjectTypeVisitor #524

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

Merged
merged 4 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static string ToDataType(this Type type)
{
return "string";
}
else if (type == typeof(TimeSpan))
{
return "string";
}
else
{
return "object";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static bool IsSimpleType(this Type type)
case TypeCode.DateTime:
case TypeCode.String:
case TypeCode.Object when type == typeof(Guid):
case TypeCode.Object when type == typeof(TimeSpan):
case TypeCode.Object when type == typeof(DateTime):
case TypeCode.Object when type == typeof(DateTimeOffset):
return true;
Expand Down Expand Up @@ -85,6 +86,7 @@ public static bool IsJObjectType(this Type type)
typeof(Guid),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
typeof(Uri),
typeof(object),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ObjectTypeVisitor : TypeVisitor
{
typeof(Guid),
typeof(DateTime),
typeof(TimeSpan),
typeof(DateTimeOffset),
typeof(Uri),
typeof(Type),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public override bool IsVisitable(Type type)
{
isVisitable = false;
}
else if (type == typeof(TimeSpan))
{
isVisitable = false;
}
else if (type == typeof(DateTimeOffset))
{
isVisitable = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors
{
public class TimeSpanObjectTypeVisitor : TypeVisitor
{
public TimeSpanObjectTypeVisitor(VisitorCollection visitorCollection) : base(visitorCollection)
{
}

/// <inheritdoc />
public override OpenApiSchema ParameterVisit(Type type, NamingStrategy namingStrategy)
{
return this.ParameterVisit(dataType: "string", dataFormat: "timespan");
}

/// <inheritdoc />
public override bool IsVisitable(Type type)
{
var isVisitable = this.IsVisitable(type, TypeCode.Object) && type == typeof(TimeSpan);

return isVisitable;
}

/// <inheritdoc />
public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type, NamingStrategy namingStrategy, params Attribute[] attributes)
{
this.Visit(acceptor, name: type.Key, title: null, dataType: "string", dataFormat: "timespan", attributes: attributes);
}

/// <inheritdoc />
public override bool IsPayloadVisitable(Type type)
{
var isVisitable = this.IsVisitable(type);

return isVisitable;
}

/// <inheritdoc />
public override OpenApiSchema PayloadVisit(Type type, NamingStrategy namingStrategy)
{
return this.PayloadVisit(dataType: "string", dataFormat: "timespan");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ protected IOpenApiAny GetOpenApiPropertyDefault(OpenApiPropertyAttribute attr)
return new OpenApiDateTime((DateTime) @default);
}

if (@default is TimeSpan)
{
return new OpenApiString(@default.ToString());
}

if (@default is DateTimeOffset)
{
return new OpenApiDateTime((DateTimeOffset) @default);
Expand Down