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

Sets request body required property to false when all action parameters are optional #591

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -66,15 +66,17 @@ public static OpenApiRequestBody CreateRequestBody(this ODataContext context, IE
Properties = new Dictionary<string, OpenApiSchema>()
};

bool allParamsNullable = true;
foreach (var parameter in action.Parameters.Skip(skip))
{
allParamsNullable &= parameter.Type.IsNullable;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last question here. Should we be checking if the parameter is of type IEdmOptionalParameter instead? Is it possible for required parameter to be nullable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed! We should. I will add a check for the annotation: Org.OData.Core.V1.OptionalParameter

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now let's only update the check to only cater for the annotation Org.OData.Core.V1.OptionalParameter as that's the scenario we want to cater for in #582. We can always come back and do that for nullable types.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

parametersSchema.Properties.Add(parameter.Name, context.CreateEdmTypeSchema(parameter.Type));
}

OpenApiRequestBody requestBody = new OpenApiRequestBody
OpenApiRequestBody requestBody = new()
{
Description = "Action parameters",
Required = true,
Required = !allParamsNullable,
Content = new Dictionary<string, OpenApiMediaType>()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Adds nullable to double schema conversions #581
- Updates tag names for actions/functions operations #585
- Creates unique operation ids for paths with composable overloaded functions #580
- Sets request body required property to false when all action parameters are optional #582
</PackageReleaseNotes>
<AssemblyName>Microsoft.OpenApi.OData.Reader</AssemblyName>
<AssemblyOriginatorKeyFile>..\..\tool\Microsoft.OpenApi.OData.snk</AssemblyOriginatorKeyFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public OpenApiRequestBodyGeneratorTest()
var edmAction = new EdmAction("NS", "Checkout", boolType, true, actionEntitySetPath);
edmAction.AddParameter(new EdmOperationParameter(edmAction, "bindingParameter", new EdmEntityTypeReference(customer, true)));
edmAction.AddParameter("param", EdmCoreModel.Instance.GetString(true));
edmAction.AddParameter("param2", EdmCoreModel.Instance.GetString(false));

model.AddElement(edmAction);

var actionImportEntitySetPath = new EdmPathExpression("Param1/Nav2");
Expand Down Expand Up @@ -89,9 +91,17 @@ public void CreateRequestBodyForActionImportReturnCorrectRequestBody()
var schema = content.Value.Schema;
Assert.Equal("object", schema.Type);
Assert.NotNull(schema.Properties);
var parameter = Assert.Single(schema.Properties);
Assert.Equal("param", parameter.Key);
Assert.Equal("string", parameter.Value.Type);

var parameters = schema.Properties;
Assert.Equal(2, parameters.Count);

var parameter1 = parameters.First(p => p.Key == "param");
Assert.Equal("param", parameter1.Key);
Assert.Equal("string", parameter1.Value.Type);

var parameter2 = parameters.First(p => p.Key == "param2");
Assert.Equal("param2", parameter2.Key);
Assert.Equal("string", parameter2.Value.Type);
}

[Fact]
Expand All @@ -116,6 +126,9 @@ public void CanSerializeAsJsonFromTheCreatedRequestBody()
""param"": {
""type"": ""string"",
""nullable"": true
},
""param2"": {
""type"": ""string""
}
}
}
Expand Down Expand Up @@ -146,9 +159,17 @@ public void CreateRequestBodyForActionReturnCorrectRequestBody()
var schema = content.Value.Schema;
Assert.Equal("object", schema.Type);
Assert.NotNull(schema.Properties);
var parameter = Assert.Single(schema.Properties);
Assert.Equal("param", parameter.Key);
Assert.Equal("string", parameter.Value.Type);

var parameters = schema.Properties;
Assert.Equal(2, parameters.Count);

var parameter1 = parameters.First(p => p.Key == "param");
Assert.Equal("param", parameter1.Key);
Assert.Equal("string", parameter1.Value.Type);

var parameter2 = parameters.First(p => p.Key == "param2");
Assert.Equal("param2", parameter2.Key);
Assert.Equal("string", parameter2.Value.Type);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10943,7 +10943,6 @@
"in": "body",
"name": "body",
"description": "Action parameters",
"required": true,
"schema": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -15768,7 +15767,6 @@
"in": "body",
"name": "body",
"description": "Action parameters",
"required": true,
"schema": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -28333,7 +28331,6 @@
"in": "body",
"name": "body",
"description": "Action parameters",
"required": true,
"schema": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7362,7 +7362,6 @@ paths:
- in: body
name: body
description: Action parameters
required: true
schema:
type: object
properties:
Expand Down Expand Up @@ -10613,7 +10612,6 @@ paths:
- in: body
name: body
description: Action parameters
required: true
schema:
type: object
properties:
Expand Down Expand Up @@ -19205,7 +19203,6 @@ paths:
- in: body
name: body
description: Action parameters
required: true
schema:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12080,8 +12080,7 @@
}
}
}
},
"required": true
}
},
"responses": {
"204": {
Expand Down Expand Up @@ -17516,8 +17515,7 @@
}
}
}
},
"required": true
}
},
"responses": {
"204": {
Expand Down Expand Up @@ -31640,8 +31638,7 @@
}
}
}
},
"required": true
}
},
"responses": {
"204": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8095,7 +8095,6 @@ paths:
- $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
- type: object
nullable: true
required: true
responses:
'204':
description: Success
Expand Down Expand Up @@ -11734,7 +11733,6 @@ paths:
- $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
- type: object
nullable: true
required: true
responses:
'204':
description: Success
Expand Down Expand Up @@ -21275,7 +21273,6 @@ paths:
- $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person'
- type: object
nullable: true
required: true
responses:
'204':
description: Success
Expand Down
Loading