How would you support content negotiation for API versioning where different versions have genuinely different response schemas, not just different serialization formats?
Short answer
This scenario combines versioning concerns (which schema to use) with content negotiation concerns (which serialization format to use) -- typically handled by separating them: API versioning (via URL segment, header, or Asp.Versioning) determines the response schema/DTO to use…
Advertisement
AModel answer
This scenario combines versioning concerns (which schema to use) with content negotiation concerns (which serialization format to use) -- typically handled by separating them: API versioning (via URL segment, header, or Asp.Versioning) determines the response schema/DTO to use, while standard content negotiation (Accept header, output formatters) independently determines the serialization format of whichever schema was selected, keeping the two orthogonal concerns cleanly separated rather than conflating them into a single mechanism.
[ApiVersion("2.0")]
[Produces("application/json", "application/xml")] // format negotiation, independent of version
[HttpGet("{id}")]
public IActionResult GetProduct(int id) {
var v2Dto = MapToV2(product); // version determines SCHEMA
return Ok(v2Dto); // content negotiation determines FORMAT (JSON or XML) of that schema
}Real-world example
An API cleanly separates 'which version's schema to return' (handled by Asp.Versioning based on URL segment) from 'which format to serialize that schema in' (handled by standard Accept-header content negotiation), avoiding a confusing conflation of the two orthogonal concerns.
Likely follow-ups
- Why is conflating versioning and format negotiation into one mechanism generally considered poor design?
- How would documentation clearly communicate this two-dimensional selection to API consumers?
Related topics