{"record":{"id":"1e64f7fcc90359a5","repo":"RicoSuter/NSwag","slug":"multiple-body-parameters-found-in-operation-operation","errorCode":null,"errorMessage":"Multiple body parameters found in operation '{_operation.OperationId}'.","messagePattern":"Multiple body parameters found in operation '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.CodeGeneration/Models/OperationModelBase.cs","lineNumber":194,"sourceCode":"        /// <summary>Gets a value indicating whether the the request has a body.</summary>\n        public bool HasBody => HasContent || HasFormParameters;\n\n        /// <summary>Gets the content parameter.</summary>\n        /// <exception cref=\"InvalidOperationException\" accessor=\"get\">Multiple body parameters found in operation.</exception>\n        public TParameterModel ContentParameter\n        {\n            get\n            {\n                TParameterModel parameter = null;\n                var parameters = Parameters;\n                for (var i = 0; i < parameters.Count; i++)\n                {\n                    var p = parameters[i];\n                    if (p.Kind == OpenApiParameterKind.Body)\n                    {\n                        if (parameter != null)\n                        {\n                            throw new InvalidOperationException($\"Multiple body parameters found in operation '{_operation.OperationId}'.\");\n                        }\n\n                        parameter = p;\n                    }\n                }\n\n                return parameter;\n            }\n        }\n\n        /// <summary>Gets the path parameters.</summary>\n        public IEnumerable<TParameterModel> PathParameters => Parameters.Where(static p => p.Kind == OpenApiParameterKind.Path);\n\n        /// <summary>Gets the query parameters.</summary>\n        public IEnumerable<TParameterModel> QueryParameters => Parameters.Where(static p => p.Kind is OpenApiParameterKind.Query or OpenApiParameterKind.ModelBinding);\n\n        /// <summary>Gets a value indicating whether the operation has query parameters.</summary>\n        public bool HasQueryParameters => QueryParameters.Any();","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.CodeGeneration/Models/OperationModelBase.cs#L176-L212","documentation":"OperationModelBase expects at most one OpenAPI body parameter per operation (per the Swagger/OpenAPI spec). While building the code-generation model, if a second body parameter is found it throws InvalidOperationException naming the operation.","triggerScenarios":"An operation's document/spec contains two parameters with Kind == Body, e.g. a handwritten JSON spec or NSwagDocument with two body parameters, or document generation misconfiguring an operation before code generation.","commonSituations":"Hand-edited swagger.json; older/post-processed specs; migrating from WCF/Swagger 1.2 definitions that allowed multiple body parts; code generators that added a body parameter alongside an existing [FromBody].","solutions":["Edit the OpenAPI/Swagger document so the operation has exactly one body parameter; merge the extras into the request schema.","Fix the controller action: only one [FromBody] parameter per action (combine into a single request model).","Regenerate the document instead of hand-editing the spec to keep it valid."],"exampleFix":"// before\npublic IActionResult Save([FromBody] AddressDto address, [FromBody] ContactDto contact)\n// after\npublic IActionResult Save([FromBody] SaveRequest request) // SaveRequest { AddressDto Address; ContactDto Contact; }","handlingStrategy":"validation","validationCode":"foreach (var path in document.Paths)\n  foreach (var op in path.Value.Values)\n    if (op.Parameters.Count(p => p.Kind == NJsonSchema.OpenApiParameterKind.Body) > 1)\n      throw new InvalidOperationException($\"Operation '{op.OperationId}' has multiple body parameters\");","typeGuard":"bool HasSingleBodyParameter(SwaggerOperationDescription op) => op.Parameters.Count(p => p.Kind == OpenApiParameterKind.Body) <= 1;","tryCatchPattern":"try { var code = generator.Generate(document); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Multiple body parameters\")) { logger.LogError(ex, \"Spec has an operation with more than one body parameter\"); throw; }","preventionTips":["Only one [FromBody] parameter per ASP.NET Core action; combine payloads into a single request model.","Validate handwritten swagger.json against the OpenAPI schema before code generation.","Generate documents via NSwag rather than editing specs by hand."],"tags":["code-generation","openapi-spec","body-parameter","nswag"],"backgroundTag":"schema-validation-failed","analyzedSha":"63daf8fcc3a25151b62eb4b326a1e8ea048a0d41","analyzedAt":"2026-09-14T11:38:15.205Z","contentChangedAt":"2026-09-14T11:38:15.205Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}