Skip to content

Commit 6f4e7a2

Browse files
committed
fix: uses backing fields instead of schema copy
Signed-off-by: Vincent Biret <[email protected]>
1 parent 30ee6ed commit 6f4e7a2

File tree

1 file changed

+105
-66
lines changed

1 file changed

+105
-66
lines changed

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

Lines changed: 105 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,68 @@ namespace Microsoft.OpenApi.Models.References
1414
/// </summary>
1515
public class OpenApiSchemaReference : OpenApiSchema
1616
{
17-
internal OpenApiSchema _target;
17+
#nullable enable
18+
private OpenApiSchema? _target;
1819
private readonly OpenApiReference _reference;
19-
private string _description;
20-
private JsonNode _default;
21-
private JsonNode _example;
22-
private IList<JsonNode> _examples;
20+
private string? _description;
21+
private JsonNode? _default;
22+
private JsonNode? _example;
23+
private IList<JsonNode>? _examples;
24+
private bool? _nullable;
25+
private IDictionary<string, OpenApiSchema>? _properties;
26+
private string? _title;
27+
private string? _schema;
28+
private string? _comment;
29+
private string? _id;
30+
private string? _dynamicRef;
31+
private string? _dynamicAnchor;
32+
private IDictionary<string, bool>? _vocabulary;
33+
private IDictionary<string, OpenApiSchema>? _definitions;
34+
private decimal? _v31ExclusiveMaximum;
35+
private decimal? _v31ExclusiveMinimum;
36+
private bool? _unEvaluatedProperties;
37+
private JsonSchemaType? _type;
38+
private string? _const;
39+
private string? _format;
40+
private decimal? _maximum;
41+
private bool? _exclusiveMaximum;
42+
private decimal? _minimum;
43+
private bool? _exclusiveMinimum;
44+
private int? _maxLength;
45+
private int? _minLength;
46+
private string? _pattern;
47+
private decimal? _multipleOf;
48+
private bool? _readOnly;
49+
private bool? _writeOnly;
50+
private IList<OpenApiSchema>? _allOf;
51+
private IList<OpenApiSchema>? _oneOf;
52+
private IList<OpenApiSchema>? _anyOf;
53+
private OpenApiSchema? _not;
54+
private ISet<string>? _required;
55+
private OpenApiSchema _items;
56+
private int? _maxItems;
57+
private int? _minItems;
58+
private bool? _uniqueItems;
59+
private IDictionary<string, OpenApiSchema>? _patternProperties;
60+
private int? _maxProperties;
61+
private int? _minProperties;
62+
private bool? _additionalPropertiesAllowed;
63+
private OpenApiSchema? _additionalProperties;
64+
private OpenApiDiscriminator? _discriminator;
65+
private OpenApiExternalDocs? _externalDocs;
66+
private bool? _deprecated;
67+
private OpenApiXml? _xml;
68+
private IDictionary<string, IOpenApiExtension>? _extensions;
69+
private bool? _unevaluatedProperties;
70+
private IList<JsonNode>? _enum;
2371

24-
#nullable enable
25-
private OpenApiSchema? _targetProxy;
72+
private OpenApiSchema? Target
2673
#nullable restore
27-
28-
private OpenApiSchema Target
2974
{
3075
get
3176
{
3277
_target ??= Reference.HostDocument?.ResolveReferenceTo<OpenApiSchema>(_reference);
33-
if (_targetProxy is null)
34-
{
35-
_targetProxy = new OpenApiSchema(_target);
36-
if (!string.IsNullOrEmpty(_description)) _targetProxy.Description = _description;
37-
}
38-
return _targetProxy;
78+
return _target;
3979
}
4080
}
4181

@@ -76,123 +116,123 @@ internal OpenApiSchemaReference(OpenApiSchema target, string referenceId)
76116
}
77117

78118
/// <inheritdoc/>
79-
public override string Title { get => Target.Title; set => Target.Title = value; }
119+
public override string Title { get => string.IsNullOrEmpty(_title) ? Target.Title : _title; set => _title = value; }
80120
/// <inheritdoc/>
81-
public override string Schema { get => Target.Schema; set => Target.Schema = value; }
121+
public override string Schema { get => string.IsNullOrEmpty(_schema) ? Target.Schema : _schema; set => _schema = value; }
82122
/// <inheritdoc/>
83-
public override string Id { get => Target.Id; set => Target.Id = value; }
123+
public override string Id { get => string.IsNullOrEmpty(_id) ? Target.Id : _id; set => _id = value; }
84124
/// <inheritdoc/>
85-
public override string Comment { get => Target.Comment; set => Target.Comment = value; }
125+
public override string Comment { get => string.IsNullOrEmpty(_comment) ? Target.Comment : _comment; set => _comment = value; }
86126
/// <inheritdoc/>
87-
public override IDictionary<string, bool> Vocabulary { get => Target.Vocabulary; set => Target.Vocabulary = value; }
127+
public override IDictionary<string, bool> Vocabulary { get => _vocabulary is not null ? _vocabulary : Target.Vocabulary; set => _vocabulary = value; }
88128
/// <inheritdoc/>
89-
public override string DynamicRef { get => Target.DynamicRef; set => Target.DynamicRef = value; }
129+
public override string DynamicRef { get => string.IsNullOrEmpty(_dynamicRef) ? Target.DynamicRef : _dynamicRef; set => _dynamicRef = value; }
90130
/// <inheritdoc/>
91-
public override string DynamicAnchor { get => Target.DynamicAnchor; set => Target.DynamicAnchor = value; }
131+
public override string DynamicAnchor { get => string.IsNullOrEmpty(_dynamicAnchor) ? Target.DynamicAnchor : _dynamicAnchor; set => _dynamicAnchor = value; }
92132
/// <inheritdoc/>
93-
public override IDictionary<string, OpenApiSchema> Definitions { get => Target.Definitions; set => Target.Definitions = value; }
133+
public override IDictionary<string, OpenApiSchema> Definitions { get => _definitions is not null ? _definitions : Target.Definitions; set => _definitions = value; }
94134
/// <inheritdoc/>
95-
public override decimal? V31ExclusiveMaximum { get => Target.V31ExclusiveMaximum; set => Target.V31ExclusiveMaximum = value; }
135+
public override decimal? V31ExclusiveMaximum { get => _v31ExclusiveMaximum is not null ? _v31ExclusiveMaximum.Value : Target.V31ExclusiveMaximum; set => _v31ExclusiveMaximum = value; }
96136
/// <inheritdoc/>
97-
public override decimal? V31ExclusiveMinimum { get => Target.V31ExclusiveMinimum; set => Target.V31ExclusiveMinimum = value; }
137+
public override decimal? V31ExclusiveMinimum { get => _v31ExclusiveMinimum is not null ? _v31ExclusiveMinimum.Value : Target.V31ExclusiveMinimum; set => _v31ExclusiveMinimum = value; }
98138
/// <inheritdoc/>
99-
public override bool UnEvaluatedProperties { get => Target.UnEvaluatedProperties; set => Target.UnEvaluatedProperties = value; }
139+
public override bool UnEvaluatedProperties { get => _unEvaluatedProperties is not null ? _unEvaluatedProperties.Value : Target.UnEvaluatedProperties; set => _unEvaluatedProperties = value; }
100140
/// <inheritdoc/>
101-
public override JsonSchemaType? Type { get => Target.Type; set => Target.Type = value; }
141+
public override JsonSchemaType? Type { get => _type is not null ? _type.Value : Target.Type; set => _type = value; }
102142
/// <inheritdoc/>
103-
public override string Const { get => Target.Const; set => Target.Const = value; }
143+
public override string Const { get => string.IsNullOrEmpty(_const) ? Target.Const : _const; set => _const = value; }
104144
/// <inheritdoc/>
105-
public override string Format { get => Target.Format; set => Target.Format = value; }
145+
public override string Format { get => string.IsNullOrEmpty(_format) ? Target.Format : _format; set => _format = value; }
106146
/// <inheritdoc/>
107147
public override string Description
108148
{
109149
get => string.IsNullOrEmpty(_description) ? Target.Description : _description;
110150
set => _description = value;
111151
}
112152
/// <inheritdoc/>
113-
public override decimal? Maximum { get => Target.Maximum; set => Target.Maximum = value; }
153+
public override decimal? Maximum { get => _maximum is not null ? _maximum : Target.Maximum; set => _maximum = value; }
114154
/// <inheritdoc/>
115-
public override bool? ExclusiveMaximum { get => Target.ExclusiveMaximum; set => Target.ExclusiveMaximum = value; }
155+
public override bool? ExclusiveMaximum { get => _exclusiveMaximum is not null ? _exclusiveMaximum : Target.ExclusiveMaximum; set => _exclusiveMaximum = value; }
116156
/// <inheritdoc/>
117-
public override decimal? Minimum { get => Target.Minimum; set => Target.Minimum = value; }
157+
public override decimal? Minimum { get => _minimum is not null ? _minimum : Target.Minimum; set => _minimum = value; }
118158
/// <inheritdoc/>
119-
public override bool? ExclusiveMinimum { get => Target.ExclusiveMinimum; set => Target.ExclusiveMinimum = value; }
159+
public override bool? ExclusiveMinimum { get => _exclusiveMinimum is not null ? _exclusiveMinimum : Target.ExclusiveMinimum; set => _exclusiveMinimum = value; }
120160
/// <inheritdoc/>
121-
public override int? MaxLength { get => Target.MaxLength; set => Target.MaxLength = value; }
161+
public override int? MaxLength { get => _maxLength is not null ? _maxLength : Target.MaxLength; set => _maxLength = value; }
122162
/// <inheritdoc/>
123-
public override int? MinLength { get => Target.MinLength; set => Target.MinLength = value; }
163+
public override int? MinLength { get => _minLength is not null ? _minLength : Target.MinLength; set => _minLength = value; }
124164
/// <inheritdoc/>
125-
public override string Pattern { get => Target.Pattern; set => Target.Pattern = value; }
165+
public override string Pattern { get => string.IsNullOrEmpty(_pattern) ? Target.Pattern : _pattern; set => _pattern = value; }
126166
/// <inheritdoc/>
127-
public override decimal? MultipleOf { get => Target.MultipleOf; set => Target.MultipleOf = value; }
167+
public override decimal? MultipleOf { get => _multipleOf is not null ? _multipleOf : Target.MultipleOf; set => _multipleOf = value; }
128168
/// <inheritdoc/>
129169
public override JsonNode Default
130170
{
131-
get => _default ??= Target.Default;
171+
get => _default ??= Target.Default; //TODO normalize like other properties
132172
set => _default = value;
133173
}
134174
/// <inheritdoc/>
135-
public override bool ReadOnly { get => Target.ReadOnly; set => Target.ReadOnly = value; }
175+
public override bool ReadOnly { get => _readOnly is not null ? _readOnly.Value : Target.ReadOnly; set => _readOnly = value; }
136176
/// <inheritdoc/>
137-
public override bool WriteOnly { get => Target.WriteOnly; set => Target.WriteOnly = value; }
177+
public override bool WriteOnly { get => _writeOnly is not null ? _writeOnly.Value : Target.WriteOnly; set => _writeOnly = value; }
138178
/// <inheritdoc/>
139-
public override IList<OpenApiSchema> AllOf { get => Target.AllOf; set => Target.AllOf = value; }
179+
public override IList<OpenApiSchema> AllOf { get => _allOf is not null ? _allOf : Target.AllOf; set => _allOf = value; }
140180
/// <inheritdoc/>
141-
public override IList<OpenApiSchema> OneOf { get => Target.OneOf; set => Target.OneOf = value; }
181+
public override IList<OpenApiSchema> OneOf { get => _oneOf is not null ? _oneOf : Target.OneOf; set => _oneOf = value; }
142182
/// <inheritdoc/>
143-
public override IList<OpenApiSchema> AnyOf { get => Target.AnyOf; set => Target.AnyOf = value; }
183+
public override IList<OpenApiSchema> AnyOf { get => _anyOf is not null ? _anyOf : Target.AnyOf; set => _anyOf = value; }
144184
/// <inheritdoc/>
145-
public override OpenApiSchema Not { get => Target.Not; set => Target.Not = value; }
185+
public override OpenApiSchema Not { get => _not is not null ? _not : Target.Not; set => _not = value; }
146186
/// <inheritdoc/>
147-
public override ISet<string> Required { get => Target.Required; set => Target.Required = value; }
187+
public override ISet<string> Required { get => _required is not null ? _required : Target.Required; set => _required = value; }
148188
/// <inheritdoc/>
149-
public override OpenApiSchema Items { get => Target.Items; set => Target.Items = value; }
189+
public override OpenApiSchema Items { get => _items is not null ? _items : Target.Items; set => _items = value; }
150190
/// <inheritdoc/>
151-
public override int? MaxItems { get => Target.MaxItems; set => Target.MaxItems = value; }
191+
public override int? MaxItems { get => _maxItems is not null ? _maxItems : Target.MaxItems; set => _maxItems = value; }
152192
/// <inheritdoc/>
153-
public override int? MinItems { get => Target.MinItems; set => Target.MinItems = value; }
193+
public override int? MinItems { get => _minItems is not null ? _minItems : Target.MinItems; set => _minItems = value; }
154194
/// <inheritdoc/>
155-
public override bool? UniqueItems { get => Target.UniqueItems; set => Target.UniqueItems = value; }
195+
public override bool? UniqueItems { get => _uniqueItems is not null ? _uniqueItems : Target.UniqueItems; set => _uniqueItems = value; }
156196
/// <inheritdoc/>
157-
public override IDictionary<string, OpenApiSchema> Properties { get => Target.Properties; set => Target.Properties = value; }
197+
public override IDictionary<string, OpenApiSchema> Properties { get => _properties is not null ? _properties : Target.Properties ; set => _properties = value; }
158198
/// <inheritdoc/>
159-
public override IDictionary<string, OpenApiSchema> PatternProperties { get => Target.PatternProperties; set => Target.PatternProperties = value; }
199+
public override IDictionary<string, OpenApiSchema> PatternProperties { get => _patternProperties is not null ? _patternProperties : Target.PatternProperties; set => _patternProperties = value; }
160200
/// <inheritdoc/>
161-
public override int? MaxProperties { get => Target.MaxProperties; set => Target.MaxProperties = value; }
201+
public override int? MaxProperties { get => _maxProperties is not null ? _maxProperties : Target.MaxProperties; set => _maxProperties = value; }
162202
/// <inheritdoc/>
163-
public override int? MinProperties { get => Target.MinProperties; set => Target.MinProperties = value; }
203+
public override int? MinProperties { get => _minProperties is not null ? _minProperties : Target.MinProperties; set => _minProperties = value; }
164204
/// <inheritdoc/>
165-
public override bool AdditionalPropertiesAllowed { get => Target.AdditionalPropertiesAllowed; set => Target.AdditionalPropertiesAllowed = value; }
205+
public override bool AdditionalPropertiesAllowed { get => _additionalPropertiesAllowed is not null ? _additionalPropertiesAllowed.Value : Target.AdditionalPropertiesAllowed; set => _additionalPropertiesAllowed = value; }
166206
/// <inheritdoc/>
167-
public override OpenApiSchema AdditionalProperties { get => Target.AdditionalProperties; set => Target.AdditionalProperties = value; }
207+
public override OpenApiSchema AdditionalProperties { get => _additionalProperties is not null ? _additionalProperties : Target.AdditionalProperties; set => _additionalProperties = value; }
168208
/// <inheritdoc/>
169-
public override OpenApiDiscriminator Discriminator { get => Target.Discriminator; set => Target.Discriminator = value; }
209+
public override OpenApiDiscriminator Discriminator { get => _discriminator is not null ? _discriminator : Target.Discriminator; set => _discriminator = value; }
170210
/// <inheritdoc/>
171211
public override JsonNode Example
172212
{
173-
get => _example ??= Target.Example;
213+
get => _example ??= Target.Example; //TODO normalize like other properties
174214
set => _example = value;
175215
}
176216
/// <inheritdoc/>
177217
public override IList<JsonNode> Examples
178218
{
179-
get => _examples ??= Target.Examples;
219+
get => _examples ??= Target.Examples; //TODO normalize like other properties
180220
set => Target.Examples = value;
181221
}
182222
/// <inheritdoc/>
183-
public override IList<JsonNode> Enum { get => Target.Enum; set => Target.Enum = value; }
223+
public override IList<JsonNode> Enum { get => _enum is not null ? _enum : Target.Enum; set => _enum = value; }
184224
/// <inheritdoc/>
185-
public override bool Nullable { get => Target.Nullable; set => Target.Nullable = value; }
225+
public override bool Nullable { get => _nullable is null ? Target.Nullable : _nullable.Value; set => _nullable = value; }
186226
/// <inheritdoc/>
187-
public override bool UnevaluatedProperties { get => Target.UnevaluatedProperties; set => Target.UnevaluatedProperties = value; }
227+
public override bool UnevaluatedProperties { get => _unevaluatedProperties is not null ? _unevaluatedProperties.Value : Target.UnevaluatedProperties; set => _unevaluatedProperties = value; }
188228
/// <inheritdoc/>
189-
public override OpenApiExternalDocs ExternalDocs { get => Target.ExternalDocs; set => Target.ExternalDocs = value; }
229+
public override OpenApiExternalDocs ExternalDocs { get => _externalDocs is not null ? _externalDocs : Target.ExternalDocs; set => _externalDocs = value; }
190230
/// <inheritdoc/>
191-
public override bool Deprecated { get => Target.Deprecated; set => Target.Deprecated = value; }
231+
public override bool Deprecated { get => _deprecated is not null ? _deprecated.Value : Target.Deprecated; set => _deprecated = value; }
192232
/// <inheritdoc/>
193-
public override OpenApiXml Xml { get => Target.Xml; set => Target.Xml = value; }
233+
public override OpenApiXml Xml { get => _xml is not null ? _xml : Target.Xml; set => _xml = value; }
194234
/// <inheritdoc/>
195-
public override IDictionary<string, IOpenApiExtension> Extensions { get => Target.Extensions; set => Target.Extensions = value; }
235+
public override IDictionary<string, IOpenApiExtension> Extensions { get => _extensions is not null ? _extensions : Target.Extensions; set => _extensions = value; }
196236

197237
/// <inheritdoc/>
198238
public override void SerializeAsV31(IOpenApiWriter writer)
@@ -240,7 +280,6 @@ public override void SerializeAsV2(IOpenApiWriter writer)
240280
if (!writer.GetSettings().ShouldInlineReference(_reference))
241281
{
242282
_reference.SerializeAsV2(writer);
243-
return;
244283
}
245284
else
246285
{

0 commit comments

Comments
 (0)