Obey request-body-strict in OpenAPI schema generation#3100
Obey request-body-strict in OpenAPI schema generation#3100
Conversation
Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
schemas.Add(entityName, CreateComponentSchema(entityName, fields: exposedColumnNames, metadataProvider, entities, allowAdditionalProperties: !isRequestBodyStrict));
```csharp
This schema is referenced in GET, PUT, PATCH, and POST response bodies (lines 330, 340, 341, 348, 349, 365, 376). Setting additionalProperties: true on a response schema tells consumers "expect extra unknown fields in the response." That is semantically wrong. The server controls its own responses; request-body-strict only governs what the server accepts in request bodies.
The _NoAutoPK and _NoPK schemas are only used in request bodies, so they are correct. The SP response schema on line 1020 is also correctly hardcoded to false.
Fix: keep the {EntityName} schema with allowAdditionalProperties: false, or create a separate request-body variant if the full schema is also needed for POST request bodies on non-autogenerated-PK entities (line 372 falls back to {entityName} when no auto-PK exists).
2. src/Core/Services/OpenAPI/OpenApiDocumentor.cs line 1020 (Positive)
```csharp
schemas.Add(entityName + SP_RESPONSE_SUFFIX, CreateComponentSchema(..., allowAdditionalProperties: false));Good. Response schemas should always be false. Same reasoning should be applied to the {EntityName} schema (see #1 above).
using System.Linq;Unused. Remove it.
The entity setup is identical in both test methods. Extract it into a private helper like CreateBookRuntimeEntities(). Each test would then differ only in the RestRuntimeOptions and assertions, which makes the intent easier to scan.
RestRuntimeOptions restOptions)The 3-arg overload (line 37) passes null here, but this parameter is not annotated as nullable. It should be RestRuntimeOptions? restOptions to match usage. The null check on line 68 already expects this.
/// <param name="runtimeEntities"></param>
/// <param name="configFileName"></param>
/// <param name="databaseEnvironment"></param>Either fill in descriptions or remove the empty tags. Empty tags are noise.
The comment // When isRequestBodyStrict is false, additionalProperties is set to true... appears 4 times. The method-level doc comment (lines 979-981) already explains this. The parameter name allowAdditionalProperties is self-documenting. Remove the inline repetition.
Good: removed the duplicate /// that was on CreateSpRequestComponentSchema. Nice cleanup. |
Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
Addressed all feedback in 46a4580:
|
Why make this change?
OpenAPI schemas did not reflect the
request-body-strictruntime setting. Whenfalse, clients can send extra fields (which are ignored), but the generated schema didn't communicate this—potentially misleading API consumers.What is this change?
Sets
additionalPropertieson request body schemas only based onrequest-body-strictconfig:true(default):additionalProperties: false— strict field validationfalse:additionalProperties: true— extra fields allowedAffected request body schemas:
{EntityName}_NoAutoPK,{EntityName}_NoPK{EntityName}_sp_request(stored procedures)Response schemas (
{EntityName},{EntityName}_sp_response) always haveadditionalProperties: falsesince the server controls response shape—this setting only governs what the server accepts in request bodies.Example OpenAPI output when
request-body-strict: false:{ "components": { "schemas": { "Book": { "type": "object", "properties": { ... }, "additionalProperties": false }, "Book_NoAutoPK": { "type": "object", "properties": { ... }, "additionalProperties": true }, "Book_NoPK": { "type": "object", "properties": { ... }, "additionalProperties": true } } } }How was this tested?
Added
RequestBodyStrictTests.cswith tests for both strict/non-strict modes validating:Book) always hasadditionalProperties: falseBook_NoAutoPK,Book_NoPK) respect therequest-body-strictsettingSample Request(s)
Returns OpenAPI document with
additionalProperties: trueon request body schemas when config has:{ "runtime": { "rest": { "request-body-strict": false } } }Original prompt
This section details on the original issue you should resolve
<issue_title>[Enh]: Obey "request-body-strict" in OpenAPI</issue_title>
<issue_description>## What?
Today, our OpenAPI includes object types that are not required by the endpoint. See #1838</issue_description>
<agent_instructions>include resolution for #1838 resolve both in a single pr with as little new code as possible</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@JerryNixon ### The ProblemCurrent behavior: The OpenAPI document generated by DAB includes schemas for models like
EntityName_NoAutoPK(entities without auto-generated primary keys) regardless of theruntime.rest.request-body-strictconfiguration setting.Issue #1838 screenshot shows: The OpenAPI document exposes
Book_NoAutoPKschema even though it may not be necessary depending on strictness settings.Understanding
request-body-strictThe
request-body-strictconfiguration controls whether DAB allows extraneous fields in REST request bodies:{ "runtime": { "rest": { "enabled": true, "path": "/api", "request-body-strict": true // Default: true } } }When
true(strict mode - DEFAULT):400 Bad RequesterrorWhen
false(non-strict mode):How It Works at Runtime
Looking at the request validation code:
And the validation enforcement:
The Enhancement Request
What DAB currently does in OpenAPI generation:
request-body-strict:EntityName- Full schema with all fieldsEntityName_NoAutoPK- Schema excluding auto-generated PKs (for POST)EntityName_NoPK- Schema excluding all PKs (for PUT/PATCH)What the enhancement wants:
request-body-strict: true: Include all schema variations (current behavior)request-body-strict: false: Potentially simplify the OpenAPI schema since extra fields are allowed anywayThe current OpenAPI generator doesn't check this setting:
Current Impact
When
request-body-strict: true(Default)OpenAPI shows:
Book- Full schemaBook_NoAutoPK- For POST r...💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.