Add toBuilder() default method to Problem interface#557
Open
Vinu2111 wants to merge 1 commit into
Open
Conversation
Adds a toBuilder() default method to the Problem interface that creates
a ProblemBuilder pre-populated with all standard fields from the current
problem instance (type, title, status, detail, instance) plus any custom
parameters from getParameters().
This enables a common use case where applications need to intercept and
enrich framework-built Problem responses with cross-cutting parameters
such as trace IDs, without losing the original problem fields:
Problem enriched = problem.toBuilder()
.with("traceId", traceId)
.build();
Note: type-specific fields from subclasses that are not exposed via
getParameters() (e.g. violations on ConstraintViolationProblem) are
not carried over, as this is an inherent constraint of the current
architecture.
Closes zalando#507
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #507
What this does
Adds a
toBuilder()default method to theProbleminterface that creates aProblemBuilderpre-populated with all standard fields from the current problem instance (type,title,status,detail,instance) plus any custom parameters fromgetParameters().Motivation
A common use case is intercepting framework-built
Problemresponses to enrich them with cross-cutting parameters like trace IDs. Previously this required rebuilding the problem from scratch usingProblem.builder(), which lost the original fields. WithtoBuilder()this becomes straightforward:Limitation
Type-specific fields from subclasses that are not exposed via
getParameters()(e.g.violationsonConstraintViolationProblem) are not carried over, as this is an inherent constraint of the current architecture. This is documented in the method Javadoc.Test coverage
5 test cases covering: full field copy, parameter copy, adding new parameters, overriding fields, and empty problem round-trip.