Skip to content

Commit 1234bfa

Browse files
committed
Add Components unit test to verify that top-level references are serialized correctly
1 parent 46dbe16 commit 1234bfa

File tree

1 file changed

+175
-8
lines changed

1 file changed

+175
-8
lines changed

test/Microsoft.OpenApi.Tests/Models/OpenApiComponentsTests.cs

Lines changed: 175 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ public class OpenApiComponentsTests
7777
},
7878
["property3"] = new OpenApiSchema
7979
{
80-
Type = "string",
81-
MaxLength = 15
80+
Reference = new OpenApiReference
81+
{
82+
Type = ReferenceType.Schema,
83+
Id = "schema2"
84+
}
8285
}
8386
},
8487
Reference = new OpenApiReference
@@ -87,6 +90,16 @@ public class OpenApiComponentsTests
8790
Id = "schema1"
8891
}
8992
},
93+
["schema2"] = new OpenApiSchema
94+
{
95+
Properties = new Dictionary<string, OpenApiSchema>
96+
{
97+
["property2"] = new OpenApiSchema
98+
{
99+
Type = "integer"
100+
}
101+
}
102+
},
90103
},
91104
SecuritySchemes = new Dictionary<string, OpenApiSecurityScheme>
92105
{
@@ -157,6 +170,81 @@ public class OpenApiComponentsTests
157170
}
158171
};
159172

173+
public static OpenApiComponents TopLevelReferencingComponents = new OpenApiComponents()
174+
{
175+
Schemas =
176+
{
177+
["schema1"] = new OpenApiSchema
178+
{
179+
Reference = new OpenApiReference()
180+
{
181+
Type = ReferenceType.Schema,
182+
Id = "schema2"
183+
}
184+
},
185+
["schema2"] = new OpenApiSchema
186+
{
187+
Type = "object",
188+
Properties =
189+
{
190+
["property1"] = new OpenApiSchema()
191+
{
192+
Type = "string"
193+
}
194+
}
195+
},
196+
}
197+
};
198+
199+
public static OpenApiComponents TopLevelSelfReferencingComponentsWithOtherProperties = new OpenApiComponents()
200+
{
201+
Schemas =
202+
{
203+
["schema1"] = new OpenApiSchema
204+
{
205+
Type = "object",
206+
Properties =
207+
{
208+
["property1"] = new OpenApiSchema()
209+
{
210+
Type = "string"
211+
}
212+
},
213+
Reference = new OpenApiReference()
214+
{
215+
Type = ReferenceType.Schema,
216+
Id = "schema1"
217+
}
218+
},
219+
["schema2"] = new OpenApiSchema
220+
{
221+
Type = "object",
222+
Properties =
223+
{
224+
["property1"] = new OpenApiSchema()
225+
{
226+
Type = "string"
227+
}
228+
}
229+
},
230+
}
231+
};
232+
233+
public static OpenApiComponents TopLevelSelfReferencingComponents = new OpenApiComponents()
234+
{
235+
Schemas =
236+
{
237+
["schema1"] = new OpenApiSchema
238+
{
239+
Reference = new OpenApiReference()
240+
{
241+
Type = ReferenceType.Schema,
242+
Id = "schema1"
243+
}
244+
}
245+
}
246+
};
247+
160248
private readonly ITestOutputHelper _output;
161249

162250
public OpenApiComponentsTests(ITestOutputHelper output)
@@ -255,8 +343,14 @@ public void SerializeAdvancedComponentsWithReferenceAsJsonV3Works()
255343
""type"": ""integer""
256344
},
257345
""property3"": {
258-
""maxLength"": 15,
259-
""type"": ""string""
346+
""$ref"": ""#/components/schemas/schema2""
347+
}
348+
}
349+
},
350+
""schema2"": {
351+
""properties"": {
352+
""property2"": {
353+
""type"": ""integer""
260354
}
261355
}
262356
}
@@ -284,8 +378,8 @@ public void SerializeAdvancedComponentsWithReferenceAsJsonV3Works()
284378
}";
285379

286380
// Act
287-
var actual = AdvancedComponentsWithReference.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0_0);
288-
381+
var actual = AdvancedComponentsWithReference.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0_0);
382+
_output.WriteLine(actual);
289383
// Assert
290384
actual = actual.MakeLineBreaksEnvironmentNeutral();
291385
expected = expected.MakeLineBreaksEnvironmentNeutral();
@@ -338,8 +432,11 @@ public void SerializeAdvancedComponentsWithReferenceAsYamlV3Works()
338432
property2:
339433
type: integer
340434
property3:
341-
maxLength: 15
342-
type: string
435+
$ref: '#/components/schemas/schema2'
436+
schema2:
437+
properties:
438+
property2:
439+
type: integer
343440
securitySchemes:
344441
securityScheme1:
345442
type: oauth2
@@ -358,6 +455,8 @@ public void SerializeAdvancedComponentsWithReferenceAsYamlV3Works()
358455
// Act
359456
var actual = AdvancedComponentsWithReference.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0_0);
360457

458+
_output.WriteLine(actual);
459+
361460
// Assert
362461
actual = actual.MakeLineBreaksEnvironmentNeutral();
363462
expected = expected.MakeLineBreaksEnvironmentNeutral();
@@ -425,5 +524,73 @@ public void SerializeBrokenComponentsAsYamlWorks()
425524
expected = expected.MakeLineBreaksEnvironmentNeutral();
426525
actual.Should().Be(expected);
427526
}
527+
528+
[Fact(Skip = "Issue #157 We are not serializing top-level reference in components correctly")]
529+
public void SerializeTopLevelReferencingComponentsAsYamlWorks()
530+
{
531+
// Arrange
532+
var expected = @"schemas:
533+
schema1:
534+
$ref: '#/components/schemas/schema2'
535+
schema2:
536+
type: object
537+
properties:
538+
property1:
539+
type: string";
540+
541+
// Act
542+
var actual = TopLevelReferencingComponents.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0_0);
543+
544+
_output.WriteLine(actual);
545+
546+
// Assert
547+
actual = actual.MakeLineBreaksEnvironmentNeutral();
548+
expected = expected.MakeLineBreaksEnvironmentNeutral();
549+
actual.Should().Be(expected);
550+
}
551+
552+
[Fact(Skip = "Issue #157 We are not serializing top-level reference in components correctly")]
553+
public void SerializeTopLevelSelfReferencingComponentsAsYamlWorks()
554+
{
555+
// Arrange
556+
var expected = @"schemas:
557+
schema1:
558+
$ref: '#/components/schemas/schema1'";
559+
560+
// Act
561+
var actual = TopLevelSelfReferencingComponents.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0_0);
562+
563+
// Assert
564+
actual = actual.MakeLineBreaksEnvironmentNeutral();
565+
expected = expected.MakeLineBreaksEnvironmentNeutral();
566+
actual.Should().Be(expected);
567+
}
568+
569+
[Fact]
570+
public void SerializeTopLevelSelfReferencingWithOtherPropertiesComponentsAsYamlWorks()
571+
{
572+
// Arrange
573+
var expected = @"schemas:
574+
schema1:
575+
type: object
576+
properties:
577+
property1:
578+
type: string
579+
schema2:
580+
type: object
581+
properties:
582+
property1:
583+
type: string";
584+
585+
// Act
586+
var actual = TopLevelSelfReferencingComponentsWithOtherProperties.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0_0);
587+
588+
_output.WriteLine(actual);
589+
590+
// Assert
591+
actual = actual.MakeLineBreaksEnvironmentNeutral();
592+
expected = expected.MakeLineBreaksEnvironmentNeutral();
593+
actual.Should().Be(expected);
594+
}
428595
}
429596
}

0 commit comments

Comments
 (0)