Skip to content

Commit 8389913

Browse files
committed
Assign to a backing property in copy constructor instead of the virtual property
1 parent bf51aa8 commit 8389913

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace Microsoft.OpenApi.Models
1919
/// </summary>
2020
public class OpenApiHeader : IOpenApiSerializable, IOpenApiReferenceable, IOpenApiExtensible, IEffective<OpenApiHeader>
2121
{
22+
private JsonSchema _schema;
23+
2224
/// <summary>
2325
/// Indicates if object is populated with data or is just a reference to the data
2426
/// </summary>
@@ -66,9 +68,13 @@ public class OpenApiHeader : IOpenApiSerializable, IOpenApiReferenceable, IOpenA
6668
public virtual bool AllowReserved { get; set; }
6769

6870
/// <summary>
69-
/// The schema defining the type used for the header.
71+
/// The schema defining the type used for the request body.
7072
/// </summary>
71-
public virtual JsonSchema Schema { get; set; }
73+
public virtual JsonSchema Schema
74+
{
75+
get => _schema;
76+
set => _schema = value;
77+
}
7278

7379
/// <summary>
7480
/// Example of the media type.
@@ -109,7 +115,7 @@ public OpenApiHeader(OpenApiHeader header)
109115
Style = header?.Style ?? Style;
110116
Explode = header?.Explode ?? Explode;
111117
AllowReserved = header?.AllowReserved ?? AllowReserved;
112-
Schema = InitializeSchema(header?.Schema);
118+
_schema = JsonNodeCloneHelper.CloneJsonSchema(header?.Schema);
113119
Example = JsonNodeCloneHelper.Clone(header?.Example);
114120
Examples = header?.Examples != null ? new Dictionary<string, OpenApiExample>(header.Examples) : null;
115121
Content = header?.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
@@ -299,14 +305,5 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
299305

300306
writer.WriteEndObject();
301307
}
302-
303-
/// <summary>
304-
/// Clone a JSON schema instance
305-
/// </summary>
306-
/// <returns></returns>
307-
protected JsonSchema InitializeSchema(JsonSchema schema)
308-
{
309-
return JsonNodeCloneHelper.CloneJsonSchema(Schema);
310-
}
311308
}
312309
}

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ namespace Microsoft.OpenApi.Models
1717
/// </summary>
1818
public class OpenApiMediaType : IOpenApiSerializable, IOpenApiExtensible
1919
{
20+
private JsonSchema _schema;
21+
2022
/// <summary>
2123
/// The schema defining the type used for the request body.
2224
/// </summary>
23-
public virtual JsonSchema Schema { get; set; }
25+
public virtual JsonSchema Schema
26+
{
27+
get => _schema;
28+
set => _schema = value;
29+
}
2430

2531
/// <summary>
2632
/// Example of the media type.
@@ -57,7 +63,7 @@ public OpenApiMediaType() { }
5763
/// </summary>
5864
public OpenApiMediaType(OpenApiMediaType mediaType)
5965
{
60-
Schema = InitializeSchema(mediaType?.Schema);
66+
_schema = JsonNodeCloneHelper.CloneJsonSchema(mediaType?.Schema);
6167
Example = JsonNodeCloneHelper.Clone(mediaType?.Example);
6268
Examples = mediaType?.Examples != null ? new Dictionary<string, OpenApiExample>(mediaType.Examples) : null;
6369
Encoding = mediaType?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(mediaType.Encoding) : null;
@@ -115,14 +121,5 @@ public void SerializeAsV2(IOpenApiWriter writer)
115121
{
116122
// Media type does not exist in V2.
117123
}
118-
119-
/// <summary>
120-
/// Clones a JSON schema instance
121-
/// </summary>
122-
/// <returns></returns>
123-
protected JsonSchema InitializeSchema(JsonSchema schema)
124-
{
125-
return JsonNodeCloneHelper.CloneJsonSchema(Schema);
126-
}
127124
}
128125
}

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class OpenApiParameter : IOpenApiSerializable, IOpenApiReferenceable, IEf
2020
{
2121
private bool? _explode;
2222
private ParameterStyle? _style;
23+
private JsonSchema _schema;
2324

2425
/// <summary>
2526
/// Indicates if object is populated with data or is just a reference to the data
@@ -107,7 +108,11 @@ public virtual bool Explode
107108
/// <summary>
108109
/// The schema defining the type used for the request body.
109110
/// </summary>
110-
public virtual JsonSchema Schema { get; set; }
111+
public virtual JsonSchema Schema
112+
{
113+
get => _schema;
114+
set => _schema = value;
115+
}
111116

112117
/// <summary>
113118
/// Examples of the media type. Each example SHOULD contain a value
@@ -163,7 +168,7 @@ public OpenApiParameter(OpenApiParameter parameter)
163168
Style = parameter?.Style ?? Style;
164169
Explode = parameter?.Explode ?? Explode;
165170
AllowReserved = parameter?.AllowReserved ?? AllowReserved;
166-
Schema = InitializeSchema(parameter?.Schema);
171+
_schema = JsonNodeCloneHelper.CloneJsonSchema(parameter?.Schema);
167172
Examples = parameter?.Examples != null ? new Dictionary<string, OpenApiExample>(parameter.Examples) : null;
168173
Example = JsonNodeCloneHelper.Clone(parameter?.Example);
169174
Content = parameter?.Content != null ? new Dictionary<string, OpenApiMediaType>(parameter.Content) : null;
@@ -447,15 +452,6 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
447452

448453
return Style;
449454
}
450-
451-
/// <summary>
452-
/// Clones an instance of a JSON schema
453-
/// </summary>
454-
/// <returns></returns>
455-
protected JsonSchema InitializeSchema(JsonSchema schema)
456-
{
457-
return JsonNodeCloneHelper.CloneJsonSchema(schema);
458-
}
459455
}
460456

461457
/// <summary>

0 commit comments

Comments
 (0)