Skip to content

Commit

Permalink
Cleanup: explicitly annotate ignoreUnknown for types that allow this (
Browse files Browse the repository at this point in the history
#7561)

Replaces the custom `ObjectMapper` configuration with settings on the affected types, which seems cleaner and allows using "standard" `ObjectMapper`s.
  • Loading branch information
snazy authored Sep 27, 2023
1 parent 7db0da8 commit 087d584
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package org.projectnessie.client.rest;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_INVALID_SUBTYPE;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -30,18 +27,11 @@
import org.projectnessie.error.ErrorCode;
import org.projectnessie.error.ImmutableNessieError;
import org.projectnessie.error.NessieError;
import org.projectnessie.error.NessieErrorDetails;
import org.projectnessie.error.NessieUnavailableException;

public class ResponseCheckFilter {

/**
* Object mapper that ignores unknown properties and unknown subtypes, so it is able to process
* instances of {@link NessieError} and especially {@link NessieErrorDetails} with added/unknown
* properties or unknown subtypes of the latter.
*/
private static final ObjectMapper MAPPER =
new ObjectMapper().disable(FAIL_ON_UNKNOWN_PROPERTIES).disable(FAIL_ON_INVALID_SUBTYPE);
private static final ObjectMapper MAPPER = new ObjectMapper();

/**
* check that response had a valid return code. Throw exception if not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.projectnessie.client.rest;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_INVALID_SUBTYPE;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -70,8 +68,7 @@
@ExtendWith(SoftAssertionsExtension.class)
public class TestResponseFilter {

private static final ObjectMapper MAPPER =
new ObjectMapper().disable(FAIL_ON_UNKNOWN_PROPERTIES).disable(FAIL_ON_INVALID_SUBTYPE);
private static final ObjectMapper MAPPER = new ObjectMapper();

@InjectSoftAssertions protected SoftAssertions soft;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.projectnessie.error;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
Expand All @@ -36,6 +37,7 @@
@Value.Immutable
@JsonSerialize(as = ImmutableNessieError.class)
@JsonDeserialize(as = ImmutableNessieError.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public interface NessieError {

/** HTTP status code of this error. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.projectnessie.error;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
Expand All @@ -36,6 +37,7 @@
discriminatorProperty = "type")
@JsonTypeIdResolver(NessieErrorDetailsTypeIdResolver.class)
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "type", visible = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public interface NessieErrorDetails {
@Value.Redacted
@JsonIgnore
Expand Down
4 changes: 4 additions & 0 deletions api/model/src/main/java/org/projectnessie/model/Conflict.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.projectnessie.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
Expand All @@ -28,6 +29,9 @@
@Value.Immutable
@JsonSerialize(as = ImmutableConflict.class)
@JsonDeserialize(as = ImmutableConflict.class)
@JsonIgnoreProperties(
// need to ignore unknown properties as this type can be used in `ReferenceConflicts`
ignoreUnknown = true)
public interface Conflict {
@Value.Parameter(order = 1)
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.projectnessie.error;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.projectnessie.model.Conflict.ConflictType.UNEXPECTED_HASH;
Expand Down Expand Up @@ -81,8 +80,7 @@ static Stream<Arguments> conflictDeserialization() {
@ParameterizedTest
@MethodSource("conflictDeserialization")
void conflictDeserialization(JsonNode input, Conflict expected) throws Exception {
Conflict parsedConflict =
new ObjectMapper().disable(FAIL_ON_UNKNOWN_PROPERTIES).treeToValue(input, Conflict.class);
Conflict parsedConflict = new ObjectMapper().treeToValue(input, Conflict.class);
Assertions.assertThat(parsedConflict).isEqualTo(expected);
}

Expand Down

0 comments on commit 087d584

Please sign in to comment.