Skip to content

Commit af42af2

Browse files
committed
Avoid virtual calls in constructors
1 parent 38bd152 commit af42af2

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Text.Json.Nodes;
7-
using Microsoft.OpenApi.Any;
87
using Microsoft.OpenApi.Extensions;
98
using Microsoft.OpenApi.Helpers;
109
using Microsoft.OpenApi.Interfaces;
@@ -114,7 +113,7 @@ public OpenApiHeader(OpenApiHeader header)
114113
Style = header?.Style ?? Style;
115114
Explode = header?.Explode ?? Explode;
116115
AllowReserved = header?.AllowReserved ?? AllowReserved;
117-
Schema = header?.Schema != null ? new(header.Schema) : null;
116+
_schema = header?.Schema != null ? new(header.Schema) : null;
118117
Example = header?.Example != null ? JsonNodeCloneHelper.Clone(header.Example) : null;
119118
Examples = header?.Examples != null ? new Dictionary<string, OpenApiExample>(header.Examples) : null;
120119
Content = header?.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Text.Json.Nodes;
7-
using Microsoft.OpenApi.Any;
87
using Microsoft.OpenApi.Helpers;
98
using Microsoft.OpenApi.Interfaces;
109
using Microsoft.OpenApi.Writers;
@@ -62,7 +61,7 @@ public OpenApiMediaType() { }
6261
/// </summary>
6362
public OpenApiMediaType(OpenApiMediaType mediaType)
6463
{
65-
Schema = mediaType?.Schema != null ? new(mediaType.Schema) : null;
64+
_schema = mediaType?.Schema != null ? new(mediaType.Schema) : null;
6665
Example = mediaType?.Example != null ? JsonNodeCloneHelper.Clone(mediaType.Example) : null;
6766
Examples = mediaType?.Examples != null ? new Dictionary<string, OpenApiExample>(mediaType.Examples) : null;
6867
Encoding = mediaType?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(mediaType.Encoding) : null;

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public OpenApiParameter(OpenApiParameter parameter)
168168
Style = parameter?.Style ?? Style;
169169
Explode = parameter?.Explode ?? Explode;
170170
AllowReserved = parameter?.AllowReserved ?? AllowReserved;
171-
Schema = parameter?.Schema != null ? new(parameter.Schema) : null;
171+
_schema = parameter?.Schema != null ? new(parameter.Schema) : null;
172172
Examples = parameter?.Examples != null ? new Dictionary<string, OpenApiExample>(parameter.Examples) : null;
173173
Example = parameter?.Example != null ? JsonNodeCloneHelper.Clone(parameter.Example) : null;
174174
Content = parameter?.Content != null ? new Dictionary<string, OpenApiMediaType>(parameter.Content) : null;

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Text.Json.Nodes;
8-
using Microsoft.OpenApi.Any;
98
using Microsoft.OpenApi.Helpers;
109
using Microsoft.OpenApi.Interfaces;
1110
using Microsoft.OpenApi.Writers;
@@ -17,6 +16,10 @@ namespace Microsoft.OpenApi.Models
1716
/// </summary>
1817
public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApiSerializable
1918
{
19+
private JsonNode _example;
20+
private JsonNode _default;
21+
private IList<JsonNode> _examples;
22+
2023
/// <summary>
2124
/// Follow JSON Schema definition. Short text providing information about the data.
2225
/// </summary>
@@ -149,7 +152,11 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApi
149152
/// Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level.
150153
/// For example, if type is string, then default can be "foo" but cannot be 1.
151154
/// </summary>
152-
public virtual JsonNode Default { get; set; }
155+
public virtual JsonNode Default
156+
{
157+
get => _default;
158+
set => _default = value;
159+
}
153160

154161
/// <summary>
155162
/// Relevant only for Schema "properties" definitions. Declares the property as "read only".
@@ -270,14 +277,22 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApi
270277
/// To represent examples that cannot be naturally represented in JSON or YAML,
271278
/// a string value can be used to contain the example with escaping where necessary.
272279
/// </summary>
273-
public virtual JsonNode Example { get; set; }
280+
public virtual JsonNode Example
281+
{
282+
get => _example;
283+
set => _example = value;
284+
}
274285

275286
/// <summary>
276287
/// A free-form property to include examples of an instance for this schema.
277288
/// To represent examples that cannot be naturally represented in JSON or YAML,
278289
/// a list of values can be used to contain the examples with escaping where necessary.
279290
/// </summary>
280-
public virtual IList<JsonNode> Examples { get; set; }
291+
public virtual IList<JsonNode> Examples
292+
{
293+
get => _examples;
294+
set => _examples = value;
295+
}
281296

282297
/// <summary>
283298
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
@@ -360,7 +375,7 @@ public OpenApiSchema(OpenApiSchema schema)
360375
MinLength = schema?.MinLength ?? MinLength;
361376
Pattern = schema?.Pattern ?? Pattern;
362377
MultipleOf = schema?.MultipleOf ?? MultipleOf;
363-
Default = schema?.Default != null ? JsonNodeCloneHelper.Clone(schema?.Default) : null;
378+
_default = schema?.Default != null ? JsonNodeCloneHelper.Clone(schema?.Default) : null;
364379
ReadOnly = schema?.ReadOnly ?? ReadOnly;
365380
WriteOnly = schema?.WriteOnly ?? WriteOnly;
366381
AllOf = schema?.AllOf != null ? new List<OpenApiSchema>(schema.AllOf) : null;
@@ -379,8 +394,8 @@ public OpenApiSchema(OpenApiSchema schema)
379394
AdditionalPropertiesAllowed = schema?.AdditionalPropertiesAllowed ?? AdditionalPropertiesAllowed;
380395
AdditionalProperties = schema?.AdditionalProperties != null ? new(schema?.AdditionalProperties) : null;
381396
Discriminator = schema?.Discriminator != null ? new(schema?.Discriminator) : null;
382-
Example = schema?.Example != null ? JsonNodeCloneHelper.Clone(schema?.Example) : null;
383-
Examples = schema?.Examples != null ? new List<JsonNode>(schema.Examples) : null;
397+
_example = schema?.Example != null ? JsonNodeCloneHelper.Clone(schema?.Example) : null;
398+
_examples = schema?.Examples != null ? new List<JsonNode>(schema.Examples) : null;
384399
Enum = schema?.Enum != null ? new List<JsonNode>(schema.Enum) : null;
385400
Nullable = schema?.Nullable ?? Nullable;
386401
ExternalDocs = schema?.ExternalDocs != null ? new(schema?.ExternalDocs) : null;
@@ -606,7 +621,7 @@ internal void WriteV31Properties(IOpenApiWriter writer)
606621
writer.WriteProperty(OpenApiConstants.V31ExclusiveMaximum, V31ExclusiveMaximum);
607622
writer.WriteProperty(OpenApiConstants.V31ExclusiveMinimum, V31ExclusiveMinimum);
608623
writer.WriteProperty(OpenApiConstants.UnevaluatedProperties, UnevaluatedProperties, false);
609-
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (nodeWriter, s) => nodeWriter.WriteAny(s));
624+
writer.WriteOptionalCollection(OpenApiConstants.Examples, _examples, (nodeWriter, s) => nodeWriter.WriteAny(s));
610625
writer.WriteOptionalMap(OpenApiConstants.PatternProperties, PatternProperties, (w, s) => s.SerializeAsV31(w));
611626
}
612627

0 commit comments

Comments
 (0)