extract OptionalFromJson for JSON mapper bug
This commit is contained in:
@ -0,0 +1,29 @@
|
||||
package net.hostsharing.hsadminng;
|
||||
|
||||
import org.openapitools.jackson.nullable.JsonNullable;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class OptionalFromJson<T> {
|
||||
|
||||
private final JsonNullable<T> optionalNullableValueFromJson;
|
||||
|
||||
public OptionalFromJson(final JsonNullable<T> optionalNullableValueFromJson) {
|
||||
this.optionalNullableValueFromJson = optionalNullableValueFromJson;
|
||||
}
|
||||
|
||||
public static <T> OptionalFromJson<T> of(final JsonNullable<T> optionalNullableValueFromJson) {
|
||||
return new OptionalFromJson<>(optionalNullableValueFromJson);
|
||||
}
|
||||
|
||||
public void ifPresent(final Consumer<T> setter) {
|
||||
// It looks like a bug to me, that the JsonNullable itself is null if the element was not in the JSON;
|
||||
// and if it is not null, isPresent() always returns true and thus ifPresent() always fires.
|
||||
// Instead there should always be a JsonNullable instance
|
||||
// and ifPresent() should only fire if the element was actually in th JSON.
|
||||
if (optionalNullableValueFromJson != null) {
|
||||
// this will work with the bug as well as without
|
||||
optionalNullableValueFromJson.ifPresent(setter);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package net.hostsharing.hsadminng.hs.hspackage;
|
||||
|
||||
import net.hostsharing.hsadminng.OptionalFromJson;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.api.PackagesApi;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.model.PackageResource;
|
||||
@ -52,14 +53,9 @@ public class PackageController implements PackagesApi {
|
||||
context.assumeRoles(assumedRoles);
|
||||
}
|
||||
final var current = packageRepository.findByUuid(packageUuid);
|
||||
if (body.getDescription() != null) {
|
||||
body.getDescription().ifPresent(current::setDescription);
|
||||
} else {
|
||||
body.toString();
|
||||
}
|
||||
OptionalFromJson.of(body.getDescription()).ifPresent(current::setDescription);
|
||||
final var saved = packageRepository.save(current);
|
||||
final var mapped = map(saved, PackageResource.class);
|
||||
return ResponseEntity.ok(mapped);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user