Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -17,10 +17,27 @@
value.HasFlag(JsonSchemaType.String) &&
!value.HasFlag(JsonSchemaType.Null);

if (string.Equals(exampleString, "null"))
{

return type is { } jsonSchema && jsonSchema.HasFlag(JsonSchemaType.Null) ? JsonNullSentinel.JsonNull : null;
}

if (isStringType)
{
return string.Equals(exampleString, "null") ? JsonNullSentinel.JsonNull : JsonValue.Create(exampleString);
return JsonValue.Create(exampleString);
if (string.Equals(exampleString, "null"))
{
return type is { } jsonSchema && jsonSchema.HasFlag(JsonSchemaType.Null) ? JsonNullSentinel.JsonNull : null;
}

var isStringType = type is { } value &&
value.HasFlag(JsonSchemaType.String) &&
!value.HasFlag(JsonSchemaType.Null);
if (isStringType)
{
return JsonValue.Create(exampleString);
}

// HACK If the value is a string, but we can't detect it as one, then
// if parsing it fails, assume it is a string that isn't quoted. There's
Expand All @@ -34,4 +51,4 @@
return JsonValue.Create(exampleString);
}
}
}

Check failure on line 54 in src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs

View workflow job for this annotation

GitHub Actions / windows-latest

} expected

Check failure on line 54 in src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs

View workflow job for this annotation

GitHub Actions / windows-latest

} expected

Check failure on line 54 in src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs

View workflow job for this annotation

GitHub Actions / windows-latest

} expected
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,8 @@
"description": "3-letter ISO country code",
"required": true,
"schema": {
"type": "string",
"example": null
},
"example": null
"type": "string"
}
},
{
"name": "city",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,8 @@
"description": "3-letter ISO country code",
"required": true,
"schema": {
"type": "string",
"example": null
},
"example": null
"type": "string"
}
},
{
"name": "city",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,8 @@
"description": "3-letter ISO country code",
"required": true,
"schema": {
"type": "string",
"example": null
},
"example": null
"type": "string"
}
},
{
"name": "city",
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,10 +14,26 @@ 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="NullableIntPropertyWithNotNullExample" example="3">Summary for NullableIntPropertyWithNotNullExample</param>
/// <param name="NullableIntPropertyWithNullExample" example="null">Summary for NullableIntPropertyWithNullExample</param>
/// <param name="IntPropertyWithNullExample" example="null">Summary for IntPropertyWithNullExample</param>
/// <param name="NullableGuidPropertyWithNullExample" example="null">Summary for NullableGuidPropertyWithNullExample</param>
/// <param name="GuidPropertyWithNullExample" example="null">Summary for GuidPropertyWithNullExample</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>
/// <param name="ObjectPropertyNullExample" example="null">Summary for ObjectPropertyNullExample</param>
/// <param name="NullableObjectPropertyNullExample" example="null">Summary for NullableObjectPropertyNullExample</param>
/// <param name="NullableDateTimePropertyWithNullExample" example="null">Summary for NullableDateTimePropertyWithNullExample</param>
/// <param name="DateTimePropertyWithNullExample" example="null">Summary for DateTimePropertyWithNullExample</param>
/// <param name="NullableTimeOnlyPropertyWithNullExample" example="null">Summary for NullableTimeOnlyPropertyWithNullExample</param>
/// <param name="TimeOnlyPropertyWithNullExample" example="null">Summary for TimeOnlyPropertyWithNullExample</param>
/// <param name="NullableTimeSpanPropertyWithNullExample" example="null">Summary for NullableTimeSpanPropertyWithNullExample</param>
/// <param name="TimeSpanPropertyWithNullExample" example="null">Summary for TimeSpanPropertyWithNullExample</param>
/// <param name="NullableDateOnlyPropertyWithNullExample" example="null">Summary for NullableDateOnlyPropertyWithNullExample</param>
/// <param name="DateOnlyPropertyWithNullExample" example="null">Summary for DateOnlyPropertyWithNullExample</param>
public record XmlAnnotatedRecord(
bool BoolProperty,
int IntProperty,
Expand All @@ -26,8 +43,24 @@ public record XmlAnnotatedRecord(
DateTime DateTimeProperty,
IntEnum EnumProperty,
Guid GuidProperty,
string? NullableStringPropertyWithNullExample,
string StringPropertyWithNullExample,
int? NullableIntPropertyWithNotNullExample,
int? NullableIntPropertyWithNullExample,
int IntPropertyWithNullExample,
Guid? NullableGuidPropertyWithNullExample,
Guid GuidPropertyWithNullExample,
string StringProperty,
string StringPropertyWithUri,
object ObjectProperty
object ObjectProperty,
object ObjectPropertyNullExample,
object? NullableObjectPropertyNullExample,
DateTime? NullableDateTimePropertyWithNullExample,
DateTime DateTimePropertyWithNullExample,
TimeOnly? NullableTimeOnlyPropertyWithNullExample,
TimeOnly TimeOnlyPropertyWithNullExample,
TimeSpan? NullableTimeSpanPropertyWithNullExample,
TimeSpan TimeSpanPropertyWithNullExample,
DateOnly? NullableDateOnlyPropertyWithNullExample,
DateOnly DateOnlyPropertyWithNullExample
);
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,46 @@ public class XmlAnnotatedType
public Guid GuidProperty { get; set; }

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

/// <summary>
/// Summary for StringPropertyWithNullExample
/// </summary>
/// <example>null</example>
public string StringPropertyWithNullExample { get; set; }
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 ObjectPropertyNullExample
/// </summary>
/// <example>null</example>
public required object ObjectPropertyNullExample { get; set; }

/// <summary>
/// Summary for NullableObjectPropertyNullExample
/// </summary>
/// <example>null</example>
public object? NullableObjectPropertyNullExample { get; set; }

/// <summary>
/// Summary for AcceptsNothing
Expand Down Expand Up @@ -124,20 +142,109 @@ public void AcceptsConstructedOfConstructedGenericType(
public void AcceptsArrayOfConstructedGenericType(int?[] param)
{
}
///
/// <summary>
/// >Summary for NullableIntPropertyWithNotNullExample
/// </summary>
/// <example>3</example>
public int? NullableIntPropertyWithNotNullExample { get; set; }

/// <summary>
/// Summary for NullableIntPropertyWithNullExample
/// </summary>
/// <example>null</example>
public int? NullableIntPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for IntPropertyWithNullExample
/// </summary>
/// <example>null</example>

public int IntPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for NullableGuidPropertyWithNullExample
/// </summary>
/// <example>null</example>

public Guid? NullableGuidPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for GuidPropertyWithNullExample
/// </summary>
/// <example>null</example>

public Guid GuidPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for NullableDateTimePropertyWithNullExample
/// </summary>
/// <example>null</example>

public DateTime? NullableDateTimePropertyWithNullExample { get; set; }

/// <summary>
/// Summary for DateTimePropertyWithNullExample
/// </summary>
/// <example>null</example>

public DateTime DateTimePropertyWithNullExample { get; set; }

/// <summary>
/// Summary for NullableTimeOnlyPropertyWithNullExample
/// </summary>
/// <example>null</example>

public TimeOnly? NullableTimeOnlyPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for TimeOnlyPropertyWithNullExample
/// </summary>
/// <example>null</example>

public TimeOnly TimeOnlyPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for NullableTimeSpanPropertyWithNullExample
/// </summary>
/// <example>null</example>

public TimeSpan? NullableTimeSpanPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for TimeSpanPropertyWithNullExample
/// </summary>
/// <example>null</example>

public TimeSpan TimeSpanPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for NullableDateOnlyPropertyWithNullExample
/// </summary>
/// <example>null</example>

public DateOnly? NullableDateOnlyPropertyWithNullExample { get; set; }

/// <summary>
/// Summary for DateOnlyPropertyWithNullExample
/// </summary>
/// <example>null</example>

public DateOnly DateOnlyPropertyWithNullExample { 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 @@ -74,7 +74,7 @@ public void Create_Returns_Null_When_Value_And_Schema_Are_Null()
[Fact]
public void Create_Returns_Null_When_Type_String_And_Value_Null_String_Literal()
{
var schema = new OpenApiSchema { Type = JsonSchemaTypes.String };
var schema = new OpenApiSchema { Type = JsonSchemaTypes.String | JsonSchemaType.Null };
schemaRepository.AddDefinition("test", schema);

var example = XmlCommentsExampleHelper.Create(
Expand Down
Loading
Loading