Skip to content

Commit

Permalink
fix: tag, response, and security scheme shallow copy
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Jan 30, 2025
1 parent 9af6f30 commit 7ac149c
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.OpenApi.Models.Interfaces;
/// Defines the base properties for the response object.
/// This interface is provided for type assertions but should not be implemented by package consumers beyond automatic mocking.
/// </summary>
public interface IOpenApiResponse : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible
public interface IOpenApiResponse : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible, IShallowCopyable<IOpenApiResponse>
{
/// <summary>
/// Maps a header name to its definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.OpenApi.Models.Interfaces;
/// Defines the base properties for the security scheme object.
/// This interface is provided for type assertions but should not be implemented by package consumers beyond automatic mocking.
/// </summary>
public interface IOpenApiSecurityScheme : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible
public interface IOpenApiSecurityScheme : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible, IShallowCopyable<IOpenApiSecurityScheme>
{
/// <summary>
/// REQUIRED. The type of the security scheme. Valid values are "apiKey", "http", "oauth2", "openIdConnect".
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/Interfaces/IOpenApiTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.OpenApi.Models.Interfaces;
/// Defines the base properties for the path item object.
/// This interface is provided for type assertions but should not be implemented by package consumers beyond automatic mocking.
/// </summary>
public interface IOpenApiTag : IOpenApiSerializable, IOpenApiReadOnlyExtensible, IOpenApiReadOnlyDescribedElement
public interface IOpenApiTag : IOpenApiSerializable, IOpenApiReadOnlyExtensible, IOpenApiReadOnlyDescribedElement, IShallowCopyable<IOpenApiTag>
{
/// <summary>
/// The name of the tag.
Expand Down
18 changes: 12 additions & 6 deletions src/Microsoft.OpenApi/Models/OpenApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public OpenApiResponse() { }
/// <summary>
/// Initializes a copy of <see cref="IOpenApiResponse"/> object
/// </summary>
public OpenApiResponse(IOpenApiResponse response)
internal OpenApiResponse(IOpenApiResponse response)
{
Utils.CheckArgumentNull(response);
Description = response?.Description ?? Description;
Headers = response?.Headers != null ? new Dictionary<string, IOpenApiHeader>(response.Headers) : null;
Content = response?.Content != null ? new Dictionary<string, OpenApiMediaType>(response.Content) : null;
Links = response?.Links != null ? new Dictionary<string, IOpenApiLink>(response.Links) : null;
Extensions = response?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(response.Extensions) : null;
Description = response.Description ?? Description;
Headers = response.Headers != null ? new Dictionary<string, IOpenApiHeader>(response.Headers) : null;
Content = response.Content != null ? new Dictionary<string, OpenApiMediaType>(response.Content) : null;
Links = response.Links != null ? new Dictionary<string, IOpenApiLink>(response.Links) : null;
Extensions = response.Extensions != null ? new Dictionary<string, IOpenApiExtension>(response.Extensions) : null;
}

/// <summary>
Expand Down Expand Up @@ -164,5 +164,11 @@ public void SerializeAsV2(IOpenApiWriter writer)

writer.WriteEndObject();
}

/// <inheritdoc/>
public IOpenApiResponse CreateShallowCopy()
{
return new OpenApiResponse(this);
}
}
}
8 changes: 7 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public OpenApiSecurityScheme() { }
/// <summary>
/// Initializes a copy of <see cref="IOpenApiSecurityScheme"/> object
/// </summary>
public OpenApiSecurityScheme(IOpenApiSecurityScheme securityScheme)
internal OpenApiSecurityScheme(IOpenApiSecurityScheme securityScheme)
{
Utils.CheckArgumentNull(securityScheme);
Type = securityScheme?.Type;
Expand Down Expand Up @@ -229,5 +229,11 @@ private static void WriteOAuthFlowForV2(IOpenApiWriter writer, string flowValue,
// scopes
writer.WriteOptionalMap(OpenApiConstants.Scopes, flow.Scopes, (w, s) => w.WriteValue(s));
}

/// <inheritdoc/>
public IOpenApiSecurityScheme CreateShallowCopy()
{
return new OpenApiSecurityScheme(this);
}
}
}
17 changes: 12 additions & 5 deletions src/Microsoft.OpenApi/Models/OpenApiTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public OpenApiTag() { }
/// <summary>
/// Initializes a copy of an <see cref="IOpenApiTag"/> object
/// </summary>
public OpenApiTag(IOpenApiTag tag)
internal OpenApiTag(IOpenApiTag tag)
{
Name = tag?.Name ?? Name;
Description = tag?.Description ?? Description;
ExternalDocs = tag?.ExternalDocs != null ? new(tag.ExternalDocs) : null;
Extensions = tag?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(tag.Extensions) : null;
Utils.CheckArgumentNull(tag);
Name = tag.Name ?? Name;
Description = tag.Description ?? Description;
ExternalDocs = tag.ExternalDocs != null ? new(tag.ExternalDocs) : null;
Extensions = tag.Extensions != null ? new Dictionary<string, IOpenApiExtension>(tag.Extensions) : null;
}

/// <summary>
Expand Down Expand Up @@ -101,5 +102,11 @@ public void SerializeAsV2(IOpenApiWriter writer)

writer.WriteEndObject();
}

/// <inheritdoc/>
public IOpenApiTag CreateShallowCopy()
{
return new OpenApiTag(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public class OpenApiResponseReference : BaseOpenApiReferenceHolder<OpenApiRespon
/// </param>
public OpenApiResponseReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null):base(referenceId, hostDocument, ReferenceType.Response, externalResource)
{
}
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="openApiResponseReference">The reference to copy</param>
private OpenApiResponseReference(OpenApiResponseReference openApiResponseReference):base(openApiResponseReference)
{

}

internal OpenApiResponseReference(OpenApiResponse target, string referenceId):base(target, referenceId, ReferenceType.Response)
Expand Down Expand Up @@ -61,5 +69,11 @@ public override IOpenApiResponse CopyReferenceAsTargetElementWithOverrides(IOpen
{
return source is OpenApiResponse ? new OpenApiResponse(this) : source;
}

/// <inheritdoc/>
public IOpenApiResponse CreateShallowCopy()
{
return new OpenApiResponseReference(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public class OpenApiSecuritySchemeReference : BaseOpenApiReferenceHolder<OpenApi
/// <param name="externalResource">The externally referenced file.</param>
public OpenApiSecuritySchemeReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null):base(referenceId, hostDocument, ReferenceType.SecurityScheme, externalResource)
{
}
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="openApiSecuritySchemeReference">The reference to copy</param>
private OpenApiSecuritySchemeReference(OpenApiSecuritySchemeReference openApiSecuritySchemeReference):base(openApiSecuritySchemeReference)
{

}
internal OpenApiSecuritySchemeReference(OpenApiSecurityScheme target, string referenceId):base(target, referenceId, ReferenceType.SecurityScheme)
{
Expand Down Expand Up @@ -68,5 +76,11 @@ public override IOpenApiSecurityScheme CopyReferenceAsTargetElementWithOverrides
{
return source is OpenApiSecurityScheme ? new OpenApiSecurityScheme(this) : source;
}

/// <inheritdoc/>
public IOpenApiSecurityScheme CreateShallowCopy()
{
return new OpenApiSecuritySchemeReference(this);
}
}
}
14 changes: 14 additions & 0 deletions src/Microsoft.OpenApi/Models/References/OpenApiTagReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public override OpenApiTag Target
/// <param name="hostDocument">The host OpenAPI document.</param>
public OpenApiTagReference(string referenceId, OpenApiDocument hostDocument):base(referenceId, hostDocument, ReferenceType.Tag)
{
}
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="openApiTagReference">The reference to copy</param>
private OpenApiTagReference(OpenApiTagReference openApiTagReference):base(openApiTagReference)
{

}

internal OpenApiTagReference(OpenApiTag target, string referenceId):base(target, referenceId, ReferenceType.Tag)
Expand All @@ -58,5 +66,11 @@ public override IOpenApiTag CopyReferenceAsTargetElementWithOverrides(IOpenApiTa
{
return source is OpenApiTag ? new OpenApiTag(this) : source;
}

/// <inheritdoc/>
public IOpenApiTag CreateShallowCopy()
{
return new OpenApiTagReference(this);
}
}
}
Loading

0 comments on commit 7ac149c

Please sign in to comment.