Skip to content

Commit f359565

Browse files
committed
Add test to validate
1 parent 0720231 commit f359565

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
<EmbeddedResource Include="V2Tests\Samples\basic.v3.yaml">
3737
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
3838
</EmbeddedResource>
39-
<EmbeddedResource Include="V2Tests\Samples\ComponentRootReference.json">
39+
<EmbeddedResource Include="V2Tests\Samples\docWithEmptyProduces.yaml">
40+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
41+
</EmbeddedResource>
42+
<EmbeddedResource Include="V2Tests\Samples\ComponentRootReference.json">
4043
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4144
</EmbeddedResource>
4245
<EmbeddedResource Include="V2Tests\Samples\minimal.v2.yaml">

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,27 @@ public void ShouldAssignSchemaToAllResponses()
409409
[Fact]
410410
public void ShouldAllowComponentsThatJustContainAReference()
411411
{
412-
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "ComponentRootReference.json")))
412+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "ComponentRootReference.json"));
413+
OpenApiStreamReader reader = new OpenApiStreamReader();
414+
OpenApiDocument doc = reader.Read(stream, out OpenApiDiagnostic diags);
415+
OpenApiSchema schema1 = doc.Components.Schemas["AllPets"];
416+
Assert.False(schema1.UnresolvedReference);
417+
OpenApiSchema schema2 = doc.ResolveReferenceTo<OpenApiSchema>(schema1.Reference);
418+
if (schema2.UnresolvedReference && schema1.Reference.Id == schema2.Reference.Id)
413419
{
414-
OpenApiStreamReader reader = new OpenApiStreamReader();
415-
OpenApiDocument doc = reader.Read(stream, out OpenApiDiagnostic diags);
416-
OpenApiSchema schema1 = doc.Components.Schemas["AllPets"];
417-
Assert.False(schema1.UnresolvedReference);
418-
OpenApiSchema schema2 = doc.ResolveReferenceTo<OpenApiSchema>(schema1.Reference);
419-
if (schema2.UnresolvedReference && schema1.Reference.Id == schema2.Reference.Id)
420-
{
421-
// detected a cycle - this code gets triggered
422-
Assert.Fail("A cycle should not be detected");
423-
}
420+
// detected a cycle - this code gets triggered
421+
Assert.Fail("A cycle should not be detected");
424422
}
425423
}
424+
425+
[Fact]
426+
public void ParseDocumentWithDefaultContentTypeSettingShouldSucceed()
427+
{
428+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "docWithEmptyProduces.yaml"));
429+
var doc = new OpenApiStreamReader(new OpenApiReaderSettings { DefaultContentType = new List<string> { "application/json" } })
430+
.Read(stream, out OpenApiDiagnostic diags);
431+
var mediaType = doc.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content;
432+
Assert.Contains("application/json", mediaType);
433+
}
426434
}
427435
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
swagger: '2.0'
2+
info:
3+
title: Sample API
4+
version: 1.0.0
5+
paths:
6+
/example:
7+
get:
8+
summary: Get Example
9+
description: Retrieves an example resource.
10+
produces: []
11+
responses:
12+
200:
13+
description: Successful response
14+
schema:
15+
format: binary,
16+
description: The content of the file.,
17+
type: string,
18+
x-ms-summary: File Content
19+
components: {}
20+

0 commit comments

Comments
 (0)