Skip to content

fix: renames annotations schema property to metadata to match #2241 #2315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 9 additions & 10 deletions src/Microsoft.OpenApi/Interfaces/IMetadataContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@

using System.Collections.Generic;

namespace Microsoft.OpenApi.Interfaces
namespace Microsoft.OpenApi.Interfaces;
/// <summary>
/// Represents an Open API element that can be annotated with
/// non-serializable properties in a property bag.
/// </summary>
public interface IMetadataContainer
{
/// <summary>
/// Represents an Open API element that can be annotated with
/// non-serializable properties in a property bag.
/// A collection of properties associated with the current OpenAPI element to be used by the application.
/// Metadata are NOT (de)serialized with the schema and can be used for custom properties.
/// </summary>
public interface IMetadataContainer
{
/// <summary>
/// A collection of properties associated with the current OpenAPI element.
/// </summary>
Dictionary<string, object>? Metadata { get; set; }
}
Dictionary<string, object>? Metadata { get; set; }
}
7 changes: 1 addition & 6 deletions src/Microsoft.OpenApi/Models/Interfaces/IOpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IOpenApiSchema : IOpenApiDescribedElement, IOpenApiReadOnlyExte
/// </summary>
public string? Title { get; }


/// <summary>
/// $schema, a JSON Schema dialect identifier. Value must be a URI
/// </summary>
Expand Down Expand Up @@ -280,12 +281,6 @@ public interface IOpenApiSchema : IOpenApiDescribedElement, IOpenApiReadOnlyExte
/// </summary>
public Dictionary<string, JsonNode>? UnrecognizedKeywords { get; }

/// <summary>
/// Any annotation to attach to the schema to be used by the application.
/// Annotations are NOT (de)serialized with the schema and can be used for custom properties.
/// </summary>
public Dictionary<string, object>? Annotations { get; }

/// <summary>
/// Follow JSON Schema definition:https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.4
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/// <summary>
/// The Schema Object allows the definition of input and output data types.
/// </summary>
public class OpenApiSchema : IOpenApiExtensible, IOpenApiSchema
public class OpenApiSchema : IOpenApiExtensible, IOpenApiSchema, IMetadataContainer
{
/// <inheritdoc />
public string? Title { get; set; }
Expand Down Expand Up @@ -248,7 +248,7 @@
public Dictionary<string, JsonNode>? UnrecognizedKeywords { get; set; }

/// <inheritdoc />
public Dictionary<string, object>? Annotations { get; set; }
public Dictionary<string, object>? Metadata { get; set; }

/// <inheritdoc />
public Dictionary<string, HashSet<string>>? DependentRequired { get; set; }
Expand All @@ -262,7 +262,7 @@
/// Initializes a copy of <see cref="IOpenApiSchema"/> object
/// </summary>
/// <param name="schema">The schema object to copy from.</param>
internal OpenApiSchema(IOpenApiSchema schema)

Check warning on line 265 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this constructor to reduce its Cognitive Complexity from 20 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
Utils.CheckArgumentNull(schema);
Title = schema.Title ?? Title;
Expand Down Expand Up @@ -317,7 +317,7 @@
Deprecated = schema.Deprecated;
Xml = schema.Xml != null ? new(schema.Xml) : null;
Extensions = schema.Extensions != null ? new Dictionary<string, IOpenApiExtension>(schema.Extensions) : null;
Annotations = schema.Annotations != null ? new Dictionary<string, object>(schema.Annotations) : null;
Metadata = schema is IMetadataContainer { Metadata: not null } mContainer ? new Dictionary<string, object>(mContainer.Metadata) : null;
UnrecognizedKeywords = schema.UnrecognizedKeywords != null ? new Dictionary<string, JsonNode>(schema.UnrecognizedKeywords) : null;
DependentRequired = schema.DependentRequired != null ? new Dictionary<string, HashSet<string>>(schema.DependentRequired) : null;
}
Expand All @@ -334,23 +334,23 @@
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
}

private static void SerializeBounds(IOpenApiWriter writer, OpenApiSpecVersion version, string propertyName, string exclusivePropertyName, string isExclusivePropertyName, string? value, string? exclusiveValue, bool? isExclusiveValue)

Check warning on line 337 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 337 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Method has 8 parameters, which is greater than the 7 authorized. (https://rules.sonarsource.com/csharp/RSPEC-107)
{
if (version >= OpenApiSpecVersion.OpenApi3_1)
{
if (!string.IsNullOrEmpty(exclusiveValue) && exclusiveValue is not null)

Check warning on line 341 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// was explicitly set in the document or object model
writer.WritePropertyName(exclusivePropertyName);
writer.WriteRaw(exclusiveValue);
}
else if (isExclusiveValue == true && !string.IsNullOrEmpty(value) && value is not null)

Check warning on line 347 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// came from parsing an old document
writer.WritePropertyName(exclusivePropertyName);
writer.WriteRaw(value);
}
else if (!string.IsNullOrEmpty(value) && value is not null)

Check warning on line 353 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// was explicitly set in the document or object model
writer.WritePropertyName(propertyName);
Expand All @@ -359,14 +359,14 @@
}
else
{
if (!string.IsNullOrEmpty(exclusiveValue) && exclusiveValue is not null)

Check warning on line 362 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// was explicitly set in a new document being downcast or object model
writer.WritePropertyName(propertyName);
writer.WriteRaw(exclusiveValue);
writer.WriteProperty(isExclusivePropertyName, true);
}
else if (!string.IsNullOrEmpty(value) && value is not null)

Check warning on line 369 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// came from parsing an old document, we're just mirroring the information
writer.WritePropertyName(propertyName);
Expand Down Expand Up @@ -428,7 +428,7 @@
// required
writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) =>
{
if (!string.IsNullOrEmpty(s) && s is not null)

Check warning on line 431 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
w.WriteValue(s);
}
Expand Down Expand Up @@ -749,7 +749,7 @@
writer.WriteEndObject();
}

private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer, OpenApiSpecVersion version)

Check warning on line 752 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
// check whether nullable is true for upcasting purposes
var isNullable = (Type.HasValue && Type.Value.HasFlag(JsonSchemaType.Null)) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ public string? Description
/// <inheritdoc/>
public Dictionary<string, JsonNode>? UnrecognizedKeywords { get => Target?.UnrecognizedKeywords; }

/// <inheritdoc/>
public Dictionary<string, object>? Annotations { get => Target?.Annotations; }

/// <inheritdoc/>
public Dictionary<string, HashSet<string>>? DependentRequired { get => Target?.DependentRequired; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public async Task LoadDocumentWithExternalReferencesInSubDirectories()
Assert.Equal(JsonSchemaType.Array, petsSchema.Type);

var petSchema = petsSchema.Items;
Assert.IsType<OpenApiSchemaReference>(petSchema);
var petSchemaReference = Assert.IsType<OpenApiSchemaReference>(petSchema);
var petSchemaTarget = petSchemaReference.RecursiveTarget;
Assert.NotNull(petSchemaTarget);

Assert.Equivalent(new OpenApiSchema
{
Expand All @@ -125,7 +127,7 @@ public async Task LoadDocumentWithExternalReferencesInSubDirectories()
Type = JsonSchemaType.String
}
}
}, petSchema);
}, petSchemaTarget);
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class OpenApiDocumentTests
["property1"] = new OpenApiSchema()
{
Type = JsonSchemaType.String,
Annotations = new Dictionary<string, object> { { "key1", "value" } }
Metadata = new Dictionary<string, object> { { "key1", "value" } }
}
}
},
Expand All @@ -55,10 +55,10 @@ public class OpenApiDocumentTests
["property1"] = new OpenApiSchema()
{
Type = JsonSchemaType.String,
Annotations = new Dictionary<string, object> { { "key1", "value" } }
Metadata = new Dictionary<string, object> { { "key1", "value" } }
}
},
Annotations = new Dictionary<string, object> { { "key1", "value" } },
Metadata = new Dictionary<string, object> { { "key1", "value" } },
},
["schema2"] = new OpenApiSchema()
{
Expand Down
14 changes: 7 additions & 7 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class OpenApiSchemaTests
{
Url = new("http://example.com/externalDocs")
},
Annotations = new Dictionary<string, object> { { "key1", "value1" }, { "key2", 2 } }
Metadata = new Dictionary<string, object> { { "key1", "value1" }, { "key2", 2 } }
};

public static readonly OpenApiSchema AdvancedSchemaObject = new()
Expand Down Expand Up @@ -473,24 +473,24 @@ public void OpenApiSchemaCopyConstructorSucceeds()
}

[Fact]
public void OpenApiSchemaCopyConstructorWithAnnotationsSucceeds()
public void OpenApiSchemaCopyConstructorWithMetadataSucceeds()
{
var baseSchema = new OpenApiSchema
{
Annotations = new Dictionary<string, object>
Metadata = new Dictionary<string, object>
{
["key1"] = "value1",
["key2"] = 2
}
};

var actualSchema = baseSchema.CreateShallowCopy();
var actualSchema = Assert.IsType<OpenApiSchema>(baseSchema.CreateShallowCopy());

Assert.Equal(baseSchema.Annotations["key1"], actualSchema.Annotations["key1"]);
Assert.Equal(baseSchema.Metadata["key1"], actualSchema.Metadata["key1"]);

baseSchema.Annotations["key1"] = "value2";
baseSchema.Metadata["key1"] = "value2";

Assert.NotEqual(baseSchema.Annotations["key1"], actualSchema.Annotations["key1"]);
Assert.NotEqual(baseSchema.Metadata["key1"], actualSchema.Metadata["key1"]);
}

public static TheoryData<JsonNode> SchemaExamples()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ namespace Microsoft.OpenApi.Models.Interfaces
Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema? AdditionalProperties { get; }
bool AdditionalPropertiesAllowed { get; }
System.Collections.Generic.List<Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema>? AllOf { get; }
System.Collections.Generic.Dictionary<string, object>? Annotations { get; }
System.Collections.Generic.List<Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema>? AnyOf { get; }
string? Comment { get; }
string? Const { get; }
Expand Down Expand Up @@ -1010,13 +1009,12 @@ namespace Microsoft.OpenApi.Models
public OpenApiResponses() { }
public OpenApiResponses(Microsoft.OpenApi.Models.OpenApiResponses openApiResponses) { }
}
public class OpenApiSchema : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReferenceable, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Interfaces.IShallowCopyable<Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema>, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema
public class OpenApiSchema : Microsoft.OpenApi.Interfaces.IMetadataContainer, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReferenceable, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Interfaces.IShallowCopyable<Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema>, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema
{
public OpenApiSchema() { }
public Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema? AdditionalProperties { get; set; }
public bool AdditionalPropertiesAllowed { get; set; }
public System.Collections.Generic.List<Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema>? AllOf { get; set; }
public System.Collections.Generic.Dictionary<string, object>? Annotations { get; set; }
public System.Collections.Generic.List<Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema>? AnyOf { get; set; }
public string? Comment { get; set; }
public string? Const { get; set; }
Expand All @@ -1042,6 +1040,7 @@ namespace Microsoft.OpenApi.Models
public int? MaxLength { get; set; }
public int? MaxProperties { get; set; }
public string? Maximum { get; set; }
public System.Collections.Generic.Dictionary<string, object>? Metadata { get; set; }
public int? MinItems { get; set; }
public int? MinLength { get; set; }
public int? MinProperties { get; set; }
Expand Down Expand Up @@ -1348,7 +1347,6 @@ namespace Microsoft.OpenApi.Models.References
public Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema? AdditionalProperties { get; }
public bool AdditionalPropertiesAllowed { get; }
public System.Collections.Generic.List<Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema>? AllOf { get; }
public System.Collections.Generic.Dictionary<string, object>? Annotations { get; }
public System.Collections.Generic.List<Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema>? AnyOf { get; }
public string? Comment { get; }
public string? Const { get; }
Expand Down