diff --git a/src/Microsoft.OpenApi/Interfaces/IMetadataContainer.cs b/src/Microsoft.OpenApi/Interfaces/IMetadataContainer.cs index d97635c9c..4407577df 100644 --- a/src/Microsoft.OpenApi/Interfaces/IMetadataContainer.cs +++ b/src/Microsoft.OpenApi/Interfaces/IMetadataContainer.cs @@ -3,17 +3,16 @@ using System.Collections.Generic; -namespace Microsoft.OpenApi.Interfaces +namespace Microsoft.OpenApi.Interfaces; +/// +/// Represents an Open API element that can be annotated with +/// non-serializable properties in a property bag. +/// +public interface IMetadataContainer { /// - /// 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. /// - public interface IMetadataContainer - { - /// - /// A collection of properties associated with the current OpenAPI element. - /// - Dictionary? Metadata { get; set; } - } + Dictionary? Metadata { get; set; } } diff --git a/src/Microsoft.OpenApi/Models/Interfaces/IOpenApiSchema.cs b/src/Microsoft.OpenApi/Models/Interfaces/IOpenApiSchema.cs index ea6f4edd3..8ef1dec1a 100644 --- a/src/Microsoft.OpenApi/Models/Interfaces/IOpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/Interfaces/IOpenApiSchema.cs @@ -17,6 +17,7 @@ public interface IOpenApiSchema : IOpenApiDescribedElement, IOpenApiReadOnlyExte /// public string? Title { get; } + /// /// $schema, a JSON Schema dialect identifier. Value must be a URI /// @@ -280,12 +281,6 @@ public interface IOpenApiSchema : IOpenApiDescribedElement, IOpenApiReadOnlyExte /// public Dictionary? UnrecognizedKeywords { get; } - /// - /// 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. - /// - public Dictionary? Annotations { get; } - /// /// Follow JSON Schema definition:https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.4 /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index 28c3e30c9..94d0d9299 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -18,7 +18,7 @@ namespace Microsoft.OpenApi.Models /// /// The Schema Object allows the definition of input and output data types. /// - public class OpenApiSchema : IOpenApiExtensible, IOpenApiSchema + public class OpenApiSchema : IOpenApiExtensible, IOpenApiSchema, IMetadataContainer { /// public string? Title { get; set; } @@ -248,7 +248,7 @@ public string? Minimum public Dictionary? UnrecognizedKeywords { get; set; } /// - public Dictionary? Annotations { get; set; } + public Dictionary? Metadata { get; set; } /// public Dictionary>? DependentRequired { get; set; } @@ -317,7 +317,7 @@ internal OpenApiSchema(IOpenApiSchema schema) Deprecated = schema.Deprecated; Xml = schema.Xml != null ? new(schema.Xml) : null; Extensions = schema.Extensions != null ? new Dictionary(schema.Extensions) : null; - Annotations = schema.Annotations != null ? new Dictionary(schema.Annotations) : null; + Metadata = schema is IMetadataContainer { Metadata: not null } mContainer ? new Dictionary(mContainer.Metadata) : null; UnrecognizedKeywords = schema.UnrecognizedKeywords != null ? new Dictionary(schema.UnrecognizedKeywords) : null; DependentRequired = schema.DependentRequired != null ? new Dictionary>(schema.DependentRequired) : null; } diff --git a/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs b/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs index 7dd3ea3e5..6c191c8e5 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs @@ -139,9 +139,6 @@ public string? Description /// public Dictionary? UnrecognizedKeywords { get => Target?.UnrecognizedKeywords; } - /// - public Dictionary? Annotations { get => Target?.Annotations; } - /// public Dictionary>? DependentRequired { get => Target?.DependentRequired; } diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs index 671a4a9eb..a8eb30cc3 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs @@ -104,7 +104,9 @@ public async Task LoadDocumentWithExternalReferencesInSubDirectories() Assert.Equal(JsonSchemaType.Array, petsSchema.Type); var petSchema = petsSchema.Items; - Assert.IsType(petSchema); + var petSchemaReference = Assert.IsType(petSchema); + var petSchemaTarget = petSchemaReference.RecursiveTarget; + Assert.NotNull(petSchemaTarget); Assert.Equivalent(new OpenApiSchema { @@ -125,7 +127,7 @@ public async Task LoadDocumentWithExternalReferencesInSubDirectories() Type = JsonSchemaType.String } } - }, petSchema); + }, petSchemaTarget); } } diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs index 7b391c401..2774680a7 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs @@ -36,7 +36,7 @@ public class OpenApiDocumentTests ["property1"] = new OpenApiSchema() { Type = JsonSchemaType.String, - Annotations = new Dictionary { { "key1", "value" } } + Metadata = new Dictionary { { "key1", "value" } } } } }, @@ -55,10 +55,10 @@ public class OpenApiDocumentTests ["property1"] = new OpenApiSchema() { Type = JsonSchemaType.String, - Annotations = new Dictionary { { "key1", "value" } } + Metadata = new Dictionary { { "key1", "value" } } } }, - Annotations = new Dictionary { { "key1", "value" } }, + Metadata = new Dictionary { { "key1", "value" } }, }, ["schema2"] = new OpenApiSchema() { diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs index 6e2f00656..779a87e3e 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs @@ -38,7 +38,7 @@ public class OpenApiSchemaTests { Url = new("http://example.com/externalDocs") }, - Annotations = new Dictionary { { "key1", "value1" }, { "key2", 2 } } + Metadata = new Dictionary { { "key1", "value1" }, { "key2", 2 } } }; public static readonly OpenApiSchema AdvancedSchemaObject = new() @@ -473,24 +473,24 @@ public void OpenApiSchemaCopyConstructorSucceeds() } [Fact] - public void OpenApiSchemaCopyConstructorWithAnnotationsSucceeds() + public void OpenApiSchemaCopyConstructorWithMetadataSucceeds() { var baseSchema = new OpenApiSchema { - Annotations = new Dictionary + Metadata = new Dictionary { ["key1"] = "value1", ["key2"] = 2 } }; - var actualSchema = baseSchema.CreateShallowCopy(); + var actualSchema = Assert.IsType(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 SchemaExamples() diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index 4a8fcea74..2ec1fb15b 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -415,7 +415,6 @@ namespace Microsoft.OpenApi.Models.Interfaces Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema? AdditionalProperties { get; } bool AdditionalPropertiesAllowed { get; } System.Collections.Generic.List? AllOf { get; } - System.Collections.Generic.Dictionary? Annotations { get; } System.Collections.Generic.List? AnyOf { get; } string? Comment { get; } string? Const { get; } @@ -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.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.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? AllOf { get; set; } - public System.Collections.Generic.Dictionary? Annotations { get; set; } public System.Collections.Generic.List? AnyOf { get; set; } public string? Comment { get; set; } public string? Const { get; set; } @@ -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? Metadata { get; set; } public int? MinItems { get; set; } public int? MinLength { get; set; } public int? MinProperties { get; set; } @@ -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? AllOf { get; } - public System.Collections.Generic.Dictionary? Annotations { get; } public System.Collections.Generic.List? AnyOf { get; } public string? Comment { get; } public string? Const { get; }