Skip to content

Commit 012ed30

Browse files
committed
Fix tests
1 parent d628df8 commit 012ed30

File tree

4 files changed

+42
-117
lines changed

4 files changed

+42
-117
lines changed

test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs

Lines changed: 22 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Linq;
67
using FluentAssertions;
78
using Json.Schema;
89
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.Models.References;
11+
using Microsoft.OpenApi.Reader;
912
using Xunit;
1013

1114
namespace Microsoft.OpenApi.Readers.Tests.ReferenceService
@@ -15,23 +18,20 @@ public class TryLoadReferenceV2Tests
1518
{
1619
private const string SampleFolderPath = "ReferenceService/Samples/";
1720

21+
public TryLoadReferenceV2Tests()
22+
{
23+
OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader());
24+
}
25+
1826
[Fact]
1927
public void LoadParameterReference()
2028
{
2129
// Arrange
2230
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
23-
24-
var reference = new OpenApiReference
25-
{
26-
Type = ReferenceType.Parameter,
27-
Id = "skipParam"
28-
};
29-
30-
// Act
31-
var referencedObject = result.OpenApiDocument.ResolveReferenceTo<OpenApiParameter>(reference);
31+
var reference = new OpenApiParameterReference("skipParam", result.OpenApiDocument);
3232

3333
// Assert
34-
referencedObject.Should().BeEquivalentTo(
34+
reference.Should().BeEquivalentTo(
3535
new OpenApiParameter
3636
{
3737
Name = "skip",
@@ -40,13 +40,8 @@ public void LoadParameterReference()
4040
Required = true,
4141
Schema = new JsonSchemaBuilder()
4242
.Type(SchemaValueType.Integer)
43-
.Format("int32"),
44-
Reference = new OpenApiReference
45-
{
46-
Type = ReferenceType.Parameter,
47-
Id = "skipParam"
48-
}
49-
}
43+
.Format("int32")
44+
}, options => options.Excluding(x => x.Reference)
5045
);
5146
}
5247

@@ -55,28 +50,16 @@ public void LoadSecuritySchemeReference()
5550
{
5651
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
5752

58-
var reference = new OpenApiReference
59-
{
60-
Type = ReferenceType.SecurityScheme,
61-
Id = "api_key_sample"
62-
};
63-
64-
// Act
65-
var referencedObject = result.OpenApiDocument.ResolveReferenceTo<OpenApiSecurityScheme>(reference);
53+
var reference = new OpenApiSecuritySchemeReference("api_key_sample", result.OpenApiDocument);
6654

6755
// Assert
68-
referencedObject.Should().BeEquivalentTo(
56+
reference.Should().BeEquivalentTo(
6957
new OpenApiSecurityScheme
7058
{
7159
Type = SecuritySchemeType.ApiKey,
7260
Name = "api_key",
73-
In = ParameterLocation.Header,
74-
Reference = new()
75-
{
76-
Type = ReferenceType.SecurityScheme,
77-
Id = "api_key_sample"
78-
}
79-
}
61+
In = ParameterLocation.Header
62+
}, options => options.Excluding(x => x.Reference)
8063
);
8164
}
8265

@@ -85,50 +68,29 @@ public void LoadResponseReference()
8568
{
8669
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
8770

88-
var reference = new OpenApiReference
89-
{
90-
Type = ReferenceType.Response,
91-
Id = "NotFound"
92-
};
93-
94-
// Act
95-
var referencedObject = result.OpenApiDocument.ResolveReferenceTo<OpenApiResponse>(reference);
71+
var reference = new OpenApiResponseReference("NotFound", result.OpenApiDocument);
9672

9773
// Assert
98-
referencedObject.Should().BeEquivalentTo(
74+
reference.Should().BeEquivalentTo(
9975
new OpenApiResponse
10076
{
10177
Description = "Entity not found.",
102-
Reference = new()
103-
{
104-
Type = ReferenceType.Response,
105-
Id = "NotFound"
106-
},
10778
Content = new Dictionary<string, OpenApiMediaType>
10879
{
10980
["application/json"] = new()
11081
}
111-
}
82+
}, options => options.Excluding(x => x.Reference)
11283
);
11384
}
11485

11586
[Fact]
11687
public void LoadResponseAndSchemaReference()
11788
{
11889
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
119-
120-
121-
var reference = new OpenApiReference
122-
{
123-
Type = ReferenceType.Response,
124-
Id = "GeneralError"
125-
};
126-
127-
// Act
128-
var referencedObject = result.OpenApiDocument.ResolveReferenceTo<OpenApiResponse>(reference);
90+
var reference = new OpenApiResponseReference("GeneralError", result.OpenApiDocument);
12991

13092
// Assert
131-
referencedObject.Should().BeEquivalentTo(
93+
reference.Should().BeEquivalentTo(
13294
new OpenApiResponse
13395
{
13496
Description = "General Error",
@@ -139,13 +101,8 @@ public void LoadResponseAndSchemaReference()
139101
Schema = new JsonSchemaBuilder()
140102
.Ref("#/definitions/SampleObject2")
141103
}
142-
},
143-
Reference = new()
144-
{
145-
Type = ReferenceType.Response,
146-
Id = "GeneralError"
147104
}
148-
}
105+
}, options => options.Excluding(x => x.Reference)
149106
);
150107
}
151108
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,6 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
302302
}
303303
}
304304
}
305-
},
306-
Reference = new OpenApiReference
307-
{
308-
Type = ReferenceType.PathItem,
309-
Id = "pets",
310-
HostDocument = actual.OpenApiDocument
311305
}
312306
}
313307
};
@@ -328,7 +322,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
328322
};
329323

330324
// Assert
331-
actual.OpenApiDocument.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.Components.PathItems["pets"].Reference.HostDocument));
325+
actual.OpenApiDocument.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.Webhooks["pets"].Reference));
332326
actual.OpenApiDiagnostic.Should().BeEquivalentTo(
333327
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 });
334328
}

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed()
513513
result.OpenApiDiagnostic.Should().BeEquivalentTo(
514514
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
515515
}
516+
516517
[Fact]
517518
public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
518519
{
@@ -552,35 +553,16 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
552553
{
553554
Type = SecuritySchemeType.ApiKey,
554555
Name = "apiKeyName1",
555-
In = ParameterLocation.Header,
556-
Reference = new OpenApiReference
557-
{
558-
Id = "securitySchemeName1",
559-
Type = ReferenceType.SecurityScheme,
560-
HostDocument = actual.OpenApiDocument
561-
}
562-
556+
In = ParameterLocation.Header
563557
},
564558
["securitySchemeName2"] = new OpenApiSecurityScheme
565559
{
566560
Type = SecuritySchemeType.OpenIdConnect,
567-
OpenIdConnectUrl = new Uri("http://example.com"),
568-
Reference = new OpenApiReference
569-
{
570-
Id = "securitySchemeName2",
571-
Type = ReferenceType.SecurityScheme,
572-
HostDocument = actual.OpenApiDocument
573-
}
561+
OpenIdConnectUrl = new Uri("http://example.com")
574562
}
575563
}
576564
};
577565

578-
var petSchema = components.Schemas["pet1"];
579-
580-
var newPetSchema = components.Schemas["newPet"];
581-
582-
var errorModelSchema = components.Schemas["errorModel"];
583-
584566
var tag1 = new OpenApiTag
585567
{
586568
Name = "tagName1",
@@ -592,7 +574,6 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
592574
}
593575
};
594576

595-
596577
var tag2 = new OpenApiTag
597578
{
598579
Name = "tagName2",
@@ -921,12 +902,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
921902
new OpenApiTag
922903
{
923904
Name = "tagName1",
924-
Description = "tagDescription1",
925-
Reference = new OpenApiReference()
926-
{
927-
Id = "tagName1",
928-
Type = ReferenceType.Tag
929-
}
905+
Description = "tagDescription1"
930906
}
931907
},
932908
SecurityRequirements = new List<OpenApiSecurityRequirement>
@@ -944,8 +920,15 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
944920
}
945921
};
946922

947-
actual.OpenApiDocument.Should().BeEquivalentTo(expected, options => options.Excluding(m => m.Name == "HostDocument"));
948-
923+
actual.OpenApiDocument.Should().BeEquivalentTo(expected, options => options
924+
.Excluding(x => x.HashCode)
925+
.Excluding(m => m.Tags[0].Reference)
926+
.Excluding(x => x.Paths["/pets"].Operations[OperationType.Get].Tags[0].Reference)
927+
.Excluding(x => x.Paths["/pets"].Operations[OperationType.Get].Tags[0].Reference.HostDocument)
928+
.Excluding(x => x.Paths["/pets"].Operations[OperationType.Post].Tags[0].Reference.HostDocument)
929+
.Excluding(x => x.Paths["/pets"].Operations[OperationType.Get].Tags[1].Reference.HostDocument)
930+
.Excluding(x => x.Paths["/pets"].Operations[OperationType.Post].Tags[1].Reference.HostDocument));
931+
949932

950933
actual.OpenApiDiagnostic.Should().BeEquivalentTo(
951934
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
@@ -969,7 +952,7 @@ public void GlobalSecurityRequirementShouldReferenceSecurityScheme()
969952
var securityRequirement = result.OpenApiDocument.SecurityRequirements.First();
970953

971954
securityRequirement.Keys.First().Should().BeEquivalentTo(result.OpenApiDocument.Components.SecuritySchemes.First().Value,
972-
options => options.Excluding(x => x.Reference.HostDocument));
955+
options => options.Excluding(x => x.Reference));
973956
}
974957

975958
[Fact]
@@ -992,14 +975,10 @@ public void HeaderParameterShouldAllowExample()
992975
Example = new OpenApiAny("99391c7e-ad88-49ec-a2ad-99ddcb1f7721"),
993976
Schema = new JsonSchemaBuilder()
994977
.Type(SchemaValueType.String)
995-
.Format(Formats.Uuid),
996-
Reference = new OpenApiReference()
997-
{
998-
Type = ReferenceType.Header,
999-
Id = "example-header"
1000-
}
978+
.Format(Formats.Uuid)
1001979
}, options => options.IgnoringCyclicReferences()
1002-
.Excluding(e => e.Example.Node.Parent));
980+
.Excluding(e => e.Example.Node.Parent)
981+
.Excluding(x => x.Reference));
1003982

1004983
var examplesHeader = result.OpenApiDocument.Components?.Headers?["examples-header"];
1005984
Assert.NotNull(examplesHeader);
@@ -1028,12 +1007,7 @@ public void HeaderParameterShouldAllowExample()
10281007
},
10291008
Schema = new JsonSchemaBuilder()
10301009
.Type(SchemaValueType.String)
1031-
.Format(Formats.Uuid),
1032-
Reference = new OpenApiReference()
1033-
{
1034-
Type = ReferenceType.Header,
1035-
Id = "examples-header"
1036-
}
1010+
.Format(Formats.Uuid)
10371011
}, options => options.IgnoringCyclicReferences()
10381012
.Excluding(e => e.Examples["uuid1"].Value.Node.Parent)
10391013
.Excluding(e => e.Examples["uuid2"].Value.Node.Parent));

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void OperationWithSecurityRequirementShouldReferenceSecurityScheme()
2929
var securityScheme = result.OpenApiDocument.Paths["/"].Operations[OperationType.Get].Security.First().Keys.First();
3030

3131
securityScheme.Should().BeEquivalentTo(result.OpenApiDocument.Components.SecuritySchemes.First().Value,
32-
options => options.Excluding(x => x.Reference.HostDocument));
32+
options => options.Excluding(x => x.Reference));
3333
}
3434

3535
[Fact]

0 commit comments

Comments
 (0)