From 7d8127ef42dbc1dd659f1f1f2da7694335b82f1d Mon Sep 17 00:00:00 2001 From: Joseph Cosentino Date: Tue, 10 Oct 2023 14:27:06 -0700 Subject: [PATCH] refactor: just use null state --- .../ipc/GetThingShadowRequestHandler.java | 3 +- .../ipc/UpdateThingShadowRequestHandler.java | 12 +++--- .../shadowmanager/model/ShadowDocument.java | 14 +++---- .../model/ShadowDocumentDeserializer.java | 40 ------------------- .../shadowmanager/util/JsonUtil.java | 8 ++++ 5 files changed, 24 insertions(+), 53 deletions(-) delete mode 100644 src/main/java/com/aws/greengrass/shadowmanager/model/ShadowDocumentDeserializer.java diff --git a/src/main/java/com/aws/greengrass/shadowmanager/ipc/GetThingShadowRequestHandler.java b/src/main/java/com/aws/greengrass/shadowmanager/ipc/GetThingShadowRequestHandler.java index 4d57f7a3..b30a12d3 100644 --- a/src/main/java/com/aws/greengrass/shadowmanager/ipc/GetThingShadowRequestHandler.java +++ b/src/main/java/com/aws/greengrass/shadowmanager/ipc/GetThingShadowRequestHandler.java @@ -103,7 +103,8 @@ public GetThingShadowResponse handleRequest(GetThingShadowRequest request, Strin } ObjectNode responseNode = ResponseMessageBuilder.builder() - .withState(currentShadowDocument.get().getState().toJsonWithDelta()) + .withState(currentShadowDocument.get().getState() == null ? JsonUtil.createEmptyObject() + : currentShadowDocument.get().getState().toJsonWithDelta()) .withMetadata(currentShadowDocument.get().getMetadata().toJson()) .withVersion(currentShadowDocument.get().getVersion()) .withTimestamp(Instant.now()).build(); diff --git a/src/main/java/com/aws/greengrass/shadowmanager/ipc/UpdateThingShadowRequestHandler.java b/src/main/java/com/aws/greengrass/shadowmanager/ipc/UpdateThingShadowRequestHandler.java index 0cb63fcb..01fdc118 100644 --- a/src/main/java/com/aws/greengrass/shadowmanager/ipc/UpdateThingShadowRequestHandler.java +++ b/src/main/java/com/aws/greengrass/shadowmanager/ipc/UpdateThingShadowRequestHandler.java @@ -184,11 +184,13 @@ public UpdateThingShadowHandlerResponse handleRequest(UpdateThingShadowRequest r // to avoid double serialization, but DB stores the single document int desiredLength = 0; int reportedLength = 0; - if (!isNullOrMissing(updatedDocument.getState().getDesired())) { - desiredLength = JsonUtil.getPayloadBytes(updatedDocument.getState().getDesired()).length; - } - if (!isNullOrMissing(updatedDocument.getState().getReported())) { - reportedLength = JsonUtil.getPayloadBytes(updatedDocument.getState().getReported()).length; + if (updatedDocument.getState() != null) { + if (!isNullOrMissing(updatedDocument.getState().getDesired())) { + desiredLength = JsonUtil.getPayloadBytes(updatedDocument.getState().getDesired()).length; + } + if (!isNullOrMissing(updatedDocument.getState().getReported())) { + reportedLength = JsonUtil.getPayloadBytes(updatedDocument.getState().getReported()).length; + } } // Make sure new document is not too big diff --git a/src/main/java/com/aws/greengrass/shadowmanager/model/ShadowDocument.java b/src/main/java/com/aws/greengrass/shadowmanager/model/ShadowDocument.java index 0eec7ae2..5ed318a0 100644 --- a/src/main/java/com/aws/greengrass/shadowmanager/model/ShadowDocument.java +++ b/src/main/java/com/aws/greengrass/shadowmanager/model/ShadowDocument.java @@ -11,7 +11,6 @@ import com.aws.greengrass.util.SerializerFactory; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.node.LongNode; import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.Getter; @@ -29,9 +28,8 @@ * Class for managing operations on the Shadow Document. */ @Getter -@JsonDeserialize(using = ShadowDocumentDeserializer.class) public class ShadowDocument { - @JsonProperty(value = SHADOW_DOCUMENT_STATE, required = true) + @JsonProperty(SHADOW_DOCUMENT_STATE) private ShadowState state; @JsonProperty(SHADOW_DOCUMENT_METADATA) @@ -136,7 +134,7 @@ private void setFields(ShadowDocument shadowDocument, Long versionOverride) { } private void setFields(ShadowState state, ShadowStateMetadata metadata, Long version) { - this.state = state == null ? new ShadowState() : state; + this.state = state; this.metadata = metadata == null ? new ShadowStateMetadata() : metadata; this.version = version; } @@ -158,7 +156,9 @@ public boolean isNewDocument() { */ public JsonNode update(JsonNode updateDocumentRequest) { JsonNode updatedStateNode = updateDocumentRequest.get(SHADOW_DOCUMENT_STATE); - + if (this.state == null) { + this.state = new ShadowState(); + } this.state.update(updatedStateNode); JsonNode patchMetadata = this.metadata.update(updatedStateNode, this.state); // Incrementing the version here since we are creating a new version of the shadow document. @@ -174,8 +174,8 @@ public JsonNode update(JsonNode updateDocumentRequest) { * @return a JSON node containing the shadow document. */ public JsonNode toJson(boolean withVersion) { - final ObjectNode result = JsonUtil.OBJECT_MAPPER.createObjectNode(); - result.set(SHADOW_DOCUMENT_STATE, this.state.toJson()); + final ObjectNode result = JsonUtil.createEmptyObject(); + result.set(SHADOW_DOCUMENT_STATE, this.state == null ? JsonUtil.createNull() : this.state.toJson()); if (this.metadata != null) { result.set(SHADOW_DOCUMENT_METADATA, this.metadata.toJson()); } diff --git a/src/main/java/com/aws/greengrass/shadowmanager/model/ShadowDocumentDeserializer.java b/src/main/java/com/aws/greengrass/shadowmanager/model/ShadowDocumentDeserializer.java deleted file mode 100644 index e581728e..00000000 --- a/src/main/java/com/aws/greengrass/shadowmanager/model/ShadowDocumentDeserializer.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -package com.aws.greengrass.shadowmanager.model; - -import com.aws.greengrass.util.SerializerFactory; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; - -import java.io.IOException; - -public class ShadowDocumentDeserializer extends JsonDeserializer { - // hack to prevent StackOverflowException for custom deserialize, - // allows us to use jackson's default deserialization for type within - // a custom deserializer - static { - SerializerFactory.getFailSafeJsonObjectMapper().addMixIn(ShadowDocument.class, DefaultJsonDeserializer.class); - } - - @JsonDeserialize - private interface DefaultJsonDeserializer { - // Reset default json deserializer - } - - @Override - public ShadowDocument deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - - ShadowDocument document = ctxt.readValue(p, ShadowDocument.class); - if (document.getState() == null) { // handle {"state": null} document - ShadowDocument clearDocument = new ShadowDocument(document); - clearDocument.getState().setClear(true); - return clearDocument; - } - return document; - } -} diff --git a/src/main/java/com/aws/greengrass/shadowmanager/util/JsonUtil.java b/src/main/java/com/aws/greengrass/shadowmanager/util/JsonUtil.java index 1d525047..342c2d06 100644 --- a/src/main/java/com/aws/greengrass/shadowmanager/util/JsonUtil.java +++ b/src/main/java/com/aws/greengrass/shadowmanager/util/JsonUtil.java @@ -305,4 +305,12 @@ public static boolean hasVersion(JsonNode document) { public static long getVersion(JsonNode document) { return document.get(SHADOW_DOCUMENT_VERSION).asLong(); } + + public static ObjectNode createEmptyObject() { + return OBJECT_MAPPER.createObjectNode(); + } + + public static JsonNode createNull() { + return OBJECT_MAPPER.nullNode(); + } }