Skip to content

Commit 7ac149c

Browse files
committed
fix: tag, response, and security scheme shallow copy
Signed-off-by: Vincent Biret <[email protected]>
1 parent 9af6f30 commit 7ac149c

File tree

10 files changed

+91
-27
lines changed

10 files changed

+91
-27
lines changed

src/Microsoft.OpenApi/Models/Interfaces/IOpenApiResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.OpenApi.Models.Interfaces;
77
/// Defines the base properties for the response object.
88
/// This interface is provided for type assertions but should not be implemented by package consumers beyond automatic mocking.
99
/// </summary>
10-
public interface IOpenApiResponse : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible
10+
public interface IOpenApiResponse : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible, IShallowCopyable<IOpenApiResponse>
1111
{
1212
/// <summary>
1313
/// Maps a header name to its definition.

src/Microsoft.OpenApi/Models/Interfaces/IOpenApiSecurityScheme.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.OpenApi.Models.Interfaces;
88
/// Defines the base properties for the security scheme object.
99
/// This interface is provided for type assertions but should not be implemented by package consumers beyond automatic mocking.
1010
/// </summary>
11-
public interface IOpenApiSecurityScheme : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible
11+
public interface IOpenApiSecurityScheme : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible, IShallowCopyable<IOpenApiSecurityScheme>
1212
{
1313
/// <summary>
1414
/// REQUIRED. The type of the security scheme. Valid values are "apiKey", "http", "oauth2", "openIdConnect".

src/Microsoft.OpenApi/Models/Interfaces/IOpenApiTag.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Microsoft.OpenApi.Models.Interfaces;
66
/// Defines the base properties for the path item object.
77
/// This interface is provided for type assertions but should not be implemented by package consumers beyond automatic mocking.
88
/// </summary>
9-
public interface IOpenApiTag : IOpenApiSerializable, IOpenApiReadOnlyExtensible, IOpenApiReadOnlyDescribedElement
9+
public interface IOpenApiTag : IOpenApiSerializable, IOpenApiReadOnlyExtensible, IOpenApiReadOnlyDescribedElement, IShallowCopyable<IOpenApiTag>
1010
{
1111
/// <summary>
1212
/// The name of the tag.

src/Microsoft.OpenApi/Models/OpenApiResponse.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public OpenApiResponse() { }
3838
/// <summary>
3939
/// Initializes a copy of <see cref="IOpenApiResponse"/> object
4040
/// </summary>
41-
public OpenApiResponse(IOpenApiResponse response)
41+
internal OpenApiResponse(IOpenApiResponse response)
4242
{
4343
Utils.CheckArgumentNull(response);
44-
Description = response?.Description ?? Description;
45-
Headers = response?.Headers != null ? new Dictionary<string, IOpenApiHeader>(response.Headers) : null;
46-
Content = response?.Content != null ? new Dictionary<string, OpenApiMediaType>(response.Content) : null;
47-
Links = response?.Links != null ? new Dictionary<string, IOpenApiLink>(response.Links) : null;
48-
Extensions = response?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(response.Extensions) : null;
44+
Description = response.Description ?? Description;
45+
Headers = response.Headers != null ? new Dictionary<string, IOpenApiHeader>(response.Headers) : null;
46+
Content = response.Content != null ? new Dictionary<string, OpenApiMediaType>(response.Content) : null;
47+
Links = response.Links != null ? new Dictionary<string, IOpenApiLink>(response.Links) : null;
48+
Extensions = response.Extensions != null ? new Dictionary<string, IOpenApiExtension>(response.Extensions) : null;
4949
}
5050

5151
/// <summary>
@@ -164,5 +164,11 @@ public void SerializeAsV2(IOpenApiWriter writer)
164164

165165
writer.WriteEndObject();
166166
}
167+
168+
/// <inheritdoc/>
169+
public IOpenApiResponse CreateShallowCopy()
170+
{
171+
return new OpenApiResponse(this);
172+
}
167173
}
168174
}

src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public OpenApiSecurityScheme() { }
5050
/// <summary>
5151
/// Initializes a copy of <see cref="IOpenApiSecurityScheme"/> object
5252
/// </summary>
53-
public OpenApiSecurityScheme(IOpenApiSecurityScheme securityScheme)
53+
internal OpenApiSecurityScheme(IOpenApiSecurityScheme securityScheme)
5454
{
5555
Utils.CheckArgumentNull(securityScheme);
5656
Type = securityScheme?.Type;
@@ -229,5 +229,11 @@ private static void WriteOAuthFlowForV2(IOpenApiWriter writer, string flowValue,
229229
// scopes
230230
writer.WriteOptionalMap(OpenApiConstants.Scopes, flow.Scopes, (w, s) => w.WriteValue(s));
231231
}
232+
233+
/// <inheritdoc/>
234+
public IOpenApiSecurityScheme CreateShallowCopy()
235+
{
236+
return new OpenApiSecurityScheme(this);
237+
}
232238
}
233239
}

src/Microsoft.OpenApi/Models/OpenApiTag.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ public OpenApiTag() { }
3434
/// <summary>
3535
/// Initializes a copy of an <see cref="IOpenApiTag"/> object
3636
/// </summary>
37-
public OpenApiTag(IOpenApiTag tag)
37+
internal OpenApiTag(IOpenApiTag tag)
3838
{
39-
Name = tag?.Name ?? Name;
40-
Description = tag?.Description ?? Description;
41-
ExternalDocs = tag?.ExternalDocs != null ? new(tag.ExternalDocs) : null;
42-
Extensions = tag?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(tag.Extensions) : null;
39+
Utils.CheckArgumentNull(tag);
40+
Name = tag.Name ?? Name;
41+
Description = tag.Description ?? Description;
42+
ExternalDocs = tag.ExternalDocs != null ? new(tag.ExternalDocs) : null;
43+
Extensions = tag.Extensions != null ? new Dictionary<string, IOpenApiExtension>(tag.Extensions) : null;
4344
}
4445

4546
/// <summary>
@@ -101,5 +102,11 @@ public void SerializeAsV2(IOpenApiWriter writer)
101102

102103
writer.WriteEndObject();
103104
}
105+
106+
/// <inheritdoc/>
107+
public IOpenApiTag CreateShallowCopy()
108+
{
109+
return new OpenApiTag(this);
110+
}
104111
}
105112
}

src/Microsoft.OpenApi/Models/References/OpenApiResponseReference.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public class OpenApiResponseReference : BaseOpenApiReferenceHolder<OpenApiRespon
2525
/// </param>
2626
public OpenApiResponseReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null):base(referenceId, hostDocument, ReferenceType.Response, externalResource)
2727
{
28+
}
29+
/// <summary>
30+
/// Copy constructor
31+
/// </summary>
32+
/// <param name="openApiResponseReference">The reference to copy</param>
33+
private OpenApiResponseReference(OpenApiResponseReference openApiResponseReference):base(openApiResponseReference)
34+
{
35+
2836
}
2937

3038
internal OpenApiResponseReference(OpenApiResponse target, string referenceId):base(target, referenceId, ReferenceType.Response)
@@ -61,5 +69,11 @@ public override IOpenApiResponse CopyReferenceAsTargetElementWithOverrides(IOpen
6169
{
6270
return source is OpenApiResponse ? new OpenApiResponse(this) : source;
6371
}
72+
73+
/// <inheritdoc/>
74+
public IOpenApiResponse CreateShallowCopy()
75+
{
76+
return new OpenApiResponseReference(this);
77+
}
6478
}
6579
}

src/Microsoft.OpenApi/Models/References/OpenApiSecuritySchemeReference.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public class OpenApiSecuritySchemeReference : BaseOpenApiReferenceHolder<OpenApi
2121
/// <param name="externalResource">The externally referenced file.</param>
2222
public OpenApiSecuritySchemeReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null):base(referenceId, hostDocument, ReferenceType.SecurityScheme, externalResource)
2323
{
24+
}
25+
/// <summary>
26+
/// Copy constructor
27+
/// </summary>
28+
/// <param name="openApiSecuritySchemeReference">The reference to copy</param>
29+
private OpenApiSecuritySchemeReference(OpenApiSecuritySchemeReference openApiSecuritySchemeReference):base(openApiSecuritySchemeReference)
30+
{
31+
2432
}
2533
internal OpenApiSecuritySchemeReference(OpenApiSecurityScheme target, string referenceId):base(target, referenceId, ReferenceType.SecurityScheme)
2634
{
@@ -68,5 +76,11 @@ public override IOpenApiSecurityScheme CopyReferenceAsTargetElementWithOverrides
6876
{
6977
return source is OpenApiSecurityScheme ? new OpenApiSecurityScheme(this) : source;
7078
}
79+
80+
/// <inheritdoc/>
81+
public IOpenApiSecurityScheme CreateShallowCopy()
82+
{
83+
return new OpenApiSecuritySchemeReference(this);
84+
}
7185
}
7286
}

src/Microsoft.OpenApi/Models/References/OpenApiTagReference.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public override OpenApiTag Target
3333
/// <param name="hostDocument">The host OpenAPI document.</param>
3434
public OpenApiTagReference(string referenceId, OpenApiDocument hostDocument):base(referenceId, hostDocument, ReferenceType.Tag)
3535
{
36+
}
37+
/// <summary>
38+
/// Copy constructor
39+
/// </summary>
40+
/// <param name="openApiTagReference">The reference to copy</param>
41+
private OpenApiTagReference(OpenApiTagReference openApiTagReference):base(openApiTagReference)
42+
{
43+
3644
}
3745

3846
internal OpenApiTagReference(OpenApiTag target, string referenceId):base(target, referenceId, ReferenceType.Tag)
@@ -58,5 +66,11 @@ public override IOpenApiTag CopyReferenceAsTargetElementWithOverrides(IOpenApiTa
5866
{
5967
return source is OpenApiTag ? new OpenApiTag(this) : source;
6068
}
69+
70+
/// <inheritdoc/>
71+
public IOpenApiTag CreateShallowCopy()
72+
{
73+
return new OpenApiTagReference(this);
74+
}
6175
}
6276
}

0 commit comments

Comments
 (0)