From 225ba6307680d7b8a463316bac807d74ee3a3779 Mon Sep 17 00:00:00 2001 From: Ryan Lohan Date: Mon, 3 Aug 2020 19:58:01 -0700 Subject: [PATCH] Added writeOnlyProperties redaction to testEntrypoint This fix allows the contract tests to correctly assert on writeOnlyProperties being removed from the model. --- .../templates/generate/HandlerWrapper.java | 4 +--- .../amazon/cloudformation/LambdaWrapper.java | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/python/rpdk/java/templates/generate/HandlerWrapper.java b/python/rpdk/java/templates/generate/HandlerWrapper.java index 8e3c9a70..3873d9ba 100644 --- a/python/rpdk/java/templates/generate/HandlerWrapper.java +++ b/python/rpdk/java/templates/generate/HandlerWrapper.java @@ -106,9 +106,7 @@ public void testEntrypoint( e.printStackTrace(); response = ProgressEvent.defaultFailureHandler(e, HandlerErrorCode.InternalFailure); } finally { - final String output = this.serializer.serialize(response); - outputStream.write(output.getBytes(Charset.forName("UTF-8"))); - outputStream.close(); + writeResponse(outputStream, response); } } diff --git a/src/main/java/software/amazon/cloudformation/LambdaWrapper.java b/src/main/java/software/amazon/cloudformation/LambdaWrapper.java index 287fc95e..1d6b4a23 100644 --- a/src/main/java/software/amazon/cloudformation/LambdaWrapper.java +++ b/src/main/java/software/amazon/cloudformation/LambdaWrapper.java @@ -367,21 +367,14 @@ public void handleRequest(final InputStream inputStream, final OutputStream outp } - private void writeResponse(final OutputStream outputStream, final ProgressEvent response) + protected void writeResponse(final OutputStream outputStream, final ProgressEvent response) throws IOException { - ResourceT model = response.getResourceModel(); - if (model != null) { - JSONObject modelObject = new JSONObject(this.serializer.serialize(model)); - + if (response.getResourceModel() != null) { // strip write only properties on final results, we will need the intact model // while provisioning if (response.getStatus() != OperationStatus.IN_PROGRESS) { - ResourceTypeSchema.load(provideResourceSchemaJSONObject()).removeWriteOnlyProperties(modelObject); + response.setResourceModel(sanitizeModel(response.getResourceModel())); } - - ResourceT sanitizedModel = this.serializer.deserializeStrict(modelObject.toString(), getModelTypeReference()); - - response.setResourceModel(sanitizedModel); } String output = this.serializer.serialize(response); @@ -389,7 +382,15 @@ private void writeResponse(final OutputStream outputStream, final ProgressEvent< outputStream.close(); } - private void validateModel(final JSONObject modelObject) throws ValidationException, IOException { + protected ResourceT sanitizeModel(final ResourceT model) throws IOException { + // strip write only properties on final results, we will need the intact model + // while provisioning + final JSONObject modelObject = new JSONObject(this.serializer.serialize(model)); + ResourceTypeSchema.load(provideResourceSchemaJSONObject()).removeWriteOnlyProperties(modelObject); + return this.serializer.deserializeStrict(modelObject.toString(), getModelTypeReference()); + } + + protected void validateModel(final JSONObject modelObject) throws ValidationException, IOException { JSONObject resourceSchemaJSONObject = provideResourceSchemaJSONObject(); if (resourceSchemaJSONObject == null) { throw new TerminalException("Unable to validate incoming model as no schema was provided.");