Skip to content

Make type in ProblemDetail nullable #35294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

cw-dimedis
Copy link
Contributor

Make ProblemDetail type field nullable to improve JSON serialization

Background

According to RFC 9457 Section 3.1.1 the "type" member is optional and when not present, its value is assumed to be "about:blank". The current Spring implementation explicitly sets this default value, which results in all problem responses containing the unhelpful "type": "about:blank" field even when no specific problem type is needed.

Changes

This change makes the type field nullable in ProblemDetail:

  • Updated setType(@Nullable URI type) to accept null values
  • Updated getType() to return @Nullable URI
  • Removed the Assert.notNull() validation that prevented null types

Compatibility

This change is completely non-breaking. Existing users will not notice any difference in behavior:

  • The type field is still initialized to BLANK_TYPE ("about:blank") by default
  • Existing code that relies on the type always being present continues to work unchanged
  • JSON serialization will continue to show "type": "about:blank" for existing usage patterns

The only difference is that developers can now optionally set the type to null if they want cleaner JSON output without the default "about:blank" value.

Benefits

  1. Optional cleaner JSON output: Developers can now choose to omit the type field entirely by setting it to null
  2. RFC compliance: Aligns with the RFC 9457 specification where the type field is truly optional
  3. Zero impact on existing code: All current usage patterns continue to work exactly as before

Example

For users who want cleaner output, they can now do:

ProblemDetail problem = ProblemDetail.forStatus(400);
problem.setType(null); // Now possible - results in cleaner JSON

This change provides more flexibility while maintaining complete backward compatibility.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 6, 2025
Signed-off-by: Christoph Wagner <[email protected]>
Signed-off-by: Christoph <[email protected]>
@cw-dimedis cw-dimedis force-pushed the problem-detail-nullable-type branch from d92ca1f to 91f2c25 Compare August 6, 2025 12:45
@rstoyanchev rstoyanchev self-assigned this Aug 7, 2025
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 7, 2025
@rstoyanchev rstoyanchev added this to the 6.2.10 milestone Aug 7, 2025
Copy link
Contributor

@rstoyanchev rstoyanchev left a comment

Choose a reason for hiding this comment

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

Thanks for the suggestion.

The @Nullable on the getter can cause issues for existing code. In 6.2.x we could avoid adding those and simply remove the Assert.notNull. It would put the burden on code setting a null value, but at least make it possible.

For 7.0 in the main branch we can accept the changes as is.

@cw-dimedis
Copy link
Contributor Author

@rstoyanchev Thanks for the feedback and the pragmatic approach for 6.2.x vs 7.0!

For 7.0, I'd suggest we could go further and remove the default BLANK_TYPE initialization entirely. The field would be null by default, giving cleaner JSON output out of the box and full RFC 9457 compliance without requiring developers to explicitly set type to null.

Is that something the team would consider? Shall I add another commit to prepare that change?

Cheers, Christoph

@rstoyanchev
Copy link
Contributor

That makes sense. Please, add the extra change.

rstoyanchev added a commit that referenced this pull request Aug 8, 2025
rstoyanchev pushed a commit that referenced this pull request Aug 8, 2025
See gh-35294

Signed-off-by: Christoph Wagner <[email protected]>
Signed-off-by: Christoph <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants