Skip to content

Add problem details JSON body with supported content type - #226

Merged
czechboy0 merged 7 commits into
apple:mainfrom
arthurcro:arthurcro/problem-details
Nov 3, 2026
Merged

czechboy0 merged 7 commits into
apple:mainfrom
arthurcro:arthurcro/problem-details

Conversation

@arthurcro

@arthurcro arthurcro commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Closes apple/swift-openapi-generator#963

When returning a 406, right now we only return the code. Figuring out what code (and thus content type) we were actually trying to return requires the user to dig into the OpenAPI doc/server codebase more:

case .unexpectedAcceptHeader: .notAcceptable

Modifications

Enables returning problem details error as the http body of the response when the server encountered a runtime error. https://www.rfc-editor.org/rfc/rfc9457.html.

For apple/swift-openapi-generator#963, along with human readable informations specific to the problem type, it includes two machine-readable extension fields to help clients diagnose and correct the mismatch of the particular problem details instance:

  • responseContentType: A string with the content type of the response that the server tried to send.
  • acceptableContentTypes: An array of strings with the media ranges, such as application/json or text/*, listed in the request's Accept header field, without parameters such as quality values.

Further, we add a root documentation folder to document supported problem details types and refer to that documentation in the problem details type field. https://www.rfc-editor.org/rfc/rfc9457.html#name-type

Result

HTTP/1.1 406 Not Acceptable
Content-Type: application/problem+json

{
  "acceptableContentTypes": ["text/plain"],
  "detail": "Response content type not acceptable, content type: application/json, accepted: text/plain",
  "responseContentType": "application/json",
  "status": 406,
  "title": "Response content type not acceptable",
  "type": "https://swiftpackageindex.com/apple/swift-openapi-runtime/documentation/openapiruntime/response-content-type-not-acceptable"
}

Test Plan

  • Added testRuntimeError_withUnexpectedAcceptHeader_returnsProblemDetails to Test_RuntimeError.
  • Added testValidateAccept_notAcceptable_reportsContentTypes to Test_Converter+Server.

@czechboy0
czechboy0 self-requested a review October 10, 2026 20:07

@czechboy0 czechboy0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @arthurcro - a great start, let's make it even better with more type-safety and creating docs pages for each of our errors here.

@arthurcro
arthurcro requested a review from czechboy0 October 30, 2026 20:00

@czechboy0 czechboy0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more nits, mainly the missing docs page

@arthurcro
arthurcro requested a review from czechboy0 October 31, 2026 13:55
@arthurcro
arthurcro force-pushed the arthurcro/problem-details branch 2 times, most recently from c8ca1af to a886963 Compare October 31, 2026 14:11
- Added missing code documentation for new types and root folder for documenting supported problem details types.
- Updated `ProblemDetails.ProblemType.type` to use updated documentation path
- Updated `details` to `detail` to match RFC 9457 fields
@arthurcro
arthurcro force-pushed the arthurcro/problem-details branch from a886963 to 6ac155d Compare October 31, 2026 14:12
- Updated `RuntimeError.unexpectedAcceptHeader` an expected header array and a received header array.
- `receivedContentTypes` and `expectedContentTypes` attached to the problem type are now arrays.
- Updated related docs to mention an array of string instead of a single string.
@arthurcro
arthurcro force-pushed the arthurcro/problem-details branch from 75c3e0d to e8b9a07 Compare October 31, 2026 20:35
@arthurcro
arthurcro requested a review from czechboy0 October 31, 2026 20:36

@czechboy0 czechboy0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @arthurcro - I added a few more tweaks, ready to go now

@czechboy0 czechboy0 added the 🔨 semver/patch No public API change. label Nov 3, 2026
@czechboy0
czechboy0 enabled auto-merge (squash) November 3, 2026 07:53
@czechboy0 czechboy0 self-assigned this Nov 3, 2026
@arthurcro

Copy link
Copy Markdown
Contributor Author

Thanks, @arthurcro - I added a few more tweaks, ready to go now

Great, thanks for the help wrapping this up! 🙇

@czechboy0

Copy link
Copy Markdown
Contributor

Thanks, @arthurcro - I added a few more tweaks, ready to go now

Great, thanks for the help wrapping this up! 🙇

No problem - could you just update the PR description to reflect the new shape? That'll ensure the squashed commit reflects where we landed.

@czechboy0
czechboy0 disabled auto-merge November 3, 2026 08:03
@arthurcro

Copy link
Copy Markdown
Contributor Author

Thanks, @arthurcro - I added a few more tweaks, ready to go now

Great, thanks for the help wrapping this up! 🙇

No problem - could you just update the PR description to reflect the new shape? That'll ensure the squashed commit reflects where we landed.

I just updated the description!

@czechboy0
czechboy0 merged commit b9ebb3f into apple:main Nov 3, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include a JSON body with the supported content type when returning a 406

2 participants