{"record":{"id":"2a9ccfa97f65e68d","repo":"RicoSuter/NSwag","slug":"multiple-response-tags-with-code-responsecode-found-in-xml","errorCode":null,"errorMessage":"Multiple response tags with code '{responseCode}' found in XML documentation for method '{methodInfo.Name}'.","messagePattern":"Multiple response tags with code '(.+?)' found in XML documentation for method '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.Generation/Processors/OperationResponseProcessorBase.cs","lineNumber":100,"sourceCode":"                    }\n                }\n            }\n        }\n\n        /// <summary>Gets the XML documentation element for the given response code or null.</summary>\n        /// <param name=\"methodInfo\">The method info.</param>\n        /// <param name=\"responseCode\">The response code.</param>\n        /// <returns>The XML element or null.</returns>\n        protected XElement GetResponseXmlDocsElement(MethodInfo methodInfo, string responseCode)\n        {\n            var operationXmlDocsNodes = GetResponseXmlDocsNodes(methodInfo);\n            try\n            {\n                return operationXmlDocsNodes?.SingleOrDefault(n => n.Name == \"response\" && n.Attributes().Any(a => a.Name == \"code\" && a.Value == responseCode));\n            }\n            catch (InvalidOperationException ex)\n            {\n                throw new InvalidOperationException($\"Multiple response tags with code '{responseCode}' found in XML documentation for method '{methodInfo.Name}'.\", ex);\n            }\n        }\n\n        private IEnumerable<XElement> GetResponseXmlDocsNodes(MethodInfo methodInfo)\n        {\n            var operationXmlDocs = methodInfo?.GetXmlDocsElement(_settings.SchemaSettings.GetXmlDocsOptions());\n            return operationXmlDocs?.Nodes()?.OfType<XElement>();\n        }\n\n        private List<OperationResponseDescription> GetOperationResponseDescriptions(IEnumerable<Attribute> responseTypeAttributes, string successResponseDescription)\n        {\n            List<OperationResponseDescription> operationResponseDescriptions = [];\n            foreach (var attribute in responseTypeAttributes)\n            {\n                dynamic responseTypeAttribute = attribute;\n                var attributeType = attribute.GetType();\n\n                var isProducesAttributeWithNoType = // ignore ProducesAttribute if it has no type, https://github.com/RicoSuter/NSwag/issues/1201","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Generation/Processors/OperationResponseProcessorBase.cs#L82-L118","documentation":"GetResponseXmlDocsElement uses SingleOrDefault to find the <response code='...'> XML docs node for a given response code; if the method's XML documentation contains more than one response tag with the same code, SingleOrDefault throws InvalidOperationException, which is rethrown with a descriptive message naming the code and method. XML docs require exactly one response element per code.","triggerScenarios":"Documenting a controller action with two /// <response code=\"200\">...</response> tags that share the same code, then running NSwag generation with XML docs enabled.","commonSituations":"Copy-pasted XML doc comments where a developer duplicated a response block and forgot to change the code attribute, e.g. two '200' response tags in ASP.NET Core controllers with GenerateXmlDocumentation enabled.","solutions":["Remove or merge duplicate <response> tags with the same code in the XML doc comments","Give each response tag a unique code attribute (200, 404, etc.)","Rebuild the project so the XML documentation file is regenerated"],"exampleFix":"// before\n/// <response code=\"200\">Success</response>\n/// <response code=\"200\">Ok</response>\n// after\n/// <response code=\"200\">Success</response>\n/// <response code=\"404\">Not found</response>","handlingStrategy":"validation","validationCode":"var codes = methodDoc.Descendants(\"response\").Select(r => (string)r.Attribute(\"code\"));\nif (codes.GroupBy(c => c).Any(g => g.Count() > 1)) throw new InvalidOperationException(\"Duplicate response codes in XML docs\");","typeGuard":null,"tryCatchPattern":"try { GenerateWithXmlDocs(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Multiple response tags\")) { /* fix XML doc comments */ }","preventionTips":["Audit controller XML comments for duplicated <response code=\"...\"> tags","Ensure each HTTP status code appears exactly once per action","Rebuild after editing XML docs so the generated XML file is fresh"],"tags":["nswag","xml-docs","duplicate","aspnetcore"],"backgroundTag":"duplicate-key","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"}