Skip to content

Commit 546086e

Browse files
committed
Update reference types to remove cloning
1 parent 16929e9 commit 546086e

11 files changed

+125
-83
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -70,11 +70,13 @@ internal OpenApiCallbackReference(OpenApiCallback target, string referenceId)
7070
};
7171
}
7272

73+
private Dictionary<RuntimeExpression, OpenApiPathItem> _pathItems;
7374
/// <inheritdoc/>
74-
public override Dictionary<RuntimeExpression, OpenApiPathItem> PathItems { get => Target.PathItems; set => Target.PathItems = value; }
75+
public override Dictionary<RuntimeExpression, OpenApiPathItem> PathItems { get => _pathItems is not null ? _pathItems : Target?.PathItems; set => _pathItems = value; }
7576

77+
private IDictionary<string, IOpenApiExtension> _extensions;
7678
/// <inheritdoc/>
77-
public override IDictionary<string, IOpenApiExtension> Extensions { get => Target.Extensions; set => Target.Extensions = value; }
79+
public override IDictionary<string, IOpenApiExtension> Extensions { get => _extensions is not null ? _extensions : Target?.Extensions; set => _extensions = value; }
7880

7981
/// <inheritdoc/>
8082
public override void SerializeAsV3(IOpenApiWriter writer)

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ public OpenApiExample Target
3030
get
3131
{
3232
_target ??= Reference.HostDocument.ResolveReferenceTo<OpenApiExample>(_reference);
33-
OpenApiExample resolved = new OpenApiExample(_target);
34-
if (!string.IsNullOrEmpty(_description)) resolved.Description = _description;
35-
if (!string.IsNullOrEmpty(_summary)) resolved.Summary = _summary;
36-
return resolved;
33+
if (!string.IsNullOrEmpty(_description)) _target.Description = _description;
34+
if (!string.IsNullOrEmpty(_summary)) _target.Summary = _summary;
35+
return _target;
3736
}
3837
}
3938

@@ -76,25 +75,28 @@ internal OpenApiExampleReference(OpenApiExample target, string referenceId)
7675
/// <inheritdoc/>
7776
public override string Description
7877
{
79-
get => string.IsNullOrEmpty(_description) ? Target.Description : _description;
78+
get => string.IsNullOrEmpty(_description) ? Target?.Description : _description;
8079
set => _description = value;
8180
}
8281

8382
/// <inheritdoc/>
8483
public override string Summary
8584
{
86-
get => string.IsNullOrEmpty(_summary) ? Target.Summary : _summary;
85+
get => string.IsNullOrEmpty(_summary) ? Target?.Summary : _summary;
8786
set => _summary = value;
8887
}
8988

89+
private IDictionary<string, IOpenApiExtension> _extensions;
9090
/// <inheritdoc/>
91-
public override IDictionary<string, IOpenApiExtension> Extensions { get => Target.Extensions; set => Target.Extensions = value; }
91+
public override IDictionary<string, IOpenApiExtension> Extensions { get => _extensions is not null ? _extensions : Target?.Extensions; set => _extensions = value; }
9292

93+
private string _externalValue;
9394
/// <inheritdoc/>
94-
public override string ExternalValue { get => Target.ExternalValue; set => Target.ExternalValue = value; }
95+
public override string ExternalValue { get => !string.IsNullOrEmpty(_externalValue) ? _externalValue : Target?.ExternalValue; set => _externalValue = value; }
9596

97+
private JsonNode _value;
9698
/// <inheritdoc/>
97-
public override JsonNode Value { get => Target.Value; set => Target.Value = value; }
99+
public override JsonNode Value { get => _value is not null ? _value : Target?.Value; set => _value = value; }
98100

99101
/// <inheritdoc/>
100102
public override void SerializeAsV3(IOpenApiWriter writer)

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ public OpenApiHeader Target
2929
get
3030
{
3131
_target ??= Reference.HostDocument.ResolveReferenceTo<OpenApiHeader>(_reference);
32-
OpenApiHeader resolved = new OpenApiHeader(_target);
33-
if (!string.IsNullOrEmpty(_description)) resolved.Description = _description;
34-
return resolved;
32+
if (!string.IsNullOrEmpty(_description)) _target.Description = _description;
33+
return _target;
3534
}
3635
}
3736

@@ -74,42 +73,53 @@ internal OpenApiHeaderReference(OpenApiHeader target, string referenceId)
7473
/// <inheritdoc/>
7574
public override string Description
7675
{
77-
get => string.IsNullOrEmpty(_description) ? Target.Description : _description;
76+
get => string.IsNullOrEmpty(_description) ? Target?.Description : _description;
7877
set => _description = value;
7978
}
8079

80+
private bool? _required;
8181
/// <inheritdoc/>
82-
public override bool Required { get => Target.Required; set => Target.Required = value; }
82+
public override bool Required { get => _required is not null ? _required.Value : Target?.Required ?? false; set => _required = value; }
8383

84+
private bool? _deprecated;
8485
/// <inheritdoc/>
85-
public override bool Deprecated { get => Target.Deprecated; set => Target.Deprecated = value; }
86+
public override bool Deprecated { get => _deprecated is not null ? _deprecated.Value : Target?.Deprecated ?? false; set => _deprecated = value; }
8687

88+
private bool? _allowEmptyValue;
8789
/// <inheritdoc/>
88-
public override bool AllowEmptyValue { get => Target.AllowEmptyValue; set => Target.AllowEmptyValue = value; }
90+
public override bool AllowEmptyValue { get => _allowEmptyValue is not null ? _allowEmptyValue.Value : Target?.AllowEmptyValue ?? false; set => _allowEmptyValue = value; }
8991

92+
private OpenApiSchema _schema;
9093
/// <inheritdoc/>
91-
public override OpenApiSchema Schema { get => Target.Schema; set => Target.Schema = value; }
94+
public override OpenApiSchema Schema { get => _schema is not null ? _schema : Target?.Schema; set => _schema = value; }
9295

96+
private ParameterStyle? _style;
9397
/// <inheritdoc/>
94-
public override ParameterStyle? Style { get => Target.Style; set => Target.Style = value; }
98+
public override ParameterStyle? Style { get => _style is not null ? _style : Target?.Style; set => _style = value; }
9599

100+
private bool? _explode;
96101
/// <inheritdoc/>
97-
public override bool Explode { get => Target.Explode; set => Target.Explode = value; }
102+
public override bool Explode { get => _explode is not null ? _explode.Value : Target?.Explode ?? false; set => _explode = value; }
98103

104+
private bool? _allowReserved;
99105
/// <inheritdoc/>
100-
public override bool AllowReserved { get => Target.AllowReserved; set => Target.AllowReserved = value; }
106+
public override bool AllowReserved { get => _allowReserved is not null ? _allowReserved.Value : Target?.AllowReserved ?? false ; set => _allowReserved = value; }
101107

108+
private JsonNode _example;
102109
/// <inheritdoc/>
103-
public override JsonNode Example { get => Target.Example; set => Target.Example = value; }
110+
public override JsonNode Example { get => _example is not null ? _example : Target?.Example; set => _example = value; }
104111

112+
private IDictionary<string, OpenApiExample> _examples;
105113
/// <inheritdoc/>
106-
public override IDictionary<string, OpenApiExample> Examples { get => Target.Examples; set => Target.Examples = value; }
114+
public override IDictionary<string, OpenApiExample> Examples { get => _examples is not null ? _examples : Target?.Examples; set => _examples = value; }
107115

116+
private IDictionary<string, OpenApiMediaType> _content;
108117
/// <inheritdoc/>
109-
public override IDictionary<string, OpenApiMediaType> Content { get => Target.Content; set => Target.Content = value; }
118+
public override IDictionary<string, OpenApiMediaType> Content { get => _content is not null ? _content : Target?.Content; set => _content = value; }
110119

120+
private IDictionary<string, IOpenApiExtension> _extensions;
111121
/// <inheritdoc/>
112-
public override IDictionary<string, IOpenApiExtension> Extensions { get => base.Extensions; set => base.Extensions = value; }
122+
public override IDictionary<string, IOpenApiExtension> Extensions { get => _extensions is not null ? _extensions : Target?.Extensions; set => _extensions = value; }
113123

114124
/// <inheritdoc/>
115125
public override void SerializeAsV31(IOpenApiWriter writer)

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ public OpenApiLink Target
2828
get
2929
{
3030
_target ??= Reference.HostDocument.ResolveReferenceTo<OpenApiLink>(_reference);
31-
OpenApiLink resolved = new OpenApiLink(_target);
32-
if (!string.IsNullOrEmpty(_description)) resolved.Description = _description;
33-
return resolved;
31+
if (!string.IsNullOrEmpty(_description)) _target.Description = _description;
32+
return _target;
3433
}
3534
}
3635

@@ -70,14 +69,17 @@ internal OpenApiLinkReference(OpenApiLink target, string referenceId)
7069
};
7170
}
7271

72+
private string _operationRef;
7373
/// <inheritdoc/>
74-
public override string OperationRef { get => Target.OperationRef; set => Target.OperationRef = value; }
74+
public override string OperationRef { get => !string.IsNullOrEmpty(_operationRef) ? _operationRef : Target?.OperationRef; set => _operationRef = value; }
7575

76+
private string _operationId;
7677
/// <inheritdoc/>
77-
public override string OperationId { get => Target.OperationId; set => Target.OperationId = value; }
78+
public override string OperationId { get => !string.IsNullOrEmpty(_operationId) ? _operationId : Target?.OperationId; set => _operationId = value; }
7879

80+
private OpenApiServer _server;
7981
/// <inheritdoc/>
80-
public override OpenApiServer Server { get => Target.Server; set => Target.Server = value; }
82+
public override OpenApiServer Server { get => _server is not null ? _server : Target?.Server; set => _server = value; }
8183

8284
/// <inheritdoc/>
8385
public override string Description
@@ -86,14 +88,17 @@ public override string Description
8688
set => _description = value;
8789
}
8890

91+
private Dictionary<string, RuntimeExpressionAnyWrapper> _parameters;
8992
/// <inheritdoc/>
90-
public override Dictionary<string, RuntimeExpressionAnyWrapper> Parameters { get => Target.Parameters; set => Target.Parameters = value; }
93+
public override Dictionary<string, RuntimeExpressionAnyWrapper> Parameters { get => _parameters is not null ? _parameters : Target?.Parameters; set => _parameters = value; }
9194

95+
private RuntimeExpressionAnyWrapper _requestBody;
9296
/// <inheritdoc/>
93-
public override RuntimeExpressionAnyWrapper RequestBody { get => Target.RequestBody; set => Target.RequestBody = value; }
97+
public override RuntimeExpressionAnyWrapper RequestBody { get => _requestBody is not null ? _requestBody : Target?.RequestBody; set => _requestBody = value; }
9498

99+
private IDictionary<string, IOpenApiExtension> _extensions;
95100
/// <inheritdoc/>
96-
public override IDictionary<string, IOpenApiExtension> Extensions { get => base.Extensions; set => base.Extensions = value; }
101+
public override IDictionary<string, IOpenApiExtension> Extensions { get => _extensions is not null ? _extensions : Target?.Extensions; set => _extensions = value; }
97102

98103
/// <inheritdoc/>
99104
public override void SerializeAsV3(IOpenApiWriter writer)

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ public OpenApiParameter Target
3131
get
3232
{
3333
_target ??= Reference.HostDocument.ResolveReferenceTo<OpenApiParameter>(_reference);
34-
OpenApiParameter resolved = new OpenApiParameter(_target);
35-
if (!string.IsNullOrEmpty(_description)) resolved.Description = _description;
36-
return resolved;
34+
if (!string.IsNullOrEmpty(_description)) _target.Description = _description;
35+
return _target;
3736
}
3837
}
3938

@@ -73,39 +72,48 @@ internal OpenApiParameterReference(OpenApiParameter target, string referenceId)
7372
};
7473
}
7574

75+
private string _name;
7676
/// <inheritdoc/>
77-
public override string Name { get => Target.Name; set => Target.Name = value; }
77+
public override string Name { get => !string.IsNullOrEmpty(_name) ? _name : Target?.Name; set => _name = value; }
7878

7979
/// <inheritdoc/>
8080
public override string Description
8181
{
82-
get => string.IsNullOrEmpty(_description) ? Target.Description : _description;
82+
get => string.IsNullOrEmpty(_description) ? Target?.Description : _description;
8383
set => _description = value;
8484
}
8585

86+
private bool? _required;
8687
/// <inheritdoc/>
87-
public override bool Required { get => Target.Required; set => Target.Required = value; }
88+
public override bool Required { get => _required is not null ? _required.Value : Target?.Required ?? false; set => _required = value; }
8889

90+
private bool? _deprecated;
8991
/// <inheritdoc/>
90-
public override bool Deprecated { get => Target.Deprecated; set => Target.Deprecated = value; }
92+
public override bool Deprecated { get => _deprecated is not null ? _deprecated.Value : Target?.Deprecated ?? false; set => _deprecated = value; }
9193

94+
private bool? _allowEmptyValue;
9295
/// <inheritdoc/>
93-
public override bool AllowEmptyValue { get => Target.AllowEmptyValue; set => Target.AllowEmptyValue = value; }
96+
public override bool AllowEmptyValue { get => _allowEmptyValue is not null ? _allowEmptyValue.Value : Target?.AllowEmptyValue ?? false; set => _allowEmptyValue = value; }
9497

98+
private bool? _allowReserved;
9599
/// <inheritdoc/>
96-
public override bool AllowReserved { get => Target.AllowReserved; set => Target.AllowReserved = value; }
100+
public override bool AllowReserved { get => _allowReserved is not null ? _allowReserved.Value : Target?.AllowReserved ?? false; set => _allowReserved = value; }
97101

102+
private OpenApiSchema _schema;
98103
/// <inheritdoc/>
99-
public override OpenApiSchema Schema { get => Target.Schema; set => Target.Schema = value; }
104+
public override OpenApiSchema Schema { get => _schema is not null ? _schema : Target?.Schema; set => _schema = value; }
100105

106+
private JsonNode _example;
101107
/// <inheritdoc/>
102-
public override IDictionary<string, OpenApiExample> Examples { get => Target.Examples; set => Target.Examples = value; }
108+
public override JsonNode Example { get => _example is not null ? _example : Target?.Example; set => _example = value; }
103109

110+
private IDictionary<string, OpenApiExample> _examples;
104111
/// <inheritdoc/>
105-
public override JsonNode Example { get => Target.Example; set => Target.Example = value; }
112+
public override IDictionary<string, OpenApiExample> Examples { get => _examples is not null ? _examples : Target?.Examples; set => _examples = value; }
106113

114+
private ParameterLocation? _in;
107115
/// <inheritdoc/>
108-
public override ParameterLocation? In { get => Target.In; set => Target.In = value; }
116+
public override ParameterLocation? In { get => _in is not null ? _in : Target?.In; set => _in = value; }
109117

110118
/// <inheritdoc/>
111119
public override ParameterStyle? Style
@@ -121,12 +129,14 @@ public override bool Explode
121129
set => _explode = value;
122130
}
123131

132+
private IDictionary<string, OpenApiMediaType> _content;
124133
/// <inheritdoc/>
125-
public override IDictionary<string, OpenApiMediaType> Content { get => Target.Content; set => Target.Content = value; }
134+
public override IDictionary<string, OpenApiMediaType> Content { get => _content is not null ? _content : Target?.Content; set => _content = value; }
126135

136+
private IDictionary<string, IOpenApiExtension> _extensions;
127137
/// <inheritdoc/>
128-
public override IDictionary<string, IOpenApiExtension> Extensions { get => Target.Extensions; set => Target.Extensions = value; }
129-
138+
public override IDictionary<string, IOpenApiExtension> Extensions { get => _extensions is not null ? _extensions : Target?.Extensions; set => _extensions = value; }
139+
130140
/// <inheritdoc/>
131141
public override void SerializeAsV3(IOpenApiWriter writer)
132142
{

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ public OpenApiPathItem Target
2929
get
3030
{
3131
_target ??= Reference.HostDocument.ResolveReferenceTo<OpenApiPathItem>(_reference);
32-
OpenApiPathItem resolved = new OpenApiPathItem(_target);
33-
if (!string.IsNullOrEmpty(_description)) resolved.Description = _description;
34-
if (!string.IsNullOrEmpty(_summary)) resolved.Summary = _summary;
35-
return resolved;
32+
if (!string.IsNullOrEmpty(_description)) _target.Description = _description;
33+
if (!string.IsNullOrEmpty(_summary)) _target.Summary = _summary;
34+
return _target;
3635
}
3736
}
3837

@@ -75,29 +74,33 @@ internal OpenApiPathItemReference(OpenApiPathItem target, string referenceId)
7574
/// <inheritdoc/>
7675
public override string Summary
7776
{
78-
get => string.IsNullOrEmpty(_summary) ? Target.Summary : _summary;
77+
get => string.IsNullOrEmpty(_summary) ? Target?.Summary : _summary;
7978
set => _summary = value;
8079
}
8180

8281
/// <inheritdoc/>
8382
public override string Description
8483
{
85-
get => string.IsNullOrEmpty(_description) ? Target.Description : _description;
84+
get => string.IsNullOrEmpty(_description) ? Target?.Description : _description;
8685
set => _description = value;
8786
}
8887

88+
private IDictionary<OperationType, OpenApiOperation> _operations;
8989
/// <inheritdoc/>
90-
public override IDictionary<OperationType, OpenApiOperation> Operations { get => Target.Operations; set => Target.Operations = value; }
90+
public override IDictionary<OperationType, OpenApiOperation> Operations { get => _operations is not null ? _operations : Target?.Operations; set => _operations = value; }
9191

92+
private IList<OpenApiServer> _servers;
9293
/// <inheritdoc/>
93-
public override IList<OpenApiServer> Servers { get => Target.Servers; set => Target.Servers = value; }
94+
public override IList<OpenApiServer> Servers { get => _servers is not null ? _servers : Target?.Servers; set => _servers = value; }
9495

96+
private IList<OpenApiParameter> _parameters;
9597
/// <inheritdoc/>
96-
public override IList<OpenApiParameter> Parameters { get => Target.Parameters; set => Target.Parameters = value; }
98+
public override IList<OpenApiParameter> Parameters { get => _parameters is not null ? _parameters : Target?.Parameters; set => _parameters = value; }
9799

100+
private IDictionary<string, IOpenApiExtension> _extensions;
98101
/// <inheritdoc/>
99-
public override IDictionary<string, IOpenApiExtension> Extensions { get => Target.Extensions; set => Target.Extensions = value; }
100-
102+
public override IDictionary<string, IOpenApiExtension> Extensions { get => _extensions is not null ? _extensions : Target?.Extensions; set => _extensions = value; }
103+
101104
/// <inheritdoc/>
102105
public override void SerializeAsV31(IOpenApiWriter writer)
103106
{

0 commit comments

Comments
 (0)