Skip to content
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

Auto validate jsonconverter objects #3890

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,19 @@ public <T> T fromJSON(Class<T> clazz, String json) throws JSONConverterException
}
try {
byte[] bytes = string.getBytes();
return mapper.readValue(bytes, clazz);
T result = mapper.readValue(bytes, clazz);

// Validate the object if it implements JSONValidatable
if (result instanceof JSONValidatable) {
try {
((JSONValidatable) result).validate();
} catch (JSONValidationException e) {
throw new JSONConverterException("Validation failed for " + clazz.getSimpleName(), e);
}
Copy link
Member

Choose a reason for hiding this comment

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

I would add a generic catch block to catch any unsuspected exceptions that might occur during the validation process (e.g. an unsuspected null pointer).
e.g. Something like:

Suggested change
}
}
catch (Exception e){
throw new JSONConverterException("Could not validate result for " + clazz.getSimpleName(), e);
}

}

return result;
} catch (IOException e) {

LOG.debug("JSON conversion failed, origin JSON:\n{}", json);
/*
* we truncate json - because when JSON to big it could flood logs - debugging
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
package com.mercedesbenz.sechub.commons.model;

/**
* Interface for objects that can validate themselves after JSON deserialization
*/
public interface JSONValidatable {
/**
* Validates the object's state after deserialization
* @throws JSONValidationException if validation fails
*/
void validate() throws JSONValidationException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
package com.mercedesbenz.sechub.commons.model;

/**
* Exception thrown when JSON validation fails
*/
public class JSONValidationException extends SecHubRuntimeException {
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to extend the SecHubRuntimeException here, RuntimeException is fine

Suggested change
public class JSONValidationException extends SecHubRuntimeException {
public class JSONValidationException extends RuntimeException {


private static final long serialVersionUID = 1L;

public JSONValidationException(String message) {
super(message);
}

public JSONValidationException(String message, Throwable cause) {
super(message, cause);
}
}