-
Notifications
You must be signed in to change notification settings - Fork 198
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
00edaf1
add TimeSpanObjectTypeVisitor
sinantutan 9434685
add missing method in TimeSpanObjectTypeVisitor and add Tests for Tim…
sinantutan 7fb5d41
Merge branch 'Azure:main' into main
sinantutan 1853506
add FakeTimeSpanParameterExample and add new tests for OpenApiExample…
sinantutan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/TimeSpanObjectTypeVisitor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...crosoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests.Fakes/FakeTimeSpanParameterExample.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...ft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/Visitors/TimeSpanObjectTypeVisitorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.