Skip to content

Commit 58e3cd6

Browse files
committed
Add tests and update public API
1 parent 57ca1cc commit 58e3cd6

File tree

4 files changed

+281
-4
lines changed

4 files changed

+281
-4
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using FluentAssertions;
7+
using Microsoft.OpenApi.Models;
8+
using Microsoft.OpenApi.Reader;
9+
using Xunit;
10+
11+
namespace Microsoft.OpenApi.Readers.Tests.V31Tests
12+
{
13+
public class OpenApiSchemaTests
14+
{
15+
private const string SampleFolderPath = "V31Tests/Samples/OpenApiSchema/";
16+
17+
[Fact]
18+
public void ParseBasicV31SchemaShouldSucceed()
19+
{
20+
var expectedObject = new OpenApiSchema()
21+
{
22+
Id = "https://example.com/arrays.schema.json",
23+
Schema = "https://json-schema.org/draft/2020-12/schema",
24+
Description = "A representation of a person, company, organization, or place",
25+
Type = "object",
26+
Properties = new Dictionary<string, OpenApiSchema>
27+
{
28+
["fruits"] = new OpenApiSchema
29+
{
30+
Type = "array",
31+
Items = new OpenApiSchema
32+
{
33+
Type = "string"
34+
}
35+
},
36+
["vegetables"] = new OpenApiSchema
37+
{
38+
Type = "array"
39+
}
40+
},
41+
Definitions = new Dictionary<string, OpenApiSchema>
42+
{
43+
["veggie"] = new OpenApiSchema
44+
{
45+
Type = "object",
46+
Required = new HashSet<string>
47+
{
48+
"veggieName",
49+
"veggieLike"
50+
},
51+
Properties = new Dictionary<string, OpenApiSchema>
52+
{
53+
["veggieName"] = new OpenApiSchema
54+
{
55+
Type = "string",
56+
Description = "The name of the vegetable."
57+
},
58+
["veggieLike"] = new OpenApiSchema
59+
{
60+
Type = "boolean",
61+
Description = "Do I like this vegetable?"
62+
}
63+
}
64+
}
65+
}
66+
};
67+
68+
// Act
69+
var schema = OpenApiModelFactory.Load<OpenApiSchema>(
70+
Path.Combine(SampleFolderPath, "jsonSchema.json"), OpenApiSpecVersion.OpenApi3_1, out _);
71+
72+
// Assert
73+
schema.Should().BeEquivalentTo(expectedObject);
74+
}
75+
76+
[Fact]
77+
public void ParseSchemaWithTypeArrayWorks()
78+
{
79+
// Arrange
80+
var schema = @"{
81+
""$id"": ""https://example.com/arrays.schema.json"",
82+
""$schema"": ""https://json-schema.org/draft/2020-12/schema"",
83+
""description"": ""A representation of a person, company, organization, or place"",
84+
""type"": [""object"", ""null""]
85+
}";
86+
87+
var expected = new OpenApiSchema()
88+
{
89+
Id = "https://example.com/arrays.schema.json",
90+
Schema = "https://json-schema.org/draft/2020-12/schema",
91+
Description = "A representation of a person, company, organization, or place",
92+
Type = new string[] { "object", "null" }
93+
};
94+
95+
// Act
96+
var actual = OpenApiModelFactory.Parse<OpenApiSchema>(schema, OpenApiSpecVersion.OpenApi3_1, out _);
97+
98+
// Assert
99+
actual.Should().BeEquivalentTo(expected);
100+
}
101+
102+
[Fact]
103+
public void TestSchemaCopyConstructorWithTypeArrayWorks()
104+
{
105+
/* Arrange
106+
* Test schema's copy constructor for deep-cloning type array
107+
*/
108+
var schemaWithTypeArray = new OpenApiSchema()
109+
{
110+
Type = new string[] { "array", "null" },
111+
Items = new OpenApiSchema
112+
{
113+
Type = "string"
114+
}
115+
};
116+
117+
var simpleSchema = new OpenApiSchema()
118+
{
119+
Type = "string"
120+
};
121+
122+
// Act
123+
var schemaWithArrayCopy = new OpenApiSchema(schemaWithTypeArray);
124+
schemaWithArrayCopy.Type = "string";
125+
126+
var simpleSchemaCopy = new OpenApiSchema(simpleSchema);
127+
simpleSchemaCopy.Type = new string[] { "string", "null" };
128+
129+
// Assert
130+
schemaWithArrayCopy.Type.Should().NotBeEquivalentTo(schemaWithTypeArray.Type);
131+
schemaWithTypeArray.Type = new string[] { "string", "null" };
132+
133+
simpleSchemaCopy.Type.Should().NotBeEquivalentTo(simpleSchema.Type);
134+
simpleSchema.Type = "string";
135+
}
136+
}
137+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"$id": "https://example.com/arrays.schema.json",
3+
"$schema": "https://json-schema.org/draft/2020-12/schema",
4+
"description": "A representation of a person, company, organization, or place",
5+
"type": "object",
6+
"properties": {
7+
"fruits": {
8+
"type": "array",
9+
"items": {
10+
"type": "string"
11+
}
12+
},
13+
"vegetables": {
14+
"type": "array"
15+
}
16+
},
17+
"$defs": {
18+
"veggie": {
19+
"type": "object",
20+
"required": [ "veggieName", "veggieLike" ],
21+
"properties": {
22+
"veggieName": {
23+
"type": "string",
24+
"description": "The name of the vegetable."
25+
},
26+
"veggieLike": {
27+
"type": "boolean",
28+
"description": "Do I like this vegetable?"
29+
}
30+
}
31+
}
32+
}
33+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using Microsoft.OpenApi.Models;
6+
using Xunit;
7+
using FluentAssertions;
8+
using Microsoft.OpenApi.Extensions;
9+
10+
namespace Microsoft.OpenApi.Tests.Models
11+
{
12+
public class OpenApiSchemaTests
13+
{
14+
public static OpenApiSchema BasicV31Schema = new()
15+
{
16+
Id = "https://example.com/arrays.schema.json",
17+
Schema = "https://json-schema.org/draft/2020-12/schema",
18+
Description = "A representation of a person, company, organization, or place",
19+
Type = "object",
20+
Properties = new Dictionary<string, OpenApiSchema>
21+
{
22+
["fruits"] = new OpenApiSchema
23+
{
24+
Type = "array",
25+
Items = new OpenApiSchema
26+
{
27+
Type = "string"
28+
}
29+
},
30+
["vegetables"] = new OpenApiSchema
31+
{
32+
Type = "array"
33+
}
34+
},
35+
Definitions = new Dictionary<string, OpenApiSchema>
36+
{
37+
["veggie"] = new OpenApiSchema
38+
{
39+
Type = "object",
40+
Required = new HashSet<string>{ "veggieName", "veggieLike" },
41+
Properties = new Dictionary<string, OpenApiSchema>
42+
{
43+
["veggieName"] = new OpenApiSchema
44+
{
45+
Type = "string",
46+
Description = "The name of the vegetable."
47+
},
48+
["veggieLike"] = new OpenApiSchema
49+
{
50+
Type = "boolean",
51+
Description = "Do I like this vegetable?"
52+
}
53+
}
54+
}
55+
}
56+
};
57+
58+
[Fact]
59+
public void SerializeBasicV31SchemaWorks()
60+
{
61+
// Arrange
62+
var expected = @"{
63+
""$id"": ""https://example.com/arrays.schema.json"",
64+
""$schema"": ""https://json-schema.org/draft/2020-12/schema"",
65+
""$defs"": {
66+
""veggie"": {
67+
""required"": [
68+
""veggieName"",
69+
""veggieLike""
70+
],
71+
""type"": ""object"",
72+
""properties"": {
73+
""veggieName"": {
74+
""type"": ""string"",
75+
""description"": ""The name of the vegetable.""
76+
},
77+
""veggieLike"": {
78+
""type"": ""boolean"",
79+
""description"": ""Do I like this vegetable?""
80+
}
81+
}
82+
}
83+
},
84+
""type"": ""object"",
85+
""properties"": {
86+
""fruits"": {
87+
""type"": ""array"",
88+
""items"": {
89+
""type"": ""string""
90+
}
91+
},
92+
""vegetables"": {
93+
""type"": ""array""
94+
}
95+
},
96+
""description"": ""A representation of a person, company, organization, or place""
97+
}";
98+
99+
// Act
100+
var actual = BasicV31Schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_1);
101+
102+
// Assert
103+
actual = actual.MakeLineBreaksEnvironmentNeutral();
104+
expected = expected.MakeLineBreaksEnvironmentNeutral();
105+
actual.Should().Be(expected);
106+
}
107+
}
108+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,14 +1003,13 @@ namespace Microsoft.OpenApi.Models
10031003
public System.Collections.Generic.ISet<string> Required { get; set; }
10041004
public string Schema { get; set; }
10051005
public string Title { get; set; }
1006-
public string Type { get; set; }
1007-
public string[] TypeArray { get; set; }
1006+
public object Type { get; set; }
10081007
public bool UnEvaluatedProperties { get; set; }
10091008
public bool UnevaluatedProperties { get; set; }
10101009
public bool? UniqueItems { get; set; }
10111010
public bool UnresolvedReference { get; set; }
1012-
public decimal V31ExclusiveMaximum { get; set; }
1013-
public decimal V31ExclusiveMinimum { get; set; }
1011+
public decimal? V31ExclusiveMaximum { get; set; }
1012+
public decimal? V31ExclusiveMinimum { get; set; }
10141013
public string Vocabulary { get; set; }
10151014
public bool WriteOnly { get; set; }
10161015
public Microsoft.OpenApi.Models.OpenApiXml Xml { get; set; }

0 commit comments

Comments
 (0)