Skip to content

Commit 919efb6

Browse files
Ref PUT to return status code 204 and an empty body
1 parent b23764a commit 919efb6

24 files changed

+138
-57
lines changed

src/Microsoft.OpenApi.OData.Reader/Common/Constants.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,30 @@ internal static class Constants
110110
/// </summary>
111111
public static string ReferenceUpdateSchemaName = "ReferenceUpdateSchema";
112112

113+
114+
/// <summary>
115+
/// Name used for reference update.
116+
/// </summary>
117+
public static string ReferencePostSchemaName = "ReferencePostSchema";
118+
113119
/// <summary>
114-
/// Name used for reference request POST body
120+
/// Name used for reference request POST body.
115121
/// </summary>
116122
public static string ReferencePostRequestBodyName = "refPostBody";
117123

118124
/// <summary>
119-
/// Name used for reference request PUT body
125+
/// Name used for reference request PUT body.
120126
/// </summary>
121127
public static string ReferencePutRequestBodyName = "refPutBody";
122128

123129
/// <summary>
124130
/// The odata type name.
125131
/// </summary>
126132
public static string OdataType = "@odata.type";
133+
134+
/// <summary>
135+
/// The odata id.
136+
/// </summary>
137+
public static string OdataId = "@odata.id";
127138
}
128139
}

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiRequestBodyGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private static OpenApiRequestBody CreateRefPostRequestBody()
120120
Reference = new OpenApiReference
121121
{
122122
Type = ReferenceType.Schema,
123-
Id = Constants.ReferencePostRequestBodyName
123+
Id = Constants.ReferencePostSchemaName
124124
}
125125
};
126126
return new OpenApiRequestBody

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,24 @@ public static IDictionary<string, OpenApiSchema> CreateSchemas(this ODataContext
9696
if(context.HasAnyNonContainedCollections())
9797
{
9898
schemas[$"String{Constants.CollectionSchemaSuffix}"] = CreateCollectionSchema(context, new OpenApiSchema { Type = "string" }, "string");
99-
schemas[Constants.ReferenceUpdateSchemaName] = new()
100-
{
101-
Type = "object",
102-
Properties = new Dictionary<string, OpenApiSchema>
99+
}
100+
101+
schemas[Constants.ReferenceUpdateSchemaName] = new()
102+
{
103+
Type = "object",
104+
Properties = new Dictionary<string, OpenApiSchema>
103105
{
104-
{"@odata.id", new OpenApiSchema { Type = "string", Nullable = false }},
106+
{Constants.OdataId, new OpenApiSchema { Type = "string", Nullable = false }},
105107
{Constants.OdataType, new OpenApiSchema { Type = "string", Nullable = true }},
106108
}
107-
};
108-
}
109+
};
109110

110-
schemas[Constants.ReferencePostRequestBodyName] = new()
111+
schemas[Constants.ReferencePostSchemaName] = new()
111112
{
112113
Type = "object",
113114
Properties = new Dictionary<string, OpenApiSchema>
114115
{
115-
{"@odata.id", new OpenApiSchema { Type = "string", Nullable = false }}
116+
{Constants.OdataId, new OpenApiSchema { Type = "string", Nullable = false }}
116117
},
117118
AdditionalProperties = new OpenApiSchema { Type = "object" }
118119
};

src/Microsoft.OpenApi.OData.Reader/Operation/RefPutOperationHandler.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ protected override void SetRequestBody(OpenApiOperation operation)
5454
/// <inheritdoc/>
5555
protected override void SetResponses(OpenApiOperation operation)
5656
{
57-
operation.AddErrorResponses(Context.Settings, true, GetRefUpdateSchema());
57+
operation.Responses = new OpenApiResponses
58+
{
59+
{
60+
Constants.StatusCode204,
61+
new OpenApiResponse { Description = "Success" }
62+
}
63+
};
64+
65+
operation.AddErrorResponses(Context.Settings, false);
5866
base.SetResponses(operation);
5967
}
6068

@@ -85,18 +93,5 @@ protected override void AppendCustomParameters(OpenApiOperation operation)
8593
AppendCustomParameters(operation, Restriction.UpdateRestrictions.CustomQueryOptions, ParameterLocation.Query);
8694
}
8795
}
88-
89-
private OpenApiSchema GetRefUpdateSchema()
90-
{
91-
return new()
92-
{
93-
UnresolvedReference = true,
94-
Reference = new OpenApiReference
95-
{
96-
Id = Constants.ReferenceUpdateSchemaName,
97-
Type = ReferenceType.Schema
98-
},
99-
};
100-
}
10196
}
10297
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiRequestBodyGeneratorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void CreateRefRequestBodies()
164164
// Assert
165165
Assert.NotNull(refPostBody);
166166
Assert.Equal("New navigation property ref value", refPostBody.Description);
167-
Assert.Equal(Common.Constants.ReferencePostRequestBodyName, refPostBody.Content.First().Value.Schema.Reference.Id);
167+
Assert.Equal(Common.Constants.ReferencePostSchemaName, refPostBody.Content.First().Value.Schema.Reference.Id);
168168
}
169169
}
170170
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ public void CreatesRefRequestBodySchema()
7474
// Act & Assert
7575
var schemas = context.CreateSchemas();
7676

77-
schemas.TryGetValue(Constants.ReferencePostRequestBodyName, out OpenApiSchema refRequestBody);
77+
schemas.TryGetValue(Constants.ReferencePostSchemaName, out OpenApiSchema refRequestBody);
7878

7979
Assert.NotNull(refRequestBody);
8080
Assert.Equal("object", refRequestBody.Type);
81-
Assert.Equal("@odata.id", refRequestBody.Properties.First().Key);
81+
Assert.Equal(Constants.OdataId, refRequestBody.Properties.First().Key);
8282
Assert.Equal("string", refRequestBody.Properties.First().Value.Type);
8383
Assert.Equal("object", refRequestBody.AdditionalProperties.Type);
8484
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/RefPostOperationHandlerTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public void CreateNavigationRefPostOperationReturnsCorrectOperation(bool enableO
5959
Assert.Equal(Common.Constants.ReferencePostRequestBodyName, operation.RequestBody.Reference.Id);
6060

6161
Assert.Equal(2, operation.Responses.Count);
62-
var statusCode = useHTTPStatusCodeClass2XX ? "2XX" : "204";
63-
Assert.Equal(new string[] { statusCode, "default" }, operation.Responses.Select(e => e.Key));
62+
Assert.Equal(new string[] { "204", "default" }, operation.Responses.Select(e => e.Key));
6463

6564
if (enableOperationId)
6665
{

test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/RefPutOperationHandlerTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public void CreateNavigationRefPutOperationReturnsCorrectOperation(bool enableOp
5858
Assert.Equal(Common.Constants.ReferencePutRequestBodyName, operation.RequestBody.Reference.Id);
5959

6060
Assert.Equal(2, operation.Responses.Count);
61-
var statusCode = useHTTPStatusCodeClass2XX ? "2XX" : "204";
62-
Assert.Equal(new string[] { statusCode, "default" }, operation.Responses.Select(e => e.Key));
61+
Assert.Equal(new string[] { "204", "default" }, operation.Responses.Select(e => e.Key));
6362

6463
if (enableOperationId)
6564
{

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,18 @@
10911091
}
10921092
}
10931093
},
1094-
"refPostBody": {
1094+
"ReferenceUpdateSchema": {
1095+
"type": "object",
1096+
"properties": {
1097+
"@odata.id": {
1098+
"type": "string"
1099+
},
1100+
"@odata.type": {
1101+
"type": "string"
1102+
}
1103+
}
1104+
},
1105+
"ReferencePostSchema": {
10951106
"type": "object",
10961107
"properties": {
10971108
"@odata.id": {

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,14 @@ definitions:
722722
type: array
723723
items:
724724
$ref: '#/definitions/DefaultNs.Address'
725-
refPostBody:
725+
ReferenceUpdateSchema:
726+
type: object
727+
properties:
728+
'@odata.id':
729+
type: string
730+
'@odata.type':
731+
type: string
732+
ReferencePostSchema:
726733
type: object
727734
properties:
728735
'@odata.id':

0 commit comments

Comments
 (0)