Skip to content

Commit f80f958

Browse files
Merge pull request #1359 from microsoft/mk/add-default-content-type-setting
Add DefaultContentType to OpenApiReaderSettings
2 parents ad1460b + f359565 commit f80f958

File tree

7 files changed

+52
-14
lines changed

7 files changed

+52
-14
lines changed

src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public class OpenApiReaderSettings
6262
/// </summary>
6363
public Uri BaseUrl { get; set; }
6464

65+
/// <summary>
66+
/// Allows clients to define a custom DefaultContentType if produces array is empty
67+
/// </summary>
68+
public List<string> DefaultContentType { get; set; }
69+
6570
/// <summary>
6671
/// Function used to provide an alternative loader for accessing external references.
6772
/// </summary>

src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public OpenApiDocument Read(YamlDocument input, out OpenApiDiagnostic diagnostic
4747
var context = new ParsingContext(diagnostic)
4848
{
4949
ExtensionParsers = _settings.ExtensionParsers,
50-
BaseUrl = _settings.BaseUrl
50+
BaseUrl = _settings.BaseUrl,
51+
DefaultContentType = _settings.DefaultContentType
5152
};
5253

5354
OpenApiDocument document = null;

src/Microsoft.OpenApi.Readers/ParsingContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class ParsingContext
2929
internal RootNode RootNode { get; set; }
3030
internal List<OpenApiTag> Tags { get; private set; } = new List<OpenApiTag>();
3131
internal Uri BaseUrl { get; set; }
32+
internal List<string> DefaultContentType { get; set; }
3233

3334
/// <summary>
3435
/// Diagnostic object that returns metadata about the parsing process.

src/Microsoft.OpenApi.Readers/V2/OpenApiResponseDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
7474

7575
var produces = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationProduces)
7676
?? context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces)
77-
?? new List<string> { "application/octet-stream" };
77+
?? context.DefaultContentType ?? new List<string> { "application/octet-stream" };
7878

7979
var schema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema, response);
8080

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)