Skip to content

Commit f143034

Browse files
committed
Different approach
1 parent 0e7bec0 commit f143034

6 files changed

Lines changed: 51 additions & 36 deletions

File tree

src/Microsoft.OpenApi/Models/JsonSchemaReference.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,22 @@ public class JsonSchemaReference : OpenApiReferenceWithDescription
114114
/// </summary>
115115
public JsonSchemaType? SchemaType { get; set; }
116116

117+
internal bool WasConstExplicitlySet { get; private set; }
118+
117119
/// <summary>
118120
/// Follow <see href="https://json-schema.org/draft/2020-12/json-schema-validation">JSON Schema definition</see>.
119121
/// </summary>
120-
public string? Const { get; set; } = OpenApiUnsetValues.UnsetString;
122+
public string? Const
123+
{
124+
get => field;
125+
set
126+
{
127+
// TODO: In the next major release, Const should be made a JsonNode.
128+
// See https://github.com/microsoft/OpenAPI.NET/issues/2935 for more information.
129+
WasConstExplicitlySet = true;
130+
field = value;
131+
}
132+
}
121133

122134
/// <summary>
123135
/// Follow <see href="https://json-schema.org/draft/2020-12/json-schema-validation">JSON Schema definition</see>.
@@ -365,6 +377,7 @@ public JsonSchemaReference(JsonSchemaReference reference) : base(reference)
365377
ExclusiveMinimum = reference.ExclusiveMinimum;
366378
SchemaType = reference.SchemaType;
367379
Const = reference.Const;
380+
WasConstExplicitlySet = reference.WasConstExplicitlySet;
368381
Format = reference.Format;
369382
Maximum = reference.Maximum;
370383
Minimum = reference.Minimum;
@@ -436,7 +449,11 @@ private void SerializeAdditionalV3XProperties(IOpenApiWriter writer, Action<IOpe
436449
writer.WriteProperty(OpenApiConstants.DynamicRef, DynamicRef);
437450
writer.WriteProperty(OpenApiConstants.DynamicAnchor, DynamicAnchor);
438451

439-
writer.WriteRequiredProperty(OpenApiConstants.Const, Const);
452+
if (WasConstExplicitlySet)
453+
{
454+
writer.WriteRequiredProperty(OpenApiConstants.Const, Const);
455+
}
456+
440457
WriteSchemaType(writer, OpenApiConstants.Type, SchemaType, allowMultipleTypes: true);
441458
writer.WriteProperty(OpenApiConstants.Format, Format);
442459
writer.WriteProperty(OpenApiConstants.MultipleOf, MultipleOf);
@@ -607,6 +624,7 @@ internal void ApplySchemaMetadata(OpenApiSchema schema, JsonObject jsonObject)
607624
ExclusiveMinimum = schema.ExclusiveMinimum;
608625
SchemaType = schema.Type;
609626
Const = schema.Const;
627+
WasConstExplicitlySet = schema.WasConstExplicitlySet;
610628
Format = schema.Format;
611629
Maximum = schema.Maximum;
612630
Minimum = schema.Minimum;

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,20 @@ Extensions is not null &&
117117
nullExtRawValue is JsonNodeExtension { Node: JsonNode jsonNode } &&
118118
jsonNode.GetValueKind() is JsonValueKind.True;
119119

120+
internal bool WasConstExplicitlySet { get; private set; }
121+
120122
/// <inheritdoc />
121-
public string? Const { get; set; } = OpenApiUnsetValues.UnsetString;
123+
public string? Const
124+
{
125+
get => field;
126+
set
127+
{
128+
// TODO: In the next major release, Const should be made a JsonNode.
129+
// See https://github.com/microsoft/OpenAPI.NET/issues/2935 for more information.
130+
WasConstExplicitlySet = true;
131+
field = value;
132+
}
133+
}
122134

123135
/// <inheritdoc />
124136
public string? Format { get; set; }
@@ -316,6 +328,15 @@ internal OpenApiSchema(IOpenApiSchema schema)
316328
Title = schema.Title ?? Title;
317329
Id = schema.Id ?? Id;
318330
Const = schema.Const ?? Const;
331+
if (schema is OpenApiSchema concreteSchema)
332+
{
333+
WasConstExplicitlySet = concreteSchema.WasConstExplicitlySet;
334+
}
335+
else if (Const is null)
336+
{
337+
WasConstExplicitlySet = false;
338+
}
339+
319340
Schema = schema.Schema ?? Schema;
320341
Comment = schema.Comment ?? Comment;
321342
Vocabulary = schema.Vocabulary != null ? new Dictionary<string, bool>(schema.Vocabulary) : null;
@@ -511,7 +532,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
511532

512533
// enum
513534
var enumValue = Enum is not { Count: > 0 }
514-
&& !ReferenceEquals(Const, OpenApiUnsetValues.UnsetString)
535+
&& WasConstExplicitlySet
515536
&& version < OpenApiSpecVersion.OpenApi3_1
516537
? new List<JsonNode> { JsonValue.Create(Const)! }
517538
: Enum;
@@ -685,7 +706,12 @@ internal void WriteJsonSchemaKeywords(IOpenApiWriter writer, Action<IOpenApiWrit
685706
writer.WriteProperty(OpenApiConstants.Id, Id);
686707
writer.WriteProperty(OpenApiConstants.DollarSchema, Schema?.ToString());
687708
writer.WriteProperty(OpenApiConstants.Comment, Comment);
688-
writer.WriteRequiredProperty(OpenApiConstants.Const, Const);
709+
710+
if (WasConstExplicitlySet)
711+
{
712+
writer.WriteRequiredProperty(OpenApiConstants.Const, Const);
713+
}
714+
689715
writer.WriteOptionalMap(OpenApiConstants.Vocabulary, Vocabulary, (w, s) => w.WriteValue(s));
690716
writer.WriteOptionalMap(OpenApiConstants.Defs, Definitions, callback);
691717
writer.WriteProperty(OpenApiConstants.Anchor, Anchor);
@@ -891,7 +917,7 @@ private void SerializeAsV2(
891917
});
892918

893919
// enum
894-
var enumValue = Enum is not { Count: > 0 } && !ReferenceEquals(Const, OpenApiUnsetValues.UnsetString)
920+
var enumValue = Enum is not { Count: > 0 } && WasConstExplicitlySet
895921
? new List<JsonNode> { JsonValue.Create(Const)! }
896922
: Enum;
897923
writer.WriteOptionalCollection(OpenApiConstants.Enum, enumValue, (nodeWriter, s) => nodeWriter.WriteAny(s));

src/Microsoft.OpenApi/Models/OpenApiUnsetValues.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public string? Title
7171
/// <inheritdoc/>
7272
public JsonSchemaType? Type { get => Reference.SchemaType ?? Target?.Type; set => Reference.SchemaType = value; }
7373
/// <inheritdoc/>
74-
public string? Const { get => string.IsNullOrEmpty(Reference.Const) ? Target?.Const : Reference.Const; set => Reference.Const = value; }
74+
public string? Const { get => Reference.WasConstExplicitlySet ? Reference.Const : Target?.Const; set => Reference.Const = value; }
7575
/// <inheritdoc/>
7676
public string? Format { get => string.IsNullOrEmpty(Reference.Format) ? Target?.Format : Reference.Format; set => Reference.Format = value; }
7777
/// <inheritdoc/>
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
#nullable enable
2-
Microsoft.OpenApi.OpenApiUnsetValues
3-
static readonly Microsoft.OpenApi.OpenApiUnsetValues.UnsetString -> string!

src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ public static void WriteRequiredProperty(this IOpenApiWriter writer, string name
4242
{
4343
Utils.CheckArgumentNullOrEmpty(name);
4444

45-
if (ReferenceEquals(value, OpenApiUnsetValues.UnsetString))
46-
{
47-
return;
48-
}
49-
5045
writer.WritePropertyName(name);
5146
if (value == null)
5247
{

0 commit comments

Comments
 (0)