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 @@ -13,13 +13,15 @@ public static JsonNode Create(
{
var type = schema?.ResolveType(schemaRepository);

var isStringType = type is { } value &&
value.HasFlag(JsonSchemaType.String) &&
!value.HasFlag(JsonSchemaType.Null);

if (isStringType)
if (string.Equals(exampleString, "null"))
{
return string.Equals(exampleString, "null") ? JsonNullSentinel.JsonNull : JsonValue.Create(exampleString);
return JsonNullSentinel.JsonNull;
}

if (type is { } value && value.HasFlag(JsonSchemaType.String))
{
return JsonValue.Create(exampleString);
}

// HACK If the value is a string, but we can't detect it as one, then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Swashbuckle.AspNetCore.SwaggerGen.Test;

#nullable enable
/// <summary>
/// Summary for XmlAnnotatedRecord
/// </summary>
Expand All @@ -13,7 +14,10 @@ namespace Swashbuckle.AspNetCore.SwaggerGen.Test;
/// <param name="DateTimeProperty" example="6/22/2022 12:00:00 AM">Summary for DateTimeProperty</param>
/// <param name="EnumProperty" example="2">Summary for EnumProperty</param>
/// <param name="GuidProperty" example="d3966535-2637-48fa-b911-e3c27405ee09">Summary for GuidProperty</param>
/// <param name="StringPropertyWithNullExample" example="null">Summary for Nullable StringPropertyWithNullExample</param>
/// <param name="NullableStringPropertyWithNullExample" example="null">Summary for NullableStringPropertyWithNullExample</param>
/// <param name="StringPropertyWithNullExample" example="null">Summary for StringPropertyWithNullExample</param>
/// <param name="NullableStringPropertyWithNotNullExample" example="example">Summary for NullableStringPropertyWithNotNullExample</param>
/// <param name="NullableIntPropertyWithNotNullExample" example="3">Summary for NullableIntPropertyWithNotNullExample</param>
/// <param name="StringProperty" example="Example for StringProperty">Summary for StringProperty</param>
/// <param name="StringPropertyWithUri" example="https://test.com/a?b=1&amp;c=2">Summary for StringPropertyWithUri</param>
/// <param name="ObjectProperty" example="{&quot;prop1&quot;: 1, &quot;prop2&quot;: &quot;foobar&quot;}">Summary for ObjectProperty</param>
Expand All @@ -26,7 +30,10 @@ public record XmlAnnotatedRecord(
DateTime DateTimeProperty,
IntEnum EnumProperty,
Guid GuidProperty,
string? NullableStringPropertyWithNullExample,
string StringPropertyWithNullExample,
string? NullableStringPropertyWithNotNullExample,
int? NullableIntPropertyWithNotNullExample,
string StringProperty,
string StringPropertyWithUri,
object ObjectProperty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Swashbuckle.AspNetCore.TestSupport;

namespace Swashbuckle.AspNetCore.SwaggerGen.Test;

#nullable enable
/// <summary>
/// Summary for XmlAnnotatedType
/// </summary>
Expand Down Expand Up @@ -62,28 +62,40 @@ public class XmlAnnotatedType
public Guid GuidProperty { get; set; }

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

/// <summary>
/// Summary for NullableStringPropertyWithNotNullExample
/// </summary>
/// <example>example</example>
public string? NullableStringPropertyWithNotNullExample { get; set; }

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

/// <summary>
/// Summary for StringProperty
/// </summary>
/// <example>Example for StringProperty</example>
public string StringProperty { get; set; }
public required string StringProperty { get; set; }

/// <summary>
/// Summary for StringPropertyWithUri
/// </summary>
/// <example><![CDATA[https://test.com/a?b=1&c=2]]></example>
public string StringPropertyWithUri { get; set; }
public required string StringPropertyWithUri { get; set; }

/// <summary>
/// Summary for ObjectProperty
/// </summary>
/// <example>{"prop1": 1, "prop2": "foobar"}</example>
public object ObjectProperty { get; set; }
public required object ObjectProperty { get; set; }

/// <summary>
/// Summary for AcceptsNothing
Expand Down Expand Up @@ -125,19 +137,25 @@ public void AcceptsArrayOfConstructedGenericType(int?[] param)
{
}

/// <summary>
/// >Summary for NullableIntPropertyWithNotNullExample
/// </summary>
/// <example>3</example>
public int? NullableIntPropertyWithNotNullExample { get; set; }

/// <summary>
/// Summary for NestedType
/// </summary>
public class NestedType
{
public string Property { get; set; }
public required string Property { get; set; }

public class InnerNestedType
{
/// <summary>
/// Summary of DoubleNestedType.InnerType.Property
/// </summary>
public string InnerProperty { get; set; }
public required string InnerProperty { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public void Apply_SetsDescription_FromPropertySummaryTag(
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.StringPropertyWithUri), JsonSchemaTypes.String, "\"https://test.com/a?b=1\\u0026c=2\"" },
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithNullExample), JsonSchemaTypes.String, "null" },
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.StringPropertyWithNullExample), JsonSchemaTypes.String, "null" },
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableStringPropertyWithNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "null" },
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.NullableStringPropertyWithNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "null" },
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableStringPropertyWithNotNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "\"example\"" },
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.NullableStringPropertyWithNotNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "\"example\"" },
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableIntPropertyWithNotNullExample), JsonSchemaTypes.Integer | JsonSchemaType.Null, "3" },
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.NullableIntPropertyWithNotNullExample), JsonSchemaTypes.Integer | JsonSchemaType.Null, "3" },
};

[Theory]
Expand Down