Skip to content

Commit 162b004

Browse files
Add request body content to API operations (#227)
Added requestBody to Swashbuckle generated specification.
1 parent 09ce5e8 commit 162b004

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/CommunityToolkit.Datasync.Server.Swashbuckle/DatasyncDocumentFilter.cs

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ internal void ProcessController(Type entityType, string routePath, OpenApiDocume
112112
case OpType.Create:
113113
// Request Edits
114114
operation.Value.AddConditionalHeader(true);
115+
operation.Value.AddRequestWithContent(context.SchemaRepository.Schemas[entityType.Name]);
115116

116117
// Response Edits
117118
operation.Value.AddResponseWithContent("201", "Created", context.SchemaRepository.Schemas[entityType.Name]);
@@ -152,6 +153,7 @@ internal void ProcessController(Type entityType, string routePath, OpenApiDocume
152153
case OpType.Replace:
153154
// Request Edits
154155
operation.Value.AddConditionalHeader();
156+
operation.Value.AddRequestWithContent(context.SchemaRepository.Schemas[entityType.Name]);
155157

156158
// Response Edits
157159
operation.Value.AddResponseWithContent("200", "OK", context.SchemaRepository.Schemas[entityType.Name]);

src/CommunityToolkit.Datasync.Server.Swashbuckle/DatasyncOperationExtensions.cs

+18
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ internal static void AddResponseWithContent(this OpenApiOperation operation, str
9595
});
9696
operation.Responses[statusCode] = response;
9797
}
98+
/// <summary>
99+
/// Adds the content type and schema to the request body.
100+
/// </summary>
101+
/// <param name="operation">The <see cref="OpenApiOperation"/> to modify.</param>
102+
/// <param name="schema">The schema of the entity in the request.</param>
103+
internal static void AddRequestWithContent(this OpenApiOperation operation, OpenApiSchema schema)
104+
{
105+
operation.RequestBody = new OpenApiRequestBody
106+
{
107+
Content = new Dictionary<string, OpenApiMediaType>
108+
{
109+
[JsonMediaType] = new OpenApiMediaType()
110+
{
111+
Schema = schema
112+
}
113+
}
114+
};
115+
}
98116

99117
/// <summary>
100118
/// Adds or replaces the 409/412 Conflict/Precondition Failed response.

tests/CommunityToolkit.Datasync.Server.Swashbuckle.Test/swagger.json

+36
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@
159159
}
160160
}
161161
],
162+
"requestBody": {
163+
"content": {
164+
"application/json": {
165+
"schema": {
166+
"$ref": "#/components/schemas/KitchenSink"
167+
}
168+
}
169+
}
170+
},
162171
"responses": {
163172
"201": {
164173
"description": "Created",
@@ -448,6 +457,15 @@
448457
}
449458
}
450459
],
460+
"requestBody": {
461+
"content": {
462+
"application/json": {
463+
"schema": {
464+
"$ref": "#/components/schemas/KitchenSink"
465+
}
466+
}
467+
}
468+
},
451469
"responses": {
452470
"200": {
453471
"description": "OK",
@@ -530,6 +548,15 @@
530548
}
531549
}
532550
],
551+
"requestBody": {
552+
"content": {
553+
"application/json": {
554+
"schema": {
555+
"$ref": "#/components/schemas/TodoItem"
556+
}
557+
}
558+
}
559+
},
533560
"responses": {
534561
"201": {
535562
"description": "Created",
@@ -819,6 +846,15 @@
819846
}
820847
}
821848
],
849+
"requestBody": {
850+
"content": {
851+
"application/json": {
852+
"schema": {
853+
"$ref": "#/components/schemas/TodoItem"
854+
}
855+
}
856+
}
857+
},
822858
"responses": {
823859
"200": {
824860
"description": "OK",

0 commit comments

Comments
 (0)