Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing response schema bug #1316

Merged
merged 19 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1349182
Create a media type object if produces is empty but a response schema…
MaggieKimani1 Aug 21, 2023
a53defa
Add a unit test for validation
MaggieKimani1 Aug 21, 2023
e9d3660
Default to application/octet-stream as the MIME type if produces is a…
MaggieKimani1 Aug 22, 2023
2ba6f70
Revert change; add a default value for produces when its null; remove…
MaggieKimani1 Aug 22, 2023
3636351
Update test
MaggieKimani1 Aug 22, 2023
6a7993e
Clean up test
MaggieKimani1 Aug 22, 2023
66c3e5c
Add test to verify request body schema is set when consumes is an emp…
MaggieKimani1 Aug 22, 2023
11d9f68
Revert previous fix
MaggieKimani1 Aug 22, 2023
5f90def
Use "application/octet-stream" as default MIME type if produces is an…
MaggieKimani1 Aug 22, 2023
9b91130
Combine two checks using TryGetValue() for efficiency
MaggieKimani1 Aug 22, 2023
589944d
Merge remote-tracking branch 'origin/vnext' into mk/integrate-json-sc…
MaggieKimani1 Aug 30, 2023
dc319ec
Only set produces if it contains a value
MaggieKimani1 Aug 30, 2023
845f314
Refactor and clean up code
MaggieKimani1 Aug 30, 2023
4e81bf9
Maintain ordering of properties as specified in fixedFieldMap by doin…
MaggieKimani1 Aug 30, 2023
ab9648c
Remove unused private method
MaggieKimani1 Aug 30, 2023
e666d3d
Add null conditional operator
MaggieKimani1 Aug 31, 2023
966b459
Use Assert.Fail() to fail the test
MaggieKimani1 Aug 31, 2023
a83f01a
Use static keyword; fix sonarcloud bug
MaggieKimani1 Aug 31, 2023
201e90e
Update langVersion to latest
MaggieKimani1 Aug 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal static partial class OpenApiV2Deserializer
{
"produces", (o, n) => {
var produces = n.CreateSimpleList(s => s.GetScalarValue());
if (produces != null) {
if (produces.Count > 0) {
n.Context.SetTempStorage(TempStorageKeys.OperationProduces, produces);
}
}
Expand Down
47 changes: 17 additions & 30 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiResponseDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,47 +73,34 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
}

var produces = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationProduces)
baywet marked this conversation as resolved.
Show resolved Hide resolved
?? context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces);
?? new List<string> { "application/octet-stream" };

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

foreach (var produce in produces)
{
var schema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema, response);

if (produces.Count == 0 && schema != null)
if (response.Content.ContainsKey(produce) && response.Content[produce] != null)
Fixed Show fixed Hide fixed
MaggieKimani1 marked this conversation as resolved.
Show resolved Hide resolved
{
var mediaType = new OpenApiMediaType
if (schema != null)
{
Schema = schema
};

response.Content.Add(string.Empty, mediaType);
response.Content[produce].Schema = schema;
ProcessAnyFields(mapNode, response.Content[produce], _mediaTypeAnyFields);
}
}

foreach (var produce in produces)
else
{

if (response.Content.ContainsKey(produce) && response.Content[produce] != null)
{
if (schema != null)
{
response.Content[produce].Schema = schema;
ProcessAnyFields(mapNode, response.Content[produce], _mediaTypeAnyFields);
}
}
else
var mediaType = new OpenApiMediaType
{
var mediaType = new OpenApiMediaType
{
Schema = schema
};
Schema = schema
};

response.Content.Add(produce, mediaType);
}
response.Content.Add(produce, mediaType);
}

context.SetTempStorage(TempStorageKeys.ResponseSchema, null, response);
context.SetTempStorage(TempStorageKeys.ResponseProducesSet, true, response);
}

context.SetTempStorage(TempStorageKeys.ResponseSchema, null, response);
context.SetTempStorage(TempStorageKeys.ResponseProducesSet, true, response);
}

private static void LoadExamples(OpenApiResponse response, ParseNode node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<EmbeddedResource Include="V2Tests\Samples\OpenApiOperation\operationWithBody.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V2Tests\Samples\OpenApiOperation\operationWithBodyAndEmptyConsumes.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V2Tests\Samples\OpenApiOperation\operationWithFormData.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ public class OpenApiOperationTests
}
}
},
Extensions = {
[OpenApiConstants.BodyName] = new OpenApiString("petObject")
Extensions = {
[OpenApiConstants.BodyName] = new OpenApiString("petObject")
}
},
Responses = new OpenApiResponses
Expand Down Expand Up @@ -381,10 +381,8 @@ public void ParseOperationWithEmptyProducesArraySetsResponseSchemaIfExists()
{
// Arrange
MapNode node;
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "operationWithEmptyProducesArrayInResponse.json")))
{
node = TestHelper.CreateYamlMapNode(stream);
}
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "operationWithEmptyProducesArrayInResponse.json"));
node = TestHelper.CreateYamlMapNode(stream);

// Act
var operation = OpenApiV2Deserializer.LoadOperation(node);
Expand All @@ -400,7 +398,7 @@ public void ParseOperationWithEmptyProducesArraySetsResponseSchemaIfExists()
Description = "OK",
Content =
{
[""] = new OpenApiMediaType()
["application/octet-stream"] = new OpenApiMediaType()
{
Schema = new OpenApiSchema()
{
Expand All @@ -419,5 +417,20 @@ public void ParseOperationWithEmptyProducesArraySetsResponseSchemaIfExists()
}
);
}

[Fact]
public void ParseOperationWithBodyAndEmptyConsumesSetsRequestBodySchemaIfExists()
{
// Arrange
MapNode node;
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "operationWithBodyAndEmptyConsumes.yaml"));
node = TestHelper.CreateYamlMapNode(stream);

// Act
var operation = OpenApiV2Deserializer.LoadOperation(node);

// Assert
operation.Should().BeEquivalentTo(_operationWithBody);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Modified from https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operation-object-example
summary: Updates a pet in the store with request body
description: ""
operationId: updatePetWithBody
consumes: []
produces:
- application/json
- application/xml
parameters:
- name: petId
in: path
description: ID of pet that needs to be updated
required: true
type: string
- name: petObject
in: body
description: Pet to update with
required: true
schema:
type: object
responses:
'200':
description: Pet updated.
'405':
description: Invalid input