RicoSuter/NSwag · error · InvalidOperationException
Multiple response tags with code
Error message
Multiple response tags with code '{responseCode}' found in XML documentation for method '{methodInfo.Name}'. What it means
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.
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
Example fix
// before /// <response code="200">Success</response> /// <response code="200">Ok</response> // after /// <response code="200">Success</response> /// <response code="404">Not found</response>
Defensive patterns
Strategy: validation
Validate before calling
var codes = methodDoc.Descendants("response").Select(r => (string)r.Attribute("code"));
if (codes.GroupBy(c => c).Any(g => g.Count() > 1)) throw new InvalidOperationException("Duplicate response codes in XML docs"); Try / catch
try { GenerateWithXmlDocs(); } catch (InvalidOperationException ex) when (ex.Message.Contains("Multiple response tags")) { /* fix XML doc comments */ } Prevention
- 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
When it happens
Trigger: Documenting a controller action with two /// <response code="200">...</response> tags that share the same code, then running NSwag generation with XML docs enabled.
Common situations: 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.
Related errors
- This UI does not support multiple documents per UI: Do not…
- The SwaggerUiRoute cannot contain
- The NSwag DI services are not registered: Call…
- No registered OpenAPI/Swagger document found for the…
- API Explorer not registered in DI.
AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14).
Data as JSON: /api/errors/2a9ccfa97f65e68d.
Report an issue: GitHub.
Appendix: source
Thrown at src/NSwag.Generation/Processors/OperationResponseProcessorBase.cs:100
}
}
}
}
/// <summary>Gets the XML documentation element for the given response code or null.</summary>
/// <param name="methodInfo">The method info.</param>
/// <param name="responseCode">The response code.</param>
/// <returns>The XML element or null.</returns>
protected XElement GetResponseXmlDocsElement(MethodInfo methodInfo, string responseCode)
{
var operationXmlDocsNodes = GetResponseXmlDocsNodes(methodInfo);
try
{
return operationXmlDocsNodes?.SingleOrDefault(n => n.Name == "response" && n.Attributes().Any(a => a.Name == "code" && a.Value == responseCode));
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException($"Multiple response tags with code '{responseCode}' found in XML documentation for method '{methodInfo.Name}'.", ex);
}
}
private IEnumerable<XElement> GetResponseXmlDocsNodes(MethodInfo methodInfo)
{
var operationXmlDocs = methodInfo?.GetXmlDocsElement(_settings.SchemaSettings.GetXmlDocsOptions());
return operationXmlDocs?.Nodes()?.OfType<XElement>();
}
private List<OperationResponseDescription> GetOperationResponseDescriptions(IEnumerable<Attribute> responseTypeAttributes, string successResponseDescription)
{
List<OperationResponseDescription> operationResponseDescriptions = [];
foreach (var attribute in responseTypeAttributes)
{
dynamic responseTypeAttribute = attribute;
var attributeType = attribute.GetType();
var isProducesAttributeWithNoType = // ignore ProducesAttribute if it has no type, https://github.com/RicoSuter/NSwag/issues/1201View on GitHub (pinned to 63daf8fcc3)