Skip to content
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 @@ -10,6 +10,11 @@ public static IOpenApiAny CreateFromJson(string json)

public static IOpenApiAny CreateFromJson(string json, JsonSerializerOptions options)
{
if (json is null)
{
return null;
}

try
{
var element = JsonSerializer.Deserialize<JsonElement>(json, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class XmlCommentsExampleHelperTests
private readonly SchemaRepository schemaRepository = new();

[Fact]
public void Create_BuildsOpenApiArrayJson__When_NotStringTypeAndDataIsArray()
public void Create_Builds_OpenApiArrayJson_When_Not_String_Type_And_Data_Is_Array()
{
var schema = new OpenApiSchema();

Expand All @@ -31,10 +31,13 @@ public void Create_BuildsOpenApiArrayJson__When_NotStringTypeAndDataIsArray()
Assert.Equal("three", item3.Value);
}

[Fact]
public void Create_BuildsOpenApiString_When_TypeString()
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData("foo")]
[InlineData("example string with special characters\"<>\r\n\"")]
public void Create_Builds_OpenApiString_When_Type_String(string exampleString)
{
string exampleString = "example string with special characters\"<>\r\n\"";
var schema = new OpenApiSchema { Type = JsonSchemaTypes.String };
schemaRepository.AddDefinition("test", schema);

Expand All @@ -48,7 +51,30 @@ public void Create_BuildsOpenApiString_When_TypeString()
}

[Fact]
public void Create_ReturnsNull_When_TypeString_and_ValueNull()
public void Create_Returns_Null_When_Type_String_And_Value_Is_Null()
{
var schema = new OpenApiSchema { Type = JsonSchemaTypes.String };
schemaRepository.AddDefinition("test", schema);

var example = XmlCommentsExampleHelper.Create(
schemaRepository, schema, null);

Assert.NotNull(example);

var actual = Assert.IsType<OpenApiNull>(example);
Assert.Equal(AnyType.Null, actual.AnyType);
}

[Fact]
public void Create_Returns_Null_When_Value_And_Schema_Are_Null()
{
var example = XmlCommentsExampleHelper.Create(schemaRepository, null, null);

Assert.Null(example);
}

[Fact]
public void Create_Returns_Null_When_Type_String_And_Value_Null_String_Literal()
{
var schema = new OpenApiSchema { Type = JsonSchemaTypes.String };
schemaRepository.AddDefinition("test", schema);
Expand All @@ -63,7 +89,7 @@ public void Create_ReturnsNull_When_TypeString_and_ValueNull()
}

[Fact]
public void Create_AllowsSchemaToBeNull()
public void Create_Allows_Schema_To_Be_Null()
{
OpenApiSchema schema = null;

Expand All @@ -74,4 +100,32 @@ public void Create_AllowsSchemaToBeNull()
var actual = Assert.IsType<OpenApiArray>(example);
Assert.Empty(actual);
}

[Fact]
public void Create_Builds_OpenApiString_When_Type_Integer()
{
string exampleString = "1";
var schema = new OpenApiSchema { Type = JsonSchemaTypes.Integer };
schemaRepository.AddDefinition("test", schema);

var example = XmlCommentsExampleHelper.Create(
schemaRepository, schema, exampleString);

Assert.NotNull(example);

var actual = Assert.IsType<OpenApiInteger>(example);
Assert.Equal(1, actual.Value);
}

[Fact]
public void Create_Returns_Null_When_Type_Integer_And_Value_Is_Null()
{
var schema = new OpenApiSchema { Type = JsonSchemaTypes.Integer };
schemaRepository.AddDefinition("test", schema);

var example = XmlCommentsExampleHelper.Create(
schemaRepository, schema, null);

Assert.Null(example);
}
}