Skip to content

Commit 70c7d43

Browse files
committed
Add test and update API
1 parent 067fbdd commit 70c7d43

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,5 +452,48 @@ public void SerializeSchemaWithJsonSchemaKeywordsWorks()
452452
schema.Vocabulary.Keys.Count.Should().Be(5);
453453
schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral());
454454
}
455+
456+
[Fact]
457+
public void ParseSchemaWithConstWorks()
458+
{
459+
var expected = @"{
460+
""$schema"": ""https://json-schema.org/draft/2020-12/schema"",
461+
""required"": [
462+
""status""
463+
],
464+
""type"": ""object"",
465+
""properties"": {
466+
""status"": {
467+
""const"": ""active"",
468+
""type"": ""string""
469+
},
470+
""user"": {
471+
""required"": [
472+
""role""
473+
],
474+
""type"": ""object"",
475+
""properties"": {
476+
""role"": {
477+
""const"": ""admin"",
478+
""type"": ""string""
479+
}
480+
}
481+
}
482+
}
483+
}";
484+
485+
var path = Path.Combine(SampleFolderPath, "schemaWithConst.json");
486+
487+
// Act
488+
var schema = OpenApiModelFactory.Load<OpenApiSchema>(path, OpenApiSpecVersion.OpenApi3_1, out _);
489+
schema.Properties["status"].Const.Should().Be("active");
490+
schema.Properties["user"].Properties["role"].Const.Should().Be("admin");
491+
492+
// serialization
493+
var writer = new StringWriter();
494+
schema.SerializeAsV31(new OpenApiJsonWriter(writer));
495+
var schemaString = writer.ToString();
496+
schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral());
497+
}
455498
}
456499
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"type": "object",
4+
"properties": {
5+
"status": {
6+
"type": "string",
7+
"const": "active"
8+
},
9+
"user": {
10+
"type": "object",
11+
"properties": {
12+
"role": {
13+
"type": "string",
14+
"const": "admin"
15+
}
16+
},
17+
"required": [ "role" ]
18+
}
19+
},
20+
"required": [ "status" ]
21+
}

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ namespace Microsoft.OpenApi.Models
398398
public const string Comment = "$comment";
399399
public const string Components = "components";
400400
public const string ComponentsSegment = "/components/";
401+
public const string Const = "const";
401402
public const string Consumes = "consumes";
402403
public const string Contact = "contact";
403404
public const string Content = "content";
@@ -882,6 +883,7 @@ namespace Microsoft.OpenApi.Models
882883
public virtual System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSchema> AllOf { get; set; }
883884
public virtual System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSchema> AnyOf { get; set; }
884885
public virtual string Comment { get; set; }
886+
public virtual string Const { get; set; }
885887
public virtual System.Text.Json.Nodes.JsonNode Default { get; set; }
886888
public virtual System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Models.OpenApiSchema> Definitions { get; set; }
887889
public virtual bool Deprecated { get; set; }
@@ -1222,6 +1224,7 @@ namespace Microsoft.OpenApi.Models.References
12221224
public override System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSchema> AllOf { get; set; }
12231225
public override System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSchema> AnyOf { get; set; }
12241226
public override string Comment { get; set; }
1227+
public override string Const { get; set; }
12251228
public override System.Text.Json.Nodes.JsonNode Default { get; set; }
12261229
public override System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Models.OpenApiSchema> Definitions { get; set; }
12271230
public override bool Deprecated { get; set; }

0 commit comments

Comments
 (0)