Skip to content

Commit

Permalink
Use JsonNode in place of OpenApiAny for Enums and Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Aug 22, 2024
1 parent 2f29a30 commit cc1439e
Show file tree
Hide file tree
Showing 64 changed files with 218 additions and 207 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Any/OpenApiAny.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public OpenApiAny(JsonNode jsonNode)
/// <param name="specVersion"></param>
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
writer.WriteAny(new OpenApiAny(Node));
writer.WriteAny(Node);
}
}
}
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi/Helpers/JsonNodeCloneHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ internal static class JsonNodeCloneHelper
ReferenceHandler = ReferenceHandler.IgnoreCycles
};

internal static OpenApiAny Clone(OpenApiAny value)
internal static JsonNode Clone(JsonNode value)
{
var jsonString = Serialize(value?.Node);
var jsonString = Serialize(value);
if (string.IsNullOrEmpty(jsonString))
{
return null;
}

var result = JsonSerializer.Deserialize<JsonNode>(jsonString, options);
return new OpenApiAny(result);
return result;
}

private static string Serialize(object obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
/// <param name="source">The source object.</param>
/// <returns>The <see cref="OpenApiDeprecationExtension"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">When the source element is not an object</exception>
public static OpenApiDeprecationExtension Parse(OpenApiAny source)
public static OpenApiDeprecationExtension Parse(JsonNode source)
{
if (source.Node is not JsonObject rawObject) return null;
if (source is not JsonObject rawObject) return null;
var extension = new OpenApiDeprecationExtension();
if (rawObject.TryGetPropertyValue(nameof(RemovalDate).ToFirstCharacterLowerCase(), out var removalDate) && removalDate is JsonNode removalDateValue)
extension.RemovalDate = removalDateValue.GetValue<DateTimeOffset>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
Expand Down Expand Up @@ -44,9 +44,9 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
/// <param name="source">The source element to parse.</param>
/// <returns>The <see cref="OpenApiEnumFlagsExtension"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">When the source element is not an object</exception>
public static OpenApiEnumFlagsExtension Parse(OpenApiAny source)
public static OpenApiEnumFlagsExtension Parse(JsonNode source)
{
if (source.Node is not JsonObject rawObject) throw new ArgumentOutOfRangeException(nameof(source));
if (source is not JsonObject rawObject) throw new ArgumentOutOfRangeException(nameof(source));
var extension = new OpenApiEnumFlagsExtension();
if (rawObject.TryGetPropertyValue(nameof(IsFlags).ToFirstCharacterLowerCase(), out var flagsValue) && flagsValue is JsonNode isFlags)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
/// <param name="source">The source element to parse.</param>
/// <returns>The <see cref="OpenApiEnumValuesDescriptionExtension"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">When the source element is not an object</exception>
public static OpenApiEnumValuesDescriptionExtension Parse(OpenApiAny source)
public static OpenApiEnumValuesDescriptionExtension Parse(JsonNode source)
{
if (source.Node is not JsonObject rawObject) return null;
if (source is not JsonObject rawObject) return null;
var extension = new OpenApiEnumValuesDescriptionExtension();
if (rawObject.TryGetPropertyValue("values", out var values) && values is JsonArray valuesArray)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
/// <param name="source">The source element to parse.</param>
/// <returns>The <see cref="OpenApiPagingExtension"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">When the source element is not an object</exception>
public static OpenApiPagingExtension Parse(OpenApiAny source)
public static OpenApiPagingExtension Parse(JsonNode source)
{
if (source.Node is not JsonObject rawObject) return null;
if (source is not JsonObject rawObject) return null;
var extension = new OpenApiPagingExtension();
if (rawObject.TryGetPropertyValue(nameof(NextLinkName).ToFirstCharacterLowerCase(), out var nextLinkName) && nextLinkName is JsonNode nextLinkNameStr)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
/// </summary>
/// <param name="source">The source object.</param>
/// <returns>The <see cref="OpenApiPrimaryErrorMessageExtension"/>.</returns>
public static OpenApiPrimaryErrorMessageExtension Parse(OpenApiAny source)
public static OpenApiPrimaryErrorMessageExtension Parse(JsonNode source)
{
if (source.Node is not JsonNode rawObject) return null;
if (source is not JsonNode rawObject) return null;
return new()
{
IsPrimaryErrorMessage = rawObject.GetValue<bool>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public bool? IsReserved
/// <param name="source">The source object.</param>
/// <returns>The <see cref="OpenApiReservedParameterExtension"/>.</returns>
/// <returns></returns>
public static OpenApiReservedParameterExtension Parse(OpenApiAny source)
public static OpenApiReservedParameterExtension Parse(JsonNode source)
{
if (source.Node is not JsonNode rawBoolean) return null;
if (source is not JsonNode rawBoolean) return null;
return new()
{
IsReserved = rawBoolean.GetValue<bool>()
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Helpers;
using Microsoft.OpenApi.Interfaces;
Expand Down Expand Up @@ -31,7 +32,7 @@ public class OpenApiExample : IOpenApiReferenceable, IOpenApiExtensible
/// exclusive. To represent examples of media types that cannot naturally represented
/// in JSON or YAML, use a string value to contain the example, escaping where necessary.
/// </summary>
public virtual OpenApiAny Value { get; set; }
public virtual JsonNode Value { get; set; }

/// <summary>
/// A URL that points to the literal example.
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Helpers;
Expand Down Expand Up @@ -77,7 +78,7 @@ public virtual OpenApiSchema Schema
/// <summary>
/// Example of the media type.
/// </summary>
public virtual OpenApiAny Example { get; set; }
public virtual JsonNode Example { get; set; }

/// <summary>
/// Examples of the media type.
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiMediaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Helpers;
using Microsoft.OpenApi.Interfaces;
Expand Down Expand Up @@ -30,7 +31,7 @@ public virtual OpenApiSchema Schema
/// Example of the media type.
/// The example object SHOULD be in the correct format as specified by the media type.
/// </summary>
public OpenApiAny Example { get; set; }
public JsonNode Example { get; set; }

/// <summary>
/// Examples of the media type.
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Helpers;
Expand Down Expand Up @@ -130,7 +131,7 @@ public virtual OpenApiSchema Schema
/// To represent examples of media types that cannot naturally be represented in JSON or YAML,
/// a string value can contain the example with escaping where necessary.
/// </summary>
public virtual OpenApiAny Example { get; set; }
public virtual JsonNode Example { get; set; }

/// <summary>
/// A map containing the representations for the parameter.
Expand Down
17 changes: 9 additions & 8 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Helpers;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;

Expand Down Expand Up @@ -148,7 +149,7 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApi
/// Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level.
/// For example, if type is string, then default can be "foo" but cannot be 1.
/// </summary>
public virtual OpenApiAny Default { get; set; }
public virtual JsonNode Default { get; set; }

/// <summary>
/// Relevant only for Schema "properties" definitions. Declares the property as "read only".
Expand Down Expand Up @@ -269,7 +270,7 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApi
/// To represent examples that cannot be naturally represented in JSON or YAML,
/// a string value can be used to contain the example with escaping where necessary.
/// </summary>
public virtual OpenApiAny Example { get; set; }
public virtual JsonNode Example { get; set; }

/// <summary>
/// A free-form property to include examples of an instance for this schema.
Expand Down Expand Up @@ -359,7 +360,7 @@ public OpenApiSchema(OpenApiSchema schema)
MinLength = schema?.MinLength ?? MinLength;
Pattern = schema?.Pattern ?? Pattern;
MultipleOf = schema?.MultipleOf ?? MultipleOf;
Default = schema?.Default != null ? new(schema?.Default.Node) : null;
Default = schema?.Default != null ? JsonNodeCloneHelper.Clone(schema?.Default) : null;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor Warning

Avoid virtual calls in a constructor or destructor.
ReadOnly = schema?.ReadOnly ?? ReadOnly;
WriteOnly = schema?.WriteOnly ?? WriteOnly;
AllOf = schema?.AllOf != null ? new List<OpenApiSchema>(schema.AllOf) : null;
Expand All @@ -378,7 +379,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 = schema?.Example != null ? new(schema?.Example.Node) : null;
Example = schema?.Example != null ? JsonNodeCloneHelper.Clone(schema?.Example) : null;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor Warning

Avoid virtual calls in a constructor or destructor.
Examples = schema?.Examples != null ? new List<JsonNode>(schema.Examples) : null;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor Warning

Avoid virtual calls in a constructor or destructor.
Enum = schema?.Enum != null ? new List<JsonNode>(schema.Enum) : null;
Nullable = schema?.Nullable ?? Nullable;
Expand Down Expand Up @@ -492,7 +493,7 @@ public void SerializeInternalWithoutReference(IOpenApiWriter writer, OpenApiSpec
writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) => w.WriteValue(s));

// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (nodeWriter, s) => nodeWriter.WriteAny(new OpenApiAny(s)));
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (nodeWriter, s) => nodeWriter.WriteAny(s));

// type
if (Type?.GetType() == typeof(string))
Expand Down Expand Up @@ -605,7 +606,7 @@ internal void WriteV31Properties(IOpenApiWriter writer)
writer.WriteProperty(OpenApiConstants.V31ExclusiveMaximum, V31ExclusiveMaximum);
writer.WriteProperty(OpenApiConstants.V31ExclusiveMinimum, V31ExclusiveMinimum);
writer.WriteProperty(OpenApiConstants.UnevaluatedProperties, UnevaluatedProperties, false);
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (nodeWriter, s) => nodeWriter.WriteAny(new OpenApiAny(s)));
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (nodeWriter, s) => nodeWriter.WriteAny(s));
writer.WriteOptionalMap(OpenApiConstants.PatternProperties, PatternProperties, (w, s) => s.SerializeAsV31(w));
}

Expand Down Expand Up @@ -701,7 +702,7 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer)
writer.WriteProperty(OpenApiConstants.MinItems, MinItems);

// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(new OpenApiAny(s)));
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(s));

// multipleOf
writer.WriteProperty(OpenApiConstants.MultipleOf, MultipleOf);
Expand Down Expand Up @@ -780,7 +781,7 @@ internal void WriteAsSchemaProperties(
writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) => w.WriteValue(s));

// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(new OpenApiAny(s)));
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(s));

// items
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
Expand Down Expand Up @@ -91,7 +92,7 @@ public override string Summary
public override string ExternalValue { get => Target.ExternalValue; set => Target.ExternalValue = value; }

/// <inheritdoc/>
public override OpenApiAny Value { get => Target.Value; set => Target.Value = value; }
public override JsonNode Value { get => Target.Value; set => Target.Value = value; }

/// <inheritdoc/>
public override void SerializeAsV3(IOpenApiWriter writer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
Expand Down Expand Up @@ -97,7 +98,7 @@ public override string Description
public override bool AllowReserved { get => Target.AllowReserved; set => Target.AllowReserved = value; }

/// <inheritdoc/>
public override OpenApiAny Example { get => Target.Example; set => Target.Example = value; }
public override JsonNode Example { get => Target.Example; set => Target.Example = value; }

/// <inheritdoc/>
public override IDictionary<string, OpenApiExample> Examples { get => Target.Examples; set => Target.Examples = value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
Expand Down Expand Up @@ -99,7 +100,7 @@ public override string Description
public override IDictionary<string, OpenApiExample> Examples { get => Target.Examples; set => Target.Examples = value; }

/// <inheritdoc/>
public override OpenApiAny Example { get => Target.Example; set => Target.Example = value; }
public override JsonNode Example { get => Target.Example; set => Target.Example = value; }

/// <inheritdoc/>
public override ParameterLocation? In { get => Target.In; set => Target.In = value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public override string Description
/// <inheritdoc/>
public override decimal? MultipleOf { get => Target.MultipleOf; set => Target.MultipleOf = value; }
/// <inheritdoc/>
public override OpenApiAny Default { get => Target.Default; set => Target.Default = value; }
public override JsonNode Default { get => Target.Default; set => Target.Default = value; }
/// <inheritdoc/>
public override bool ReadOnly { get => Target.ReadOnly; set => Target.ReadOnly = value; }
/// <inheritdoc/>
Expand Down Expand Up @@ -160,7 +160,7 @@ public override string Description
/// <inheritdoc/>
public override OpenApiDiscriminator Discriminator { get => Target.Discriminator; set => Target.Discriminator = value; }
/// <inheritdoc/>
public override OpenApiAny Example { get => Target.Example; set => Target.Example = value; }
public override JsonNode Example { get => Target.Example; set => Target.Example = value; }
/// <inheritdoc/>
public override IList<JsonNode> Examples { get => Target.Examples; set => Target.Examples = value; }
/// <inheritdoc/>
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Models/RuntimeExpressionAnyWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.OpenApi.Models
/// </summary>
public class RuntimeExpressionAnyWrapper : IOpenApiElement
{
private OpenApiAny _any;
private JsonNode _any;
private RuntimeExpression _expression;

/// <summary>
Expand All @@ -35,7 +35,7 @@ public RuntimeExpressionAnyWrapper(RuntimeExpressionAnyWrapper runtimeExpression
/// <summary>
/// Gets/Sets the <see cref="JsonNode"/>
/// </summary>
public OpenApiAny Any
public JsonNode Any
{
get
{
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Reader/OpenApiReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.MicrosoftExtensions;
Expand Down Expand Up @@ -49,7 +50,7 @@ public class OpenApiReaderSettings
/// <summary>
/// Dictionary of parsers for converting extensions into strongly typed classes
/// </summary>
public Dictionary<string, Func<OpenApiAny, OpenApiSpecVersion, IOpenApiExtension>> ExtensionParsers { get; set; } = new();
public Dictionary<string, Func<JsonNode, OpenApiSpecVersion, IOpenApiExtension>> ExtensionParsers { get; set; } = new();

/// <summary>
/// Rules to use for validating OpenAPI specification. If none are provided a default set of rules are applied.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using System;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;

Expand All @@ -13,8 +14,8 @@ internal class AnyFieldMapParameter<T>
/// Constructor.
/// </summary>
public AnyFieldMapParameter(
Func<T, OpenApiAny> propertyGetter,
Action<T, OpenApiAny> propertySetter,
Func<T, JsonNode> propertyGetter,
Action<T, JsonNode> propertySetter,
Func<T, OpenApiSchema> SchemaGetter = null)
{
this.PropertyGetter = propertyGetter;
Expand All @@ -25,12 +26,12 @@ public AnyFieldMapParameter(
/// <summary>
/// Function to retrieve the value of the property.
/// </summary>
public Func<T, OpenApiAny> PropertyGetter { get; }
public Func<T, JsonNode> PropertyGetter { get; }

/// <summary>
/// Function to set the value of the property.
/// </summary>
public Action<T, OpenApiAny> PropertySetter { get; }
public Action<T, JsonNode> PropertySetter { get; }

/// <summary>
/// Function to get the schema to apply to the property.
Expand Down
Loading

0 comments on commit cc1439e

Please sign in to comment.