Skip to content

Commit a4bf268

Browse files
author
RJ Lohan
authored
Added writeOnlyProperties redaction to testEntrypoint (#296)
This fix allows the contract tests to correctly assert on writeOnlyProperties being removed from the model.
1 parent e37eccc commit a4bf268

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

python/rpdk/java/templates/generate/HandlerWrapper.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ public void testEntrypoint(
106106
e.printStackTrace();
107107
response = ProgressEvent.defaultFailureHandler(e, HandlerErrorCode.InternalFailure);
108108
} finally {
109-
final String output = this.serializer.serialize(response);
110-
outputStream.write(output.getBytes(Charset.forName("UTF-8")));
111-
outputStream.close();
109+
writeResponse(outputStream, response);
112110
}
113111
}
114112

src/main/java/software/amazon/cloudformation/LambdaWrapper.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,29 +367,30 @@ public void handleRequest(final InputStream inputStream, final OutputStream outp
367367

368368
}
369369

370-
private void writeResponse(final OutputStream outputStream, final ProgressEvent<ResourceT, CallbackT> response)
370+
protected void writeResponse(final OutputStream outputStream, final ProgressEvent<ResourceT, CallbackT> response)
371371
throws IOException {
372-
ResourceT model = response.getResourceModel();
373-
if (model != null) {
374-
JSONObject modelObject = new JSONObject(this.serializer.serialize(model));
375-
372+
if (response.getResourceModel() != null) {
376373
// strip write only properties on final results, we will need the intact model
377374
// while provisioning
378375
if (response.getStatus() != OperationStatus.IN_PROGRESS) {
379-
ResourceTypeSchema.load(provideResourceSchemaJSONObject()).removeWriteOnlyProperties(modelObject);
376+
response.setResourceModel(sanitizeModel(response.getResourceModel()));
380377
}
381-
382-
ResourceT sanitizedModel = this.serializer.deserializeStrict(modelObject.toString(), getModelTypeReference());
383-
384-
response.setResourceModel(sanitizedModel);
385378
}
386379

387380
String output = this.serializer.serialize(response);
388381
outputStream.write(output.getBytes(StandardCharsets.UTF_8));
389382
outputStream.close();
390383
}
391384

392-
private void validateModel(final JSONObject modelObject) throws ValidationException, IOException {
385+
protected ResourceT sanitizeModel(final ResourceT model) throws IOException {
386+
// strip write only properties on final results, we will need the intact model
387+
// while provisioning
388+
final JSONObject modelObject = new JSONObject(this.serializer.serialize(model));
389+
ResourceTypeSchema.load(provideResourceSchemaJSONObject()).removeWriteOnlyProperties(modelObject);
390+
return this.serializer.deserializeStrict(modelObject.toString(), getModelTypeReference());
391+
}
392+
393+
protected void validateModel(final JSONObject modelObject) throws ValidationException, IOException {
393394
JSONObject resourceSchemaJSONObject = provideResourceSchemaJSONObject();
394395
if (resourceSchemaJSONObject == null) {
395396
throw new TerminalException("Unable to validate incoming model as no schema was provided.");

0 commit comments

Comments
 (0)