Skip to content

Commit

Permalink
Add obsoletion attributes and fix CloneFromCopyConstructor
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia committed Jul 9, 2024
1 parent a0b6f9d commit 82a5bf9
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/Microsoft.OpenApi/Any/OpenApiAnyCloneHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.OpenApi.Any
Expand All @@ -15,6 +16,7 @@ public class OpenApiAnyCloneHelper
/// </summary>
/// <param name="obj">The object instance.</param>
/// <returns>A clone copy or the object itself.</returns>
[Obsolete("Use native AoT-friendly generic overload of CloneFromCopyConstructor instead.")]
public static IOpenApiAny CloneFromCopyConstructor(IOpenApiAny obj)
{
if (obj != null)
Expand Down
13 changes: 7 additions & 6 deletions src/Microsoft.OpenApi/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static T GetAttributeOfType<T>(this Enum enumValue) where T : Attribute
/// Use <see cref="DisplayAttribute"/> if exists.
/// Otherwise, use the standard string representation.
/// </returns>
[Obsolete("Use native AoT-friendly type-specific overloads GetDisplayName methods instead.")]
public static string GetDisplayName(this Enum enumValue)
{
var attribute = enumValue.GetAttributeOfType<DisplayAttribute>();
Expand All @@ -58,21 +59,21 @@ public static string GetDisplayName(this Enum enumValue)
ParameterStyle.SpaceDelimited => "spaceDelimited",
ParameterStyle.PipeDelimited => "pipeDelimited",
ParameterStyle.DeepObject => "deepObject",
_ => parameterStyle.ToString()
_ => throw new InvalidOperationException($"Unknown parameter style: {parameterStyle}")
};

/// <summary>
/// Gets the enum display for name <see cref="ParameterLocation" /> without the use of reflection.
/// </summary>
/// <param name="parameterLocation">The enum value.</param>
/// <returns>The display string to use.</returns>
internal static string GetDisplayName(this ParameterLocation parameterLocation) => parameterLocation switch
public static string GetDisplayName(this ParameterLocation parameterLocation) => parameterLocation switch
{
ParameterLocation.Query => "query",
ParameterLocation.Header => "header",
ParameterLocation.Path => "path",
ParameterLocation.Cookie => "cookie",
_ => parameterLocation.ToString()
_ => throw new InvalidOperationException($"Unknown parameter location: {parameterLocation}")
};

/// <summary>
Expand All @@ -92,7 +93,7 @@ public static string GetDisplayName(this Enum enumValue)
ReferenceType.Link => "links",
ReferenceType.Callback => "callbacks",
ReferenceType.Tag => "tags",
_ => referenceType.ToString()
_ => throw new InvalidOperationException($"Unknown reference type: {referenceType}")
};

/// <summary>
Expand All @@ -110,7 +111,7 @@ public static string GetDisplayName(this Enum enumValue)
OperationType.Head => "head",
OperationType.Patch => "patch",
OperationType.Trace => "trace",
_ => operationType.ToString()
_ => throw new InvalidOperationException($"Unknown operation type: {operationType}")
};

/// <summary>
Expand All @@ -124,7 +125,7 @@ public static string GetDisplayName(this Enum enumValue)
SecuritySchemeType.Http => "http",
SecuritySchemeType.OAuth2 => "oauth2",
SecuritySchemeType.OpenIdConnect => "openIdConnect",
_ => securitySchemeType.ToString()
_ => throw new InvalidOperationException($"Unknown security scheme type: {securitySchemeType}")
};
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public OpenApiExample(OpenApiExample example)
{
Summary = example?.Summary ?? Summary;
Description = example?.Description ?? Description;
Value = OpenApiAnyCloneHelper.CloneFromCopyConstructor(example?.Value);
Value = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(example?.Value);
ExternalValue = example?.ExternalValue ?? ExternalValue;
Extensions = example?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(example.Extensions) : null;
Reference = example?.Reference != null ? new(example?.Reference) : null;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public OpenApiHeader(OpenApiHeader header)
Explode = header?.Explode ?? Explode;
AllowReserved = header?.AllowReserved ?? AllowReserved;
Schema = header?.Schema != null ? new(header?.Schema) : null;
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(header?.Example);
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(header?.Example);
Examples = header?.Examples != null ? new Dictionary<string, OpenApiExample>(header.Examples) : null;
Content = header?.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
Extensions = header?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(header.Extensions) : null;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiMediaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public OpenApiMediaType() {}
public OpenApiMediaType(OpenApiMediaType mediaType)
{
Schema = mediaType?.Schema != null ? new(mediaType?.Schema) : null;
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(mediaType?.Example);
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(mediaType?.Example);
Examples = mediaType?.Examples != null ? new Dictionary<string, OpenApiExample>(mediaType.Examples) : null;
Encoding = mediaType?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(mediaType.Encoding) : null;
Extensions = mediaType?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(mediaType.Extensions) : null;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public OpenApiParameter(OpenApiParameter parameter)
AllowReserved = parameter?.AllowReserved ?? AllowReserved;
Schema = parameter?.Schema != null ? new(parameter?.Schema) : null;
Examples = parameter?.Examples != null ? new Dictionary<string, OpenApiExample>(parameter.Examples) : null;
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(parameter?.Example);
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(parameter?.Example);
Content = parameter?.Content != null ? new Dictionary<string, OpenApiMediaType>(parameter.Content) : null;
Extensions = parameter?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(parameter.Extensions) : null;
AllowEmptyValue = parameter?.AllowEmptyValue ?? AllowEmptyValue;
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public OpenApiSchema(OpenApiSchema schema)
MinLength = schema?.MinLength ?? MinLength;
Pattern = schema?.Pattern ?? Pattern;
MultipleOf = schema?.MultipleOf ?? MultipleOf;
Default = OpenApiAnyCloneHelper.CloneFromCopyConstructor(schema?.Default);
Default = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(schema?.Default);
ReadOnly = schema?.ReadOnly ?? ReadOnly;
WriteOnly = schema?.WriteOnly ?? WriteOnly;
AllOf = schema?.AllOf != null ? new List<OpenApiSchema>(schema.AllOf) : null;
Expand All @@ -281,7 +281,7 @@ public OpenApiSchema(OpenApiSchema schema)
AdditionalPropertiesAllowed = schema?.AdditionalPropertiesAllowed ?? AdditionalPropertiesAllowed;
AdditionalProperties = schema?.AdditionalProperties != null ? new(schema?.AdditionalProperties) : null;
Discriminator = schema?.Discriminator != null ? new(schema?.Discriminator) : null;
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(schema?.Example);
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(schema?.Example);
Enum = schema?.Enum != null ? new List<IOpenApiAny>(schema.Enum) : null;
Nullable = schema?.Nullable ?? Nullable;
ExternalDocs = schema?.ExternalDocs != null ? new(schema?.ExternalDocs) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public RuntimeExpressionAnyWrapper() {}
/// </summary>
public RuntimeExpressionAnyWrapper(RuntimeExpressionAnyWrapper runtimeExpressionAnyWrapper)
{
Any = OpenApiAnyCloneHelper.CloneFromCopyConstructor(runtimeExpressionAnyWrapper?.Any);
Any = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(runtimeExpressionAnyWrapper?.Any);
Expression = runtimeExpressionAnyWrapper?.Expression;
}

Expand Down

0 comments on commit 82a5bf9

Please sign in to comment.