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 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
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 Expand Up @@ -161,6 +165,10 @@ public static string ToDataFormat(this Type type)
{
return "date-time";
}
else if (type == typeof(TimeSpan))
{
return "timespan";
}
else
{
return null;
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,61 @@
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors
{
/// <summary>
/// This represents the type visitor for <see cref="TimeSpan"/>.
/// </summary>
public class TimeSpanObjectTypeVisitor : TypeVisitor
{
/// <inheritdoc />
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 IsParameterVisitable(Type type)
{
var isVisitable = this.IsVisitable(type, TypeCode.Object) && type == typeof(TimeSpan);

return isVisitable;
}

/// <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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Resolvers;
using Newtonsoft.Json.Serialization;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests.Fakes
{
public class FakeTimeSpanParameterExample : OpenApiExample<TimeSpan>
{
public override IOpenApiExample<TimeSpan> Build(NamingStrategy namingStrategy = null)
{
this.Examples.Add(OpenApiExampleResolver.Resolve("timeSpanValue1", new TimeSpan(6,12,14).ToString(), namingStrategy));
this.Examples.Add(OpenApiExampleResolver.Resolve("timeSpanValue2", new TimeSpan(6,12,14,45).ToString(), namingStrategy));
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ public void Given_DateTimeOffsetType_When_Instantiated_Then_It_Should_Return_Res
(result[exampleName].Value as OpenApiDateTime).Value.Should().Be(DateTimeOffset.Parse(exampleValue));
}

[TestMethod]
[DataRow("timeSpanValue1","06:12:14")]
[DataRow("timeSpanValue2", "6.12:14:45")]
public void Given_TimeSpanType_When_Instantiated_Then_It_Should_Return_Result(string exampleName, string exampleValue)
{
var namingStrategy = new DefaultNamingStrategy();
var example = new FakeTimeSpanParameterExample();

var result = example.Build(namingStrategy).Examples;

result[exampleName].Value.Should().BeOfType<OpenApiString>();
(result[exampleName].Value as OpenApiString).Value.Should().Be(exampleValue);
}

[TestMethod]
[DataRow("guidValue1", "74be27de-1e4e-49d9-b579-fe0b331d3642")]
public void Given_GuidType_When_Instantiated_Then_It_Should_Return_Result(string exampleName, string exampleValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void Given_TypeCode_ToDataType_Should_Throw_Exception()
[DataRow(typeof(DateTime), "string")]
[DataRow(typeof(DateTimeOffset), "string")]
[DataRow(typeof(Guid), "string")]
[DataRow(typeof(TimeSpan), "string")]
[DataRow(typeof(object), "object")]
public void Given_TypeCode_ToDataType_Should_Return_Value(Type type, string expected)
{
Expand All @@ -69,6 +70,7 @@ public void Given_TypeCode_ToDataFormat_Should_Throw_Exception()
[DataRow(typeof(DateTime), "date-time")]
[DataRow(typeof(DateTimeOffset), "date-time")]
[DataRow(typeof(Guid), "uuid")]
[DataRow(typeof(TimeSpan), "timespan")]
[DataRow(typeof(object), null)]
public void Given_TypeCode_ToDataFormat_Should_Return_Value(Type type, string expected)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public void Given_NonGenericType_When_GetUnderlyingType_Invoked_Then_It_Should_R
[DataRow(typeof(int?), typeof(int))]
[DataRow(typeof(bool?), typeof(bool))]
[DataRow(typeof(DateTime?), typeof(DateTime))]
[DataRow(typeof(TimeSpan?), typeof(TimeSpan))]
public void Given_NullableType_When_GetUnderlyingType_Invoked_Then_It_Should_Return_Result(Type type, Type expected)
{
var result = TypeExtensions.GetUnderlyingType(type);
Expand All @@ -129,6 +130,7 @@ public void Given_ListType_When_GetUnderlyingType_Invoked_Then_It_Should_Return_
[DataRow(typeof(List<int?>), typeof(int))]
[DataRow(typeof(List<bool?>), typeof(bool))]
[DataRow(typeof(List<DateTime?>), typeof(DateTime))]
[DataRow(typeof(List<TimeSpan?>), typeof(TimeSpan))]
public void Given_NullableListType_When_GetUnderlyingType_Invoked_Then_It_Should_Return_Result(Type type, Type expected)
{
var result = TypeExtensions.GetUnderlyingType(type);
Expand All @@ -151,6 +153,7 @@ public void Given_DictionaryType_When_GetUnderlyingType_Invoked_Then_It_Should_R
[DataRow(typeof(Dictionary<string, int?>), typeof(int))]
[DataRow(typeof(Dictionary<string, bool?>), typeof(bool))]
[DataRow(typeof(Dictionary<string, DateTime?>), typeof(DateTime))]
[DataRow(typeof(Dictionary<string, TimeSpan?>), typeof(TimeSpan))]
public void Given_NullableDictionaryType_When_GetUnderlyingType_Invoked_Then_It_Should_Return_Result(Type type, Type expected)
{
var result = TypeExtensions.GetUnderlyingType(type);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

using FluentAssertions;

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors;
using Microsoft.OpenApi.Any;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Newtonsoft.Json.Serialization;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests.Visitors;

[TestClass]
public class TimeSpanObjectTypeVisitorTests
{
private VisitorCollection _visitorCollection;
private IVisitor _visitor;
private NamingStrategy _strategy;

[TestInitialize]
public void Init()
{
this._visitorCollection = new VisitorCollection();
this._visitor = new TimeSpanObjectTypeVisitor(this._visitorCollection);
this._strategy = new CamelCaseNamingStrategy();
}

[DataTestMethod]
[DataRow(typeof(TimeSpan), false)]
public void Given_Type_When_IsNavigatable_Invoked_Then_It_Should_Return_Result(Type type, bool expected)
{
var result = this._visitor.IsNavigatable(type);

result.Should().Be(expected);
}

[DataTestMethod]
[DataRow(typeof(TimeSpan), true)]
public void Given_Type_When_IsVisitable_Invoked_Then_It_Should_Return_Result(Type type, bool expected)
{
var result = this._visitor.IsVisitable(type);

result.Should().Be(expected);
}

[DataTestMethod]
[DataRow(typeof(TimeSpan), true)]
public void Given_Type_When_IsPayloadVisitable_Invoked_Then_It_Should_Return_Result(Type type, bool expected)
{
var result = this._visitor.IsPayloadVisitable(type);

result.Should().Be(expected);
}

[DataTestMethod]
[DataRow("string", "timespan")]
public void Given_Type_When_Visit_Invoked_Then_It_Should_Return_Result(string dataType, string dataFormat)
{
var name = "hello";
var acceptor = new OpenApiSchemaAcceptor();
var type = new KeyValuePair<string, Type>(name, typeof(TimeSpan));

this._visitor.Visit(acceptor, type, this._strategy);

acceptor.Schemas.Should().ContainKey(name);
acceptor.Schemas[name].Type.Should().Be(dataType);
acceptor.Schemas[name].Format.Should().Be(dataFormat);
}

[DataTestMethod]
[DataRow("hello", "lorem ipsum")]
public void Given_OpenApiPropertyAttribute_When_Visit_Invoked_Then_It_Should_Return_Result(string name, string description)
{
var acceptor = new OpenApiSchemaAcceptor();
var type = new KeyValuePair<string, Type>(name, typeof(TimeSpan));
var attribute = new OpenApiPropertyAttribute() { Description = description };

this._visitor.Visit(acceptor, type, this._strategy, attribute);

acceptor.Schemas[name].Nullable.Should().Be(false);
acceptor.Schemas[name].Default.Should().BeNull();
acceptor.Schemas[name].Description.Should().Be(description);
}

[DataTestMethod]
[DataRow("hello", true, "lorem ipsum")]
[DataRow("hello", false, "lorem ipsum")]
public void Given_OpenApiPropertyAttribute_With_Default_When_Visit_Invoked_Then_It_Should_Return_Result(string name, bool nullable, string description)
{
var @default = DateTime.UtcNow;
var acceptor = new OpenApiSchemaAcceptor();
var type = new KeyValuePair<string, Type>(name, typeof(TimeSpan));
var attribute = new OpenApiPropertyAttribute() { Nullable = nullable, Default = @default, Description = description };

this._visitor.Visit(acceptor, type, this._strategy, attribute);

acceptor.Schemas[name].Nullable.Should().Be(nullable);
acceptor.Schemas[name].Default.Should().NotBeNull();
(acceptor.Schemas[name].Default as OpenApiDateTime).Value.Should().Be(@default);
acceptor.Schemas[name].Description.Should().Be(description);
}

[DataTestMethod]
[DataRow("hello", true, "lorem ipsum")]
[DataRow("hello", false, "lorem ipsum")]
public void Given_OpenApiPropertyAttribute_Without_Default_When_Visit_Invoked_Then_It_Should_Return_Result(string name, bool nullable, string description)
{
var acceptor = new OpenApiSchemaAcceptor();
var type = new KeyValuePair<string, Type>(name, typeof(TimeSpan));
var attribute = new OpenApiPropertyAttribute() { Nullable = nullable, Description = description };

this._visitor.Visit(acceptor, type, this._strategy, attribute);

acceptor.Schemas[name].Nullable.Should().Be(nullable);
acceptor.Schemas[name].Default.Should().BeNull();
acceptor.Schemas[name].Description.Should().Be(description);
}

[DataTestMethod]
[DataRow("hello", OpenApiVisibilityType.Advanced)]
[DataRow("hello", OpenApiVisibilityType.Important)]
[DataRow("hello", OpenApiVisibilityType.Internal)]
public void Given_OpenApiSchemaVisibilityAttribute_When_Visit_Invoked_Then_It_Should_Return_Result(string name, OpenApiVisibilityType visibility)
{
var acceptor = new OpenApiSchemaAcceptor();
var type = new KeyValuePair<string, Type>(name, typeof(TimeSpan));
var attribute = new OpenApiSchemaVisibilityAttribute(visibility);

this._visitor.Visit(acceptor, type, this._strategy, attribute);

acceptor.Schemas[name].Extensions.Should().ContainKey("x-ms-visibility");
acceptor.Schemas[name].Extensions["x-ms-visibility"].Should().BeOfType<OpenApiString>();
(acceptor.Schemas[name].Extensions["x-ms-visibility"] as OpenApiString).Value.Should().Be(visibility.ToDisplayName(this._strategy));
}

[DataTestMethod]
[DataRow("string", "timespan")]
public void Given_Type_When_ParameterVisit_Invoked_Then_It_Should_Return_Result(string dataType, string dataFormat)
{
var result = this._visitor.ParameterVisit(typeof(TimeSpan), this._strategy);

result.Type.Should().Be(dataType);
result.Format.Should().Be(dataFormat);
}

[DataTestMethod]
[DataRow("string", "timespan")]
public void Given_Type_When_PayloadVisit_Invoked_Then_It_Should_Return_Null(string dataType, string dataFormat)
{
var result = this._visitor.PayloadVisit(typeof(TimeSpan), this._strategy);

result.Type.Should().Be(dataType);
result.Format.Should().Be(dataFormat);
}
}