Skip to content

Commit

Permalink
Merge pull request #1359 from microsoft/mk/add-default-content-type-s…
Browse files Browse the repository at this point in the history
…etting

Add DefaultContentType to OpenApiReaderSettings
  • Loading branch information
MaggieKimani1 authored Sep 26, 2023
2 parents ad1460b + f359565 commit f80f958
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public class OpenApiReaderSettings
/// </summary>
public Uri BaseUrl { get; set; }

/// <summary>
/// Allows clients to define a custom DefaultContentType if produces array is empty
/// </summary>
public List<string> DefaultContentType { get; set; }

/// <summary>
/// Function used to provide an alternative loader for accessing external references.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public OpenApiDocument Read(YamlDocument input, out OpenApiDiagnostic diagnostic
var context = new ParsingContext(diagnostic)
{
ExtensionParsers = _settings.ExtensionParsers,
BaseUrl = _settings.BaseUrl
BaseUrl = _settings.BaseUrl,
DefaultContentType = _settings.DefaultContentType
};

OpenApiDocument document = null;
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.OpenApi.Readers/ParsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ParsingContext
internal RootNode RootNode { get; set; }
internal List<OpenApiTag> Tags { get; private set; } = new List<OpenApiTag>();
internal Uri BaseUrl { get; set; }
internal List<string> DefaultContentType { get; set; }

/// <summary>
/// Diagnostic object that returns metadata about the parsing process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
<EmbeddedResource Include="V2Tests\Samples\basic.v3.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V2Tests\Samples\ComponentRootReference.json">
<EmbeddedResource Include="V2Tests\Samples\docWithEmptyProduces.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V2Tests\Samples\ComponentRootReference.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V2Tests\Samples\minimal.v2.yaml">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,27 @@ public void ShouldAssignSchemaToAllResponses()
[Fact]
public void ShouldAllowComponentsThatJustContainAReference()
{
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "ComponentRootReference.json")))
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "ComponentRootReference.json"));
OpenApiStreamReader reader = new OpenApiStreamReader();
OpenApiDocument doc = reader.Read(stream, out OpenApiDiagnostic diags);
OpenApiSchema schema1 = doc.Components.Schemas["AllPets"];
Assert.False(schema1.UnresolvedReference);
OpenApiSchema schema2 = doc.ResolveReferenceTo<OpenApiSchema>(schema1.Reference);
if (schema2.UnresolvedReference && schema1.Reference.Id == schema2.Reference.Id)
{
OpenApiStreamReader reader = new OpenApiStreamReader();
OpenApiDocument doc = reader.Read(stream, out OpenApiDiagnostic diags);
OpenApiSchema schema1 = doc.Components.Schemas["AllPets"];
Assert.False(schema1.UnresolvedReference);
OpenApiSchema schema2 = doc.ResolveReferenceTo<OpenApiSchema>(schema1.Reference);
if (schema2.UnresolvedReference && schema1.Reference.Id == schema2.Reference.Id)
{
// detected a cycle - this code gets triggered
Assert.Fail("A cycle should not be detected");
}
// detected a cycle - this code gets triggered
Assert.Fail("A cycle should not be detected");
}
}

[Fact]
public void ParseDocumentWithDefaultContentTypeSettingShouldSucceed()
{
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "docWithEmptyProduces.yaml"));
var doc = new OpenApiStreamReader(new OpenApiReaderSettings { DefaultContentType = new List<string> { "application/json" } })
.Read(stream, out OpenApiDiagnostic diags);
var mediaType = doc.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content;
Assert.Contains("application/json", mediaType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
swagger: '2.0'
info:
title: Sample API
version: 1.0.0
paths:
/example:
get:
summary: Get Example
description: Retrieves an example resource.
produces: []
responses:
200:
description: Successful response
schema:
format: binary,
description: The content of the file.,
type: string,
x-ms-summary: File Content
components: {}

0 comments on commit f80f958

Please sign in to comment.