-
-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide meaningful error message for
bom
and vex
exceeding Jackso…
…n's character limit Also document the limitation in the OpenAPI spec of the respective `PUT` methods that accept JSON payloads. Fixes #3182 Signed-off-by: nscuro <[email protected]>
- Loading branch information
Showing
5 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
84 changes: 84 additions & 0 deletions
84
src/main/java/org/dependencytrack/resources/v1/exception/JsonMappingExceptionMapper.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.resources.v1.exception; | ||
|
||
import com.fasterxml.jackson.core.exc.StreamConstraintsException; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import org.dependencytrack.resources.v1.problems.ProblemDetails; | ||
import org.dependencytrack.resources.v1.vo.BomSubmitRequest; | ||
import org.dependencytrack.resources.v1.vo.VexSubmitRequest; | ||
|
||
import javax.annotation.Priority; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.container.ResourceInfo; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @since 4.11.0 | ||
*/ | ||
@Provider | ||
@Priority(1) | ||
public class JsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> { | ||
|
||
@Context | ||
private HttpServletRequest request; | ||
|
||
@Context | ||
private ResourceInfo resourceInfo; | ||
|
||
@Override | ||
public Response toResponse(final JsonMappingException exception) { | ||
final var problemDetails = new ProblemDetails(); | ||
problemDetails.setStatus(400); | ||
problemDetails.setTitle("The provided JSON payload could not be mapped"); | ||
|
||
String detail = exception.getMessage(); | ||
if (exception.getCause() instanceof StreamConstraintsException) { | ||
if (exception.getPath() != null && !exception.getPath().isEmpty()) { | ||
final JsonMappingException.Reference reference = exception.getPath().get(0); | ||
if (Objects.equals(reference.getFrom(), BomSubmitRequest.class) | ||
&& "bom".equals(reference.getFieldName())) { | ||
detail = """ | ||
The BOM is too large to be transmitted safely via Base64 encoded JSON value. \ | ||
Please use the "POST /api/v1/bom" endpoint with Content-Type "multipart/form-data" instead. \ | ||
Original cause: %s""".formatted(detail); | ||
} else if (Objects.equals(reference.getFrom(), VexSubmitRequest.class) | ||
Check warning on line 65 in src/main/java/org/dependencytrack/resources/v1/exception/JsonMappingExceptionMapper.java Codacy Production / Codacy Static Code Analysissrc/main/java/org/dependencytrack/resources/v1/exception/JsonMappingExceptionMapper.java#L65
|
||
&& "vex".equals(reference.getFieldName())) { | ||
detail = """ | ||
The VEX is too large to be transmitted safely via Base64 encoded JSON value. \ | ||
Please use the "POST /api/v1/vex" endpoint with Content-Type "multipart/form-data" instead. \ | ||
Original cause: %s""".formatted(detail); | ||
} | ||
} | ||
} | ||
|
||
problemDetails.setDetail(detail); | ||
|
||
return Response | ||
.status(Response.Status.BAD_REQUEST) | ||
.type(ProblemDetails.MEDIA_TYPE_JSON) | ||
.entity(problemDetails) | ||
.build(); | ||
} | ||
|
||
} |
This file contains 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
This file contains 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