diff --git a/src/Microsoft.OpenApi/Models/Interfaces/IOpenApiParameter.cs b/src/Microsoft.OpenApi/Models/Interfaces/IOpenApiParameter.cs index 9044f26a9..363cc1cd4 100644 --- a/src/Microsoft.OpenApi/Models/Interfaces/IOpenApiParameter.cs +++ b/src/Microsoft.OpenApi/Models/Interfaces/IOpenApiParameter.cs @@ -5,7 +5,7 @@ namespace Microsoft.OpenApi.Models.Interfaces; /// -/// Defines the base properties for the example object. +/// Defines the base properties for the parameter object. /// This interface is provided for type assertions but should not be implemented by package consumers beyond automatic mocking. /// public interface IOpenApiParameter : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible diff --git a/src/Microsoft.OpenApi/Models/References/BaseOpenApiReferenceHolder.cs b/src/Microsoft.OpenApi/Models/References/BaseOpenApiReferenceHolder.cs new file mode 100644 index 000000000..6b1f02575 --- /dev/null +++ b/src/Microsoft.OpenApi/Models/References/BaseOpenApiReferenceHolder.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.OpenApi.Interfaces; +using Microsoft.OpenApi.Writers; + +namespace Microsoft.OpenApi.Models.References; +/// +/// Base class for OpenApiReferenceHolder. +/// +/// The concrete class implementation type for the model. +/// The interface type for the model. +public abstract class BaseOpenApiReferenceHolder : IOpenApiReferenceHolder where T : class, IOpenApiReferenceable, V where V : IOpenApiSerializable +{ + internal T _target; + /// + public T Target + { + get + { + _target ??= Reference.HostDocument.ResolveReferenceTo(Reference); + return _target; + } + } + /// + /// Copy constructor + /// + /// The parameter reference to copy + protected BaseOpenApiReferenceHolder(BaseOpenApiReferenceHolder source) + { + Utils.CheckArgumentNull(source); + Reference = source.Reference != null ? new(source.Reference) : null; + UnresolvedReference = source.UnresolvedReference; + //no need to copy summary and description as if they are not overridden, they will be fetched from the target + //if they are, the reference copy will handle it + } + private protected BaseOpenApiReferenceHolder(T target, string referenceId, ReferenceType referenceType) + { + _target = target; + + Reference = new OpenApiReference() + { + Id = referenceId, + Type = referenceType, + }; + } + /// + /// Constructor initializing the reference object. + /// + /// The reference Id. + /// The host OpenAPI document. + /// The reference type. + /// Optional: External resource in the reference. + /// It may be: + /// 1. a absolute/relative file path, for example: ../commons/pet.json + /// 2. a Url, for example: http://localhost/pet.json + /// + protected BaseOpenApiReferenceHolder(string referenceId, OpenApiDocument hostDocument, ReferenceType referenceType, string externalResource = null) + { + Utils.CheckArgumentNullOrEmpty(referenceId); + + Reference = new OpenApiReference() + { + Id = referenceId, + HostDocument = hostDocument, + Type = referenceType, + ExternalResource = externalResource + }; + } + /// + public bool UnresolvedReference { get; set; } + /// + public OpenApiReference Reference { get; set; } + /// + public abstract V CopyReferenceAsTargetElementWithOverrides(V source); + /// + public void SerializeAsV3(IOpenApiWriter writer) + { + if (!writer.GetSettings().ShouldInlineReference(Reference)) + { + Reference.SerializeAsV3(writer); + } + else + { + SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV3(writer)); + } + } + + /// + public void SerializeAsV31(IOpenApiWriter writer) + { + if (!writer.GetSettings().ShouldInlineReference(Reference)) + { + Reference.SerializeAsV31(writer); + } + else + { + SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV31(writer)); + } + } + + /// + public virtual void SerializeAsV2(IOpenApiWriter writer) + { + if (!writer.GetSettings().ShouldInlineReference(Reference)) + { + Reference.SerializeAsV2(writer); + } + else + { + SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV2(writer)); + } + } + + /// + /// Serialize the reference as a reference or the target object. + /// This method is used to accelerate the serialization methods implementations. + /// + /// The OpenApiWriter. + /// The action to serialize the target object. + private protected void SerializeInternal(IOpenApiWriter writer, + Action action) + { + Utils.CheckArgumentNull(writer); + action(writer, Target); + } +} diff --git a/src/Microsoft.OpenApi/Models/References/OpenApiCallbackReference.cs b/src/Microsoft.OpenApi/Models/References/OpenApiCallbackReference.cs index f357c4532..14b96c9e6 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiCallbackReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiCallbackReference.cs @@ -13,32 +13,8 @@ namespace Microsoft.OpenApi.Models.References /// /// Callback Object Reference: A reference to a map of possible out-of band callbacks related to the parent operation. /// - public class OpenApiCallbackReference : IOpenApiCallback, IOpenApiReferenceHolder + public class OpenApiCallbackReference : BaseOpenApiReferenceHolder, IOpenApiCallback { -#nullable enable - internal OpenApiCallback _target; - /// - public OpenApiReference Reference { get; set; } - - /// - public bool UnresolvedReference { get; set; } - - /// - /// Gets the target callback. - /// - /// - /// If the reference is not resolved, this will return null. - /// - public OpenApiCallback Target -#nullable restore - { - get - { - _target ??= Reference.HostDocument.ResolveReferenceTo(Reference); - return _target; - } - } - /// /// Constructor initializing the reference object. /// @@ -49,39 +25,20 @@ public OpenApiCallback Target /// 1. an absolute/relative file path, for example: ../commons/pet.json /// 2. a Url, for example: http://localhost/pet.json /// - public OpenApiCallbackReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null) + public OpenApiCallbackReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null):base(referenceId, hostDocument, ReferenceType.Callback, externalResource) { - Utils.CheckArgumentNullOrEmpty(referenceId); - - Reference = new OpenApiReference() - { - Id = referenceId, - HostDocument = hostDocument, - Type = ReferenceType.Callback, - ExternalResource = externalResource - }; } /// /// Copy constructor /// /// The callback reference to copy - public OpenApiCallbackReference(OpenApiCallbackReference callback) + public OpenApiCallbackReference(OpenApiCallbackReference callback):base(callback) { - Utils.CheckArgumentNull(callback); - Reference = callback.Reference != null ? new(callback.Reference) : null; - UnresolvedReference = callback.UnresolvedReference; } - internal OpenApiCallbackReference(OpenApiCallback target, string referenceId) + internal OpenApiCallbackReference(OpenApiCallback target, string referenceId):base(target, referenceId, ReferenceType.Callback) { - _target = target; - - Reference = new OpenApiReference() - { - Id = referenceId, - Type = ReferenceType.Callback, - }; } /// @@ -91,52 +48,16 @@ internal OpenApiCallbackReference(OpenApiCallback target, string referenceId) public IDictionary Extensions { get => Target?.Extensions; } /// - public void SerializeAsV3(IOpenApiWriter writer) + public override IOpenApiCallback CopyReferenceAsTargetElementWithOverrides(IOpenApiCallback source) { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV3(writer); - } - else - { - SerializeInternal(writer, (writer, element) => element.SerializeAsV3(writer)); - } - } - - /// - public void SerializeAsV31(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV31(writer); - } - else - { - SerializeInternal(writer, (writer, element) => element.SerializeAsV31(writer)); - } - } - - /// - public IOpenApiCallback CopyReferenceAsTargetElementWithOverrides(IOpenApiCallback source) - { - // the copy here is never called since callbacks do not have any overridable fields. - // if the spec evolves to include overridable fields for callbacks, the serialize methods will need to call this copy method. return source is OpenApiCallback ? new OpenApiCallback(this) : source; } /// - public void SerializeAsV2(IOpenApiWriter writer) + public override void SerializeAsV2(IOpenApiWriter writer) { // examples components are not supported in OAS 2.0 Reference.SerializeAsV2(writer); } - - /// - private void SerializeInternal(IOpenApiWriter writer, - Action action) - { - Utils.CheckArgumentNull(writer); - action(writer, Target); - } } } diff --git a/src/Microsoft.OpenApi/Models/References/OpenApiExampleReference.cs b/src/Microsoft.OpenApi/Models/References/OpenApiExampleReference.cs index 9f1842001..9a1c5ae16 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiExampleReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiExampleReference.cs @@ -13,30 +13,8 @@ namespace Microsoft.OpenApi.Models.References /// /// Example Object Reference. /// - public class OpenApiExampleReference : IOpenApiReferenceHolder, IOpenApiExample + public class OpenApiExampleReference : BaseOpenApiReferenceHolder, IOpenApiExample { - /// - public OpenApiReference Reference { get; set; } - - /// - public bool UnresolvedReference { get; set; } - internal OpenApiExample _target; - - /// - /// Gets the target example. - /// - /// - /// If the reference is not resolved, this will return null. - /// - public OpenApiExample Target - { - get - { - _target ??= Reference.HostDocument.ResolveReferenceTo(Reference); - return _target; - } - } - /// /// Constructor initializing the reference object. /// @@ -47,41 +25,20 @@ public OpenApiExample Target /// 1. a absolute/relative file path, for example: ../commons/pet.json /// 2. a Url, for example: http://localhost/pet.json /// - public OpenApiExampleReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null) + public OpenApiExampleReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null):base(referenceId, hostDocument, ReferenceType.Example, externalResource) { - Utils.CheckArgumentNullOrEmpty(referenceId); - - Reference = new OpenApiReference() - { - Id = referenceId, - HostDocument = hostDocument, - Type = ReferenceType.Example, - ExternalResource = externalResource - }; } /// /// Copy constructor /// /// The reference to copy. - public OpenApiExampleReference(OpenApiExampleReference example) + public OpenApiExampleReference(OpenApiExampleReference example):base(example) { - Utils.CheckArgumentNull(example); - Reference = example.Reference != null ? new(example.Reference) : null; - UnresolvedReference = example.UnresolvedReference; - //no need to copy summary and description as if they are not overridden, they will be fetched from the target - //if they are, the reference copy will handle it } - internal OpenApiExampleReference(OpenApiExample target, string referenceId) + internal OpenApiExampleReference(OpenApiExample target, string referenceId):base(target, referenceId, ReferenceType.Example) { - _target = target; - - Reference = new OpenApiReference() - { - Id = referenceId, - Type = ReferenceType.Example, - }; } /// @@ -120,50 +77,16 @@ public string Summary public JsonNode Value { get => Target?.Value; } /// - public void SerializeAsV3(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV3(writer); - } - else - { - SerializeInternal(writer, (writer, referenceElement) => CopyReferenceAsTargetElementWithOverrides(referenceElement).SerializeAsV3(writer)); - } - } - - /// - public void SerializeAsV31(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV31(writer); - } - else - { - SerializeInternal(writer, (writer, referenceElement) => CopyReferenceAsTargetElementWithOverrides(referenceElement).SerializeAsV31(writer)); - } - } - - /// - public IOpenApiExample CopyReferenceAsTargetElementWithOverrides(IOpenApiExample source) + public override IOpenApiExample CopyReferenceAsTargetElementWithOverrides(IOpenApiExample source) { return source is OpenApiExample ? new OpenApiExample(this) : source; } /// - public void SerializeAsV2(IOpenApiWriter writer) + public override void SerializeAsV2(IOpenApiWriter writer) { // examples components are not supported in OAS 2.0 Reference.SerializeAsV2(writer); } - - /// - private void SerializeInternal(IOpenApiWriter writer, - Action action) - { - Utils.CheckArgumentNull(writer); - action(writer, Target); - } } } diff --git a/src/Microsoft.OpenApi/Models/References/OpenApiHeaderReference.cs b/src/Microsoft.OpenApi/Models/References/OpenApiHeaderReference.cs index 71e8cace0..dfbee5ce7 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiHeaderReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiHeaderReference.cs @@ -13,29 +13,8 @@ namespace Microsoft.OpenApi.Models.References /// /// Header Object Reference. /// - public class OpenApiHeaderReference : IOpenApiHeader, IOpenApiReferenceHolder + public class OpenApiHeaderReference : BaseOpenApiReferenceHolder, IOpenApiHeader { - /// - public OpenApiReference Reference { get; set; } - - /// - public bool UnresolvedReference { get; set; } - internal OpenApiHeader _target; - /// - /// Gets the target header. - /// - /// - /// If the reference is not resolved, this will return null. - /// - public OpenApiHeader Target - { - get - { - _target ??= Reference.HostDocument.ResolveReferenceTo(Reference); - return _target; - } - } - /// /// Constructor initializing the reference object. /// @@ -46,41 +25,20 @@ public OpenApiHeader Target /// 1. a absolute/relative file path, for example: ../commons/pet.json /// 2. a Url, for example: http://localhost/pet.json /// - public OpenApiHeaderReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null) + public OpenApiHeaderReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null):base(referenceId, hostDocument, ReferenceType.Header, externalResource) { - Utils.CheckArgumentNullOrEmpty(referenceId); - - Reference = new OpenApiReference() - { - Id = referenceId, - HostDocument = hostDocument, - Type = ReferenceType.Header, - ExternalResource = externalResource - }; } /// /// Copy constructor /// /// The object to copy - public OpenApiHeaderReference(OpenApiHeaderReference header) + public OpenApiHeaderReference(OpenApiHeaderReference header):base(header) { - Utils.CheckArgumentNull(header); - Reference = header.Reference != null ? new(header.Reference) : null; - UnresolvedReference = header.UnresolvedReference; - //no need to copy description as if they are not overridden, they will be fetched from the target - //if they are, the reference copy will handle it } - internal OpenApiHeaderReference(OpenApiHeader target, string referenceId) + internal OpenApiHeaderReference(OpenApiHeader target, string referenceId):base(target, referenceId, ReferenceType.Header) { - _target = target; - - Reference = new OpenApiReference() - { - Id = referenceId, - Type = ReferenceType.Header, - }; } /// @@ -130,55 +88,9 @@ public string Description public IDictionary Extensions { get => Target?.Extensions; } /// - public void SerializeAsV31(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV31(writer); - } - else - { - SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV31(writer)); - } - } - - /// - public void SerializeAsV3(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV3(writer); - } - else - { - SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV3(writer)); - } - } - - /// - public void SerializeAsV2(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV2(writer); - } - else - { - SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV2(writer)); - } - } - /// - public IOpenApiHeader CopyReferenceAsTargetElementWithOverrides(IOpenApiHeader source) + public override IOpenApiHeader CopyReferenceAsTargetElementWithOverrides(IOpenApiHeader source) { return source is OpenApiHeader ? new OpenApiHeader(this) : source; } - - /// - private void SerializeInternal(IOpenApiWriter writer, - Action action) - { - Utils.CheckArgumentNull(writer); - action(writer, Target); - } } } diff --git a/src/Microsoft.OpenApi/Models/References/OpenApiLinkReference.cs b/src/Microsoft.OpenApi/Models/References/OpenApiLinkReference.cs index 57a4b1e4f..f177bee2c 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiLinkReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiLinkReference.cs @@ -12,29 +12,8 @@ namespace Microsoft.OpenApi.Models.References /// /// Link Object Reference. /// - public class OpenApiLinkReference : IOpenApiLink, IOpenApiReferenceHolder + public class OpenApiLinkReference : BaseOpenApiReferenceHolder, IOpenApiLink { - /// - public OpenApiReference Reference { get; set; } - - /// - public bool UnresolvedReference { get; set; } - internal OpenApiLink _target; - /// - /// Gets the target link. - /// - /// - /// If the reference is not resolved, this will return null. - /// - public OpenApiLink Target - { - get - { - _target ??= Reference.HostDocument.ResolveReferenceTo(Reference); - return _target; - } - } - /// /// Constructor initializing the reference object. /// @@ -45,41 +24,18 @@ public OpenApiLink Target /// 1. a absolute/relative file path, for example: ../commons/pet.json /// 2. a Url, for example: http://localhost/pet.json /// - public OpenApiLinkReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null) + public OpenApiLinkReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null):base(referenceId, hostDocument, ReferenceType.Link, externalResource) { - Utils.CheckArgumentNullOrEmpty(referenceId); - - Reference = new OpenApiReference() - { - Id = referenceId, - HostDocument = hostDocument, - Type = ReferenceType.Link, - ExternalResource = externalResource - }; } /// /// Copy constructor. /// /// The reference to copy - public OpenApiLinkReference(OpenApiLinkReference reference) + public OpenApiLinkReference(OpenApiLinkReference reference):base(reference) { - Utils.CheckArgumentNull(reference); - - Reference = reference.Reference != null ? new(reference.Reference) : null; - UnresolvedReference = reference.UnresolvedReference; - //no need to copy summary and description as if they are not overridden, they will be fetched from the target - //if they are, the reference copy will handle it } - - internal OpenApiLinkReference(OpenApiLink target, string referenceId) + internal OpenApiLinkReference(OpenApiLink target, string referenceId):base(target, referenceId, ReferenceType.Link) { - _target = target; - - Reference = new OpenApiReference() - { - Id = referenceId, - Type = ReferenceType.Link, - }; } /// @@ -114,49 +70,15 @@ public string Description public IDictionary Extensions { get => Target?.Extensions; } /// - public void SerializeAsV3(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV3(writer); - } - else - { - SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV3(writer)); - } - } - - /// - public void SerializeAsV31(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV31(writer); - } - else - { - SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV31(writer)); - } - } - - /// - public void SerializeAsV2(IOpenApiWriter writer) + public override void SerializeAsV2(IOpenApiWriter writer) { // Link object does not exist in V2. } /// - public IOpenApiLink CopyReferenceAsTargetElementWithOverrides(IOpenApiLink source) + public override IOpenApiLink CopyReferenceAsTargetElementWithOverrides(IOpenApiLink source) { return source is OpenApiLink ? new OpenApiLink(this) : source; } - - /// - private void SerializeInternal(IOpenApiWriter writer, - Action action) - { - Utils.CheckArgumentNull(writer); - action(writer, Target); - } } } diff --git a/src/Microsoft.OpenApi/Models/References/OpenApiParameterReference.cs b/src/Microsoft.OpenApi/Models/References/OpenApiParameterReference.cs index da9d1dabd..82c73afda 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiParameterReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiParameterReference.cs @@ -1,42 +1,18 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. -using System; using System.Collections.Generic; using System.Text.Json.Nodes; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models.Interfaces; -using Microsoft.OpenApi.Writers; namespace Microsoft.OpenApi.Models.References { /// /// Parameter Object Reference. /// - public class OpenApiParameterReference : IOpenApiParameter, IOpenApiReferenceHolder + public class OpenApiParameterReference : BaseOpenApiReferenceHolder, IOpenApiParameter { - /// - public OpenApiReference Reference { get; set; } - - /// - public bool UnresolvedReference { get; set; } - internal OpenApiParameter _target; - - /// - /// Gets the target parameter. - /// - /// - /// If the reference is not resolved, this will return null. - /// - public OpenApiParameter Target - { - get - { - _target ??= Reference.HostDocument.ResolveReferenceTo(Reference); - return _target; - } - } - /// /// Constructor initializing the reference object. /// @@ -47,41 +23,20 @@ public OpenApiParameter Target /// 1. a absolute/relative file path, for example: ../commons/pet.json /// 2. a Url, for example: http://localhost/pet.json /// - public OpenApiParameterReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null) + public OpenApiParameterReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null):base(referenceId, hostDocument, ReferenceType.Parameter, externalResource) { - Utils.CheckArgumentNullOrEmpty(referenceId); - - Reference = new OpenApiReference() - { - Id = referenceId, - HostDocument = hostDocument, - Type = ReferenceType.Parameter, - ExternalResource = externalResource - }; } /// /// Copy constructor /// /// The parameter reference to copy - public OpenApiParameterReference(OpenApiParameterReference parameter) + public OpenApiParameterReference(OpenApiParameterReference parameter):base(parameter) { - Utils.CheckArgumentNull(parameter); - Reference = parameter.Reference != null ? new(parameter.Reference) : null; - UnresolvedReference = parameter.UnresolvedReference; - //no need to copy summary and description as if they are not overridden, they will be fetched from the target - //if they are, the reference copy will handle it } - internal OpenApiParameterReference(OpenApiParameter target, string referenceId) + internal OpenApiParameterReference(OpenApiParameter target, string referenceId):base(target, referenceId, ReferenceType.Parameter) { - _target = target; - - Reference = new OpenApiReference() - { - Id = referenceId, - Type = ReferenceType.Parameter, - }; } /// @@ -137,56 +92,9 @@ public string Description public IDictionary Extensions { get => Target.Extensions; } /// - public void SerializeAsV3(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV3(writer); - } - else - { - SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV3(writer)); - } - } - - /// - public void SerializeAsV31(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV31(writer); - } - else - { - SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV31(writer)); - } - } - - /// - public void SerializeAsV2(IOpenApiWriter writer) - { - if (!writer.GetSettings().ShouldInlineReference(Reference)) - { - Reference.SerializeAsV2(writer); - } - else - { - SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV2(writer)); - } - } - - /// - public IOpenApiParameter CopyReferenceAsTargetElementWithOverrides(IOpenApiParameter source) + public override IOpenApiParameter CopyReferenceAsTargetElementWithOverrides(IOpenApiParameter source) { return source is OpenApiParameter ? new OpenApiParameter(this) : source; } - - /// - private void SerializeInternal(IOpenApiWriter writer, - Action action) - { - Utils.CheckArgumentNull(writer); - action(writer, Target); - } } } diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index f89d60453..3e8451080 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -1155,38 +1155,42 @@ namespace Microsoft.OpenApi.Models } namespace Microsoft.OpenApi.Models.References { - public class OpenApiCallbackReference : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiCallback + public abstract class BaseOpenApiReferenceHolder : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiSerializable + where T : class, Microsoft.OpenApi.Interfaces.IOpenApiReferenceable, V + where V : Microsoft.OpenApi.Interfaces.IOpenApiSerializable { - public OpenApiCallbackReference(Microsoft.OpenApi.Models.References.OpenApiCallbackReference callback) { } - public OpenApiCallbackReference(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument hostDocument, string externalResource = null) { } - public System.Collections.Generic.IDictionary Extensions { get; } - public System.Collections.Generic.Dictionary PathItems { get; } + protected BaseOpenApiReferenceHolder(Microsoft.OpenApi.Models.References.BaseOpenApiReferenceHolder source) { } + protected BaseOpenApiReferenceHolder(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument hostDocument, Microsoft.OpenApi.Models.ReferenceType referenceType, string externalResource = null) { } public Microsoft.OpenApi.Models.OpenApiReference Reference { get; set; } - public Microsoft.OpenApi.Models.OpenApiCallback Target { get; } + public T Target { get; } public bool UnresolvedReference { get; set; } - public Microsoft.OpenApi.Models.Interfaces.IOpenApiCallback CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiCallback source) { } - public void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } + public abstract V CopyReferenceAsTargetElementWithOverrides(V source); + public virtual void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } public void SerializeAsV3(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } public void SerializeAsV31(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } } - public class OpenApiExampleReference : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiExample, Microsoft.OpenApi.Models.Interfaces.IOpenApiSummarizedElement + public class OpenApiCallbackReference : Microsoft.OpenApi.Models.References.BaseOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiCallback + { + public OpenApiCallbackReference(Microsoft.OpenApi.Models.References.OpenApiCallbackReference callback) { } + public OpenApiCallbackReference(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument hostDocument, string externalResource = null) { } + public System.Collections.Generic.IDictionary Extensions { get; } + public System.Collections.Generic.Dictionary PathItems { get; } + public override Microsoft.OpenApi.Models.Interfaces.IOpenApiCallback CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiCallback source) { } + public override void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } + } + public class OpenApiExampleReference : Microsoft.OpenApi.Models.References.BaseOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiExample, Microsoft.OpenApi.Models.Interfaces.IOpenApiSummarizedElement { public OpenApiExampleReference(Microsoft.OpenApi.Models.References.OpenApiExampleReference example) { } public OpenApiExampleReference(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument hostDocument, string externalResource = null) { } public string Description { get; set; } public System.Collections.Generic.IDictionary Extensions { get; } public string ExternalValue { get; } - public Microsoft.OpenApi.Models.OpenApiReference Reference { get; set; } public string Summary { get; set; } - public Microsoft.OpenApi.Models.OpenApiExample Target { get; } - public bool UnresolvedReference { get; set; } public System.Text.Json.Nodes.JsonNode Value { get; } - public Microsoft.OpenApi.Models.Interfaces.IOpenApiExample CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiExample source) { } - public void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public void SerializeAsV3(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public void SerializeAsV31(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } + public override Microsoft.OpenApi.Models.Interfaces.IOpenApiExample CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiExample source) { } + public override void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } } - public class OpenApiHeaderReference : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiHeader + public class OpenApiHeaderReference : Microsoft.OpenApi.Models.References.BaseOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiHeader { public OpenApiHeaderReference(Microsoft.OpenApi.Models.References.OpenApiHeaderReference header) { } public OpenApiHeaderReference(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument hostDocument, string externalResource = null) { } @@ -1199,18 +1203,12 @@ namespace Microsoft.OpenApi.Models.References public System.Collections.Generic.IDictionary Examples { get; } public bool Explode { get; } public System.Collections.Generic.IDictionary Extensions { get; } - public Microsoft.OpenApi.Models.OpenApiReference Reference { get; set; } public bool Required { get; } public Microsoft.OpenApi.Models.OpenApiSchema Schema { get; } public Microsoft.OpenApi.Models.ParameterStyle? Style { get; } - public Microsoft.OpenApi.Models.OpenApiHeader Target { get; } - public bool UnresolvedReference { get; set; } - public Microsoft.OpenApi.Models.Interfaces.IOpenApiHeader CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiHeader source) { } - public void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public void SerializeAsV3(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public void SerializeAsV31(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } + public override Microsoft.OpenApi.Models.Interfaces.IOpenApiHeader CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiHeader source) { } } - public class OpenApiLinkReference : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiLink + public class OpenApiLinkReference : Microsoft.OpenApi.Models.References.BaseOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiLink { public OpenApiLinkReference(Microsoft.OpenApi.Models.References.OpenApiLinkReference reference) { } public OpenApiLinkReference(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument hostDocument, string externalResource = null) { } @@ -1219,17 +1217,12 @@ namespace Microsoft.OpenApi.Models.References public string OperationId { get; } public string OperationRef { get; } public System.Collections.Generic.IDictionary Parameters { get; } - public Microsoft.OpenApi.Models.OpenApiReference Reference { get; set; } public Microsoft.OpenApi.Models.RuntimeExpressionAnyWrapper RequestBody { get; } public Microsoft.OpenApi.Models.OpenApiServer Server { get; } - public Microsoft.OpenApi.Models.OpenApiLink Target { get; } - public bool UnresolvedReference { get; set; } - public Microsoft.OpenApi.Models.Interfaces.IOpenApiLink CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiLink source) { } - public void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public void SerializeAsV3(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public void SerializeAsV31(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } + public override Microsoft.OpenApi.Models.Interfaces.IOpenApiLink CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiLink source) { } + public override void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } } - public class OpenApiParameterReference : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiParameter + public class OpenApiParameterReference : Microsoft.OpenApi.Models.References.BaseOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiParameter { public OpenApiParameterReference(Microsoft.OpenApi.Models.References.OpenApiParameterReference parameter) { } public OpenApiParameterReference(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument hostDocument, string externalResource = null) { } @@ -1244,16 +1237,10 @@ namespace Microsoft.OpenApi.Models.References public System.Collections.Generic.IDictionary Extensions { get; } public Microsoft.OpenApi.Models.ParameterLocation? In { get; } public string Name { get; } - public Microsoft.OpenApi.Models.OpenApiReference Reference { get; set; } public bool Required { get; } public Microsoft.OpenApi.Models.OpenApiSchema Schema { get; } public Microsoft.OpenApi.Models.ParameterStyle? Style { get; } - public Microsoft.OpenApi.Models.OpenApiParameter Target { get; } - public bool UnresolvedReference { get; set; } - public Microsoft.OpenApi.Models.Interfaces.IOpenApiParameter CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiParameter source) { } - public void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public void SerializeAsV3(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public void SerializeAsV31(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } + public override Microsoft.OpenApi.Models.Interfaces.IOpenApiParameter CopyReferenceAsTargetElementWithOverrides(Microsoft.OpenApi.Models.Interfaces.IOpenApiParameter source) { } } public class OpenApiPathItemReference : Microsoft.OpenApi.Models.OpenApiPathItem, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiReferenceHolder, Microsoft.OpenApi.Interfaces.IOpenApiSerializable {