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 StringProperty
/// </summary>
/// <example>null</example>
public string NullableStringProperty { get; set; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little nitpick (sorry) but the current name would make me think it's a nullable string in the context of nullable reference types. I think it would make more sense to name it StringPropertyWithNullExample.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I struggled with how to name this, your suggestion makes a lot more sense. Just updated the PR with this change.


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

[Theory]
[InlineData("null", typeof(OpenApiNull), null)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there's currently only one case, and I can't really think of a need for others, I would define this test as a Fact (no need for InlineData plus parameters) rather than a Theory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will do!

public void CreateFromJson_NullType(string json, Type expectedType, object expectedValue)
{
var openApiAnyObject = OpenApiAnyFactory.CreateFromJson(json);
Assert.NotNull(openApiAnyObject);
Assert.Equal(expectedType, openApiAnyObject.GetType());
Assert.Equal(AnyType.Null, openApiAnyObject.AnyType);
var valueProperty = expectedType.GetProperty("Value");
Assert.Equal(expectedValue, 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.NullableStringProperty), "string", "null")]
[UseInvariantCulture]
public void Apply_SetsExample_FromPropertyExampleTag(
Type declaringType,
Expand Down