Skip to content
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

Nullable strings showing string null in examples #2151

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
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
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
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.

Expand Up @@ -66,6 +66,7 @@ public void Apply_SetsDescription_FromFieldSummaryTag()
[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