Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia committed Jun 26, 2024
1 parent cd876c1 commit ca073bf
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
7 changes: 3 additions & 4 deletions src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ namespace Microsoft.AspNetCore.OpenApi;
/// </summary>
internal static class JsonNodeSchemaExtensions
{
private static readonly bool _isNullabilityInfoContextSupported =
AppContext.TryGetSwitch("System.Reflection.NullabilityInfoContext.IsSupported", out bool isSupported) ? isSupported : true;

private static readonly Dictionary<Type, OpenApiSchema> _simpleTypeToOpenApiSchema = new()
{
[typeof(bool)] = new() { Type = "boolean" },
Expand Down Expand Up @@ -364,7 +361,7 @@ internal static void ApplySchemaReferenceId(this JsonNode schema, JsonSchemaExpo
/// <param name="parameterInfo">The <see cref="ParameterInfo" /> associated with the schema.</param>
internal static void ApplyNullabilityContextInfo(this JsonNode schema, ParameterInfo parameterInfo)
{
if (parameterInfo.ParameterType.IsValueType || !_isNullabilityInfoContextSupported)
if (parameterInfo.ParameterType.IsValueType)
{
return;
}
Expand All @@ -384,6 +381,8 @@ internal static void ApplyNullabilityContextInfo(this JsonNode schema, Parameter
/// <param name="propertyInfo">The <see cref="JsonPropertyInfo" /> associated with the schema.</param>
internal static void ApplyNullabilityContextInfo(this JsonNode schema, JsonPropertyInfo propertyInfo)
{
// Avoid setting explicit nullability annotations for `object` types so they continue to match on the catch
// all schema (no type, no format, no constraints).
if (propertyInfo.PropertyType != typeof(object) && (propertyInfo.IsGetNullable || propertyInfo.IsSetNullable))
{
schema[OpenApiSchemaKeywords.NullableKeyword] = true;
Expand Down
19 changes: 18 additions & 1 deletion src/OpenApi/src/Extensions/JsonTypeInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,34 @@ internal static class JsonTypeInfoExtensions
private static readonly List<Type> _exemptPrimitives =
[
typeof(bool),
typeof(byte),
typeof(sbyte),
typeof(byte[]),
typeof(string),
typeof(int),
typeof(uint),
typeof(nint),
typeof(nuint),
typeof(Int128),
typeof(UInt128),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(decimal),
typeof(Half),
typeof(ulong),
typeof(short),
typeof(ushort),
typeof(char),
typeof(object)
typeof(object),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeOnly),
typeof(DateOnly),
typeof(Guid),
typeof(Uri),
typeof(Version)
];
private static readonly Dictionary<Type, string> _simpleTypeToName = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@
},
"components": {
"schemas": {
"DateTime": {
"type": "string",
"format": "date-time"
},
"IFormFile": {
"type": "string",
"format": "binary"
Expand Down Expand Up @@ -234,7 +230,8 @@
"type": "boolean"
},
"createdAt": {
"$ref": "#/components/schemas/DateTime"
"type": "string",
"format": "date-time"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@
},
"components": {
"schemas": {
"DateTime": {
"type": "string",
"format": "date-time"
},
"Todo": {
"required": [
"id",
Expand All @@ -113,7 +109,8 @@
"type": "boolean"
},
"createdAt": {
"$ref": "#/components/schemas/DateTime"
"type": "string",
"format": "date-time"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"type": "boolean"
},
"createdAt": {
"$ref": "#/components/schemas/DateTime"
"type": "string",
"format": "date-time"
}
}
}
Expand Down Expand Up @@ -137,7 +138,8 @@
"type": "object",
"properties": {
"dueDate": {
"$ref": "#/components/schemas/DateTime"
"type": "string",
"format": "date-time"
},
"id": {
"type": "integer",
Expand All @@ -150,7 +152,8 @@
"type": "boolean"
},
"createdAt": {
"$ref": "#/components/schemas/DateTime"
"type": "string",
"format": "date-time"
}
}
}
Expand All @@ -166,16 +169,9 @@
"ArrayOfGuid": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Guid"
"type": "string",
"format": "uuid"
}
},
"DateTime": {
"type": "string",
"format": "date-time"
},
"Guid": {
"type": "string",
"format": "uuid"
}
},
"securitySchemes": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,8 @@ await VerifyOpenApiDocument(builder, options, document =>
Assert.False(responseSchema.Extensions.TryGetValue("x-my-extension", out var _));
// Schemas are distinct because of applied transformer so no reference is used.
Assert.Null(responseSchema.Reference);

// References are only created for `DateTime` type
Assert.Collection(document.Components.Schemas.Keys,
key =>
{
Assert.Equal("DateTime", key);
});
// No schemas get componentized here
Assert.Empty(document.Components.Schemas);
});
}
}

0 comments on commit ca073bf

Please sign in to comment.