Skip to content

Commit

Permalink
Merge pull request #2151 from ebooks-com/nullable-strings-showing-str…
Browse files Browse the repository at this point in the history
…ing-null-in-examples

Nullable strings showing string null in examples
  • Loading branch information
domaindrivendev authored Jun 29, 2021
2 parents 21391c2 + 97357eb commit a66864f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ private static IOpenApiAny CreateOpenApiObject(JsonElement jsonElement)

private static IOpenApiAny CreateFromJsonElement(JsonElement jsonElement)
{
if (jsonElement.ValueKind == JsonValueKind.Null)
return new OpenApiNull();

if (jsonElement.ValueKind == JsonValueKind.True || jsonElement.ValueKind == JsonValueKind.False)
return new OpenApiBoolean(jsonElement.GetBoolean());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void ApplyMemberTags(OpenApiSchema schema, SchemaFilterContext context)
var exampleNode = fieldOrPropertyNode.SelectSingleNode("example");
if (exampleNode != null)
{
var exampleAsJson = (schema.ResolveType(context.SchemaRepository) == "string")
var exampleAsJson = (schema.ResolveType(context.SchemaRepository) == "string") && !exampleNode.Value.Equals("null")
? $"\"{exampleNode.InnerXml}\""
: exampleNode.InnerXml;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public class XmlAnnotatedType
/// <example>d3966535-2637-48fa-b911-e3c27405ee09</example>
public Guid GuidProperty { get; set; }

/// <summary>
/// Summary for Nullable StringPropertyWithNullExample
/// </summary>
/// <example>null</example>
public string StringPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for StringProperty
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public void CreateFromJson_SimpleType(string json, Type expectedType, object exp
Assert.Equal(expectedValue, actualValue);
}

[Fact]
public void CreateFromJson_NullType()
{
var expectedType = typeof(OpenApiNull);

var openApiAnyObject = OpenApiAnyFactory.CreateFromJson("null");
Assert.NotNull(openApiAnyObject);
Assert.Equal(expectedType, openApiAnyObject.GetType());
Assert.Equal(AnyType.Null, openApiAnyObject.AnyType);
var valueProperty = expectedType.GetProperty("Value");
Assert.Null(valueProperty);
}

[Theory]
[InlineData("[1,2]", typeof(OpenApiInteger), 1, 2)]
[InlineData("[4294877294,4294877295]", typeof(OpenApiLong), 4294877294L, 4294877295L)]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void Apply_SetsDescription_FromPropertySummaryTag(
[InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.GuidProperty), "string", "\"d3966535-2637-48fa-b911-e3c27405ee09\"")]
[InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringProperty), "string", "\"Example for StringProperty\"")]
[InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.ObjectProperty), "object", "{\n \"prop1\": 1,\n \"prop2\": \"foobar\"\n}")]
[InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithNullExample), "string", "null")]
[UseInvariantCulture]
public void Apply_SetsExample_FromPropertyExampleTag(
Type declaringType,
Expand Down

0 comments on commit a66864f

Please sign in to comment.