Skip to content

Commit

Permalink
Added a template method to ResponseTemplateTransformer allowing a sub…
Browse files Browse the repository at this point in the history
…class to supply extra model elements
  • Loading branch information
tomakehurst committed Mar 17, 2019
1 parent 54f500a commit a2586ae
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ public String getName() {
@Override
public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource files, Parameters parameters) {
ResponseDefinitionBuilder newResponseDefBuilder = ResponseDefinitionBuilder.like(responseDefinition);

final ImmutableMap<String, Object> model = ImmutableMap.<String, Object>builder()
.put("parameters", firstNonNull(parameters, Collections.<String, Object>emptyMap()))
.put("request", RequestTemplateModel.from(request)).build();
.put("request", RequestTemplateModel.from(request))
.putAll(addExtraModelElements(request, responseDefinition, files, parameters))
.build();

if (responseDefinition.specifiesTextBodyContent()) {
Template bodyTemplate = uncheckedCompileTemplate(responseDefinition.getBody());
Expand Down Expand Up @@ -150,6 +153,13 @@ public String apply(String input) {
return newResponseDefBuilder.build();
}

/**
* Override this to add extra elements to the template model
*/
protected Map<String, Object> addExtraModelElements(Request request, ResponseDefinition responseDefinition, FileSource files, Parameters parameters) {
return Collections.emptyMap();
}

private void applyTemplatedResponseBody(ResponseDefinitionBuilder newResponseDefBuilder, ImmutableMap<String, Object> model, Template bodyTemplate) {
String newBody = uncheckedApplyTemplate(bodyTemplate, model);
newResponseDefBuilder.withBody(newBody);
Expand Down

0 comments on commit a2586ae

Please sign in to comment.