Skip to content

Commit

Permalink
Merge pull request #422 from microsoft/feature/ms-extensions
Browse files Browse the repository at this point in the history
moves all microsoft extensions to a single place, depends on microsoft/OpenAPI.NET#1333
  • Loading branch information
baywet authored Sep 11, 2023
2 parents e2a162c + 0edd8f8 commit fc3f252
Show file tree
Hide file tree
Showing 19 changed files with 3,166 additions and 3,645 deletions.
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi.OData.Reader/Edm/ODataSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.OData.Edm;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OData.Edm.Vocabularies;
using Microsoft.OpenApi.OData.OpenApiExtensions;

namespace Microsoft.OpenApi.OData.Edm
{
Expand Down Expand Up @@ -109,7 +108,7 @@ public string GetPathItemName(OpenApiConvertSettings settings)
return GetPathItemName(settings, new HashSet<string>());
}
/// <summary>
/// Profides a suffix for the operation id based on the operation path.
/// Provides a suffix for the operation id based on the operation path.
/// </summary>
/// <param name="path">Path to use to deduplicate.</param>
/// <param name="settings">The settings.</param>
Expand All @@ -131,7 +130,8 @@ public string GetPathHash(OpenApiConvertSettings settings, ODataPath path = defa
/// <summary>
/// Provides any deprecation information for the segment.
/// </summary>
public OpenApiDeprecationExtension Deprecation { get; set; }
[Obsolete("This property never returned any value or was used by the library.")]
public Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiDeprecationExtension Deprecation { get; set; }
/// <summary>
/// Returns the list of <see cref="IEdmVocabularyAnnotatable"/> this segment refers to.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static class RecordExpressionExtensions
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
return (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmIntegerConstantExpression value) ?
value.Value :
null;
Expand All @@ -46,7 +46,7 @@ public static string GetString(this IEdmRecordExpression record, string property
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
return (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmStringConstantExpression value) ?
value.Value :
null;
Expand All @@ -63,7 +63,7 @@ public static string GetString(this IEdmRecordExpression record, string property
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
return (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmBooleanConstantExpression value) ?
value.Value :
null;
Expand All @@ -80,7 +80,7 @@ public static string GetString(this IEdmRecordExpression record, string property
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
return (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmDateConstantExpression value) ?
value.Value :
null;
Expand All @@ -99,7 +99,7 @@ public static string GetString(this IEdmRecordExpression record, string property
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
return (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmEnumMemberExpression value &&
value.EnumMembers != null &&
value.EnumMembers.Any() &&
Expand All @@ -121,7 +121,7 @@ public static T GetRecord<T>(this IEdmRecordExpression record, string propertyNa
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

if (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
if (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmRecordExpression recordValue)
{
T a = new();
Expand All @@ -143,7 +143,7 @@ public static string GetPropertyPath(this IEdmRecordExpression record, string pr
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
return (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmPathExpression value) ?
value.Path :
null;
Expand All @@ -160,7 +160,7 @@ public static IList<string> GetCollectionPropertyPath(this IEdmRecordExpression
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

if (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
if (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmCollectionExpression value && value.Elements != null)
{
IList<string> properties =
Expand Down Expand Up @@ -189,7 +189,7 @@ public static IList<string> GetCollection(this IEdmRecordExpression record, stri
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

if (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
if (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmCollectionExpression collection && collection.Elements != null)
{
IList<string> items = collection.Elements
Expand All @@ -215,7 +215,7 @@ public static IList<T> GetCollection<T>(this IEdmRecordExpression record, string
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));

if (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
if (record.Properties?.FirstOrDefault(e => propertyName.Equals(e.Name, StringComparison.Ordinal)) is IEdmPropertyConstructor property &&
property.Value is IEdmCollectionExpression collection && collection.Elements != null)
{
IList<T> items = new List<T>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.OpenApiExtensions;
using Microsoft.OpenApi.MicrosoftExtensions;

namespace Microsoft.OpenApi.OData.Generator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using Microsoft.OpenApi.Exceptions;
using System.Linq;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.OData.OpenApiExtensions;
using Microsoft.OpenApi.MicrosoftExtensions;
using Microsoft.OpenApi.OData.Vocabulary.Core;

namespace Microsoft.OpenApi.OData.Generator
Expand Down Expand Up @@ -324,9 +324,8 @@ public static OpenApiSchema CreateEnumTypeSchema(this ODataContext context, IEdm
var enumFlagsExtension = new OpenApiEnumFlagsExtension
{
IsFlags = true,
Style = "simple"
};
schema.Extensions.Add(enumFlagsExtension.Name, enumFlagsExtension);
schema.Extensions.Add(OpenApiEnumFlagsExtension.Name, enumFlagsExtension);
}

var extension = (context.Settings.OpenApiSpecVersion == OpenApiSpecVersion.OpenApi2_0 ||
Expand All @@ -345,7 +344,7 @@ public static OpenApiSchema CreateEnumTypeSchema(this ODataContext context, IEdm
}

if(extension?.ValuesDescriptions.Any() ?? false)
schema.Extensions.Add(extension.Name, extension);
schema.Extensions.Add(OpenApiEnumValuesDescriptionExtension.Name, extension);
schema.Title = enumType.Name;
return schema;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,23 @@
// ------------------------------------------------------------

using System;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.Writers;

namespace Microsoft.OpenApi.OData.OpenApiExtensions;

/// <summary>
/// Extension element for OpenAPI to add deprecation information. x-ms-deprecation
/// </summary>
public class OpenApiDeprecationExtension : IOpenApiExtension
/// <inheritdoc />
[Obsolete("This class is deprecated. Use Microsoft.OpenApi.MicrosoftExtensions.OpenApiDeprecationExtension instead.")]
public class OpenApiDeprecationExtension : Microsoft.OpenApi.MicrosoftExtensions.OpenApiDeprecationExtension
{
/// <summary>
/// Name of the extension as used in the description.
/// Name of the extension use in OpenAPI document.
/// </summary>
public string Name => "x-ms-deprecation";
public new string Name => Microsoft.OpenApi.MicrosoftExtensions.OpenApiDeprecationExtension.Name;
/// <summary>
/// The date at which the element has been/will be removed entirely from the service.
/// </summary>
public DateTime? RemovalDate { get; set; }
public new DateTime? RemovalDate { get => base.RemovalDate.HasValue ? base.RemovalDate.Value.DateTime : default; set => base.RemovalDate = value.HasValue ? new DateTimeOffset(value.Value) : default; }
/// <summary>
/// The date at which the element has been/will be deprecated.
/// </summary>
public DateTime? Date { get; set; }
/// <summary>
/// The version this revision was introduced.
/// </summary>
public string Version { get; set; }
/// <summary>
/// The description of the revision.
/// </summary>
public string Description { get; set; }
/// <inheritdoc />
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
if(writer == null)
throw new ArgumentNullException(nameof(writer));

if(RemovalDate.HasValue || Date.HasValue || !string.IsNullOrEmpty(Version) || !string.IsNullOrEmpty(Description))
{
writer.WriteStartObject();

if(RemovalDate.HasValue)
writer.WriteProperty(nameof(RemovalDate).ToFirstCharacterLowerCase(), RemovalDate.Value);
if(Date.HasValue)
writer.WriteProperty(nameof(Date).ToFirstCharacterLowerCase(), Date.Value);
if(!string.IsNullOrEmpty(Version))
writer.WriteProperty(nameof(Version).ToFirstCharacterLowerCase(), Version);
if(!string.IsNullOrEmpty(Description))
writer.WriteProperty(nameof(Description).ToFirstCharacterLowerCase(), Description);

writer.WriteEndObject();
}
}
public new DateTime? Date { get => base.Date.HasValue ? base.Date.Value.DateTime : default; set => base.Date = value.HasValue ? new DateTimeOffset(value.Value) : default; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,15 @@
// ------------------------------------------------------------

using System;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.Writers;

namespace Microsoft.OpenApi.OData.OpenApiExtensions;

/// <summary>
/// Extension element for OpenAPI to add deprecation information. x-ms-enum-flags
/// </summary>
public class OpenApiEnumFlagsExtension : IOpenApiExtension
/// <inheritdoc />
[Obsolete("This class is deprecated. Use Microsoft.OpenApi.MicrosoftExtensions.OpenApiEnumFlagsExtension instead.")]
public class OpenApiEnumFlagsExtension : Microsoft.OpenApi.MicrosoftExtensions.OpenApiEnumFlagsExtension
{
/// <summary>
/// Name of the extension as used in the description.
/// </summary>
public string Name => "x-ms-enum-flags";
/// <summary>
/// Whether the enum is a flagged enum.
/// </summary>
public bool IsFlags { get; set; }
/// <summary>
/// The serialization style of the flagged enum.
/// </summary>
public string Style { get; set; }
/// <inheritdoc />
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
if(writer == null)
throw new ArgumentNullException(nameof(writer));

writer.WriteStartObject();
writer.WriteProperty(nameof(IsFlags).ToFirstCharacterLowerCase(), IsFlags);
writer.WriteProperty(nameof(Style).ToFirstCharacterLowerCase(),Style);
writer.WriteEndObject();
}
public new string Name => Microsoft.OpenApi.MicrosoftExtensions.OpenApiEnumFlagsExtension.Name;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,11 @@
// ------------------------------------------------------------

using System;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;

namespace Microsoft.OpenApi.OData.OpenApiExtensions;

/// <summary>
/// Extension element for OpenAPI to add tag the primary error message to use on error types. x-ms-primary-error-message
/// </summary>
public class OpenApiPrimaryErrorMessageExtension : IOpenApiExtension
/// <inheritdoc />
[Obsolete("This class is deprecated. Use Microsoft.OpenApi.MicrosoftExtensions.OpenApiPrimaryErrorMessageExtension instead.")]
public class OpenApiPrimaryErrorMessageExtension : Microsoft.OpenApi.MicrosoftExtensions.OpenApiPrimaryErrorMessageExtension
{
/// <summary>
/// Name of the extension as used in the description.
/// </summary>
public static string Name => "x-ms-primary-error-message";
/// <inheritdoc />
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
if(writer == null)
throw new ArgumentNullException(nameof(writer));
writer.WriteValue(IsPrimaryErrorMessage);
}
/// <summary>
/// Whether this property is the primary error message to use on error types.
/// </summary>
public bool IsPrimaryErrorMessage { get; set; }
/// <summary>
/// Parses the <see cref="IOpenApiAny"/> to <see cref="OpenApiPrimaryErrorMessageExtension"/>.
/// </summary>
/// <param name="source">The source object.</param>
/// <returns>The <see cref="OpenApiPrimaryErrorMessageExtension"/>.</returns>
public static OpenApiPrimaryErrorMessageExtension Parse(IOpenApiAny source)
{
if (source is not OpenApiBoolean rawObject) throw new ArgumentOutOfRangeException(nameof(source));
return new OpenApiPrimaryErrorMessageExtension() {
IsPrimaryErrorMessage = rawObject.Value
};
}
}
Loading

0 comments on commit fc3f252

Please sign in to comment.