Adds support to specify priority and allow for template override (#1882)

* Adds support to specify priority and make it possible to override the snippet template

Fixes gh-1818

* Adds support to specify priority and make it possible to override the snippet template

Fixes gh-1818

---------

Co-authored-by: Wallace Wadge <wallace.wadge@gamesysgroup.com>
This commit is contained in:
Wallace Wadge
2023-04-06 10:05:50 +02:00
committed by GitHub
parent 0d9669f5a0
commit e4d19d8e9b
3 changed files with 20 additions and 2 deletions

View File

@@ -84,13 +84,17 @@ public class ContractDslSnippet extends TemplatedSnippet {
@Override
public void document(Operation operation) throws IOException {
TemplateEngine templateEngine = (TemplateEngine) operation.getAttributes().get(TemplateEngine.class.getName());
String renderedContract = templateEngine.compileTemplate("default-dsl-contract-only")
String renderedContract = templateEngine.compileTemplate(getTemplate())
.render(createModelForContract(operation));
this.model.put("contract", renderedContract);
storeDslContract(operation, renderedContract);
super.document(operation);
}
protected String getTemplate() {
return "default-dsl-contract-only";
}
private void insertResponseModel(Operation operation, Map<String, Object> model) {
OperationResponse response = operation.getResponse();
model.put("response_status", response.getStatus().value());
@@ -152,9 +156,18 @@ public class ContractDslSnippet extends TemplatedSnippet {
Map<String, Object> modelForContract = new HashMap<>();
insertRequestModel(operation, modelForContract);
insertResponseModel(operation, modelForContract);
insertAdditionalModel(operation, modelForContract);
return modelForContract;
}
protected void insertAdditionalModel(Operation operation, Map<String, Object> modelForContract) {
boolean hasPriority = getAttributes().containsKey("priority");
modelForContract.put("priority_present", hasPriority);
if (hasPriority) {
modelForContract.put("priority", getAttributes().get("priority"));
}
}
private void storeDslContract(Operation operation, String content) throws IOException {
RestDocumentationContext context = (RestDocumentationContext) operation.getAttributes()
.get(RestDocumentationContext.class.getName());

View File

@@ -41,4 +41,7 @@ Contract.make {
}
{{/response_headers_present}}
}
{{#priority_present}}
priority {{priority}}
{{/priority_present}}
}

View File

@@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.groovy.util.Maps;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -94,7 +95,7 @@ public class ContractDslSnippetTests {
.jsonPath("$[?(@.bar in ['baz','bazz','bazzz'])]")
.contentType(MediaType.valueOf("application/json")))
// then Contract DSL documentation
.andDo(document("index", SpringCloudContractRestDocs.dslContract()));
.andDo(document("index", SpringCloudContractRestDocs.dslContract(Maps.of("priority", 1))));
// end::contract_snippet[]
then(file("/contracts/index.groovy")).exists();
@@ -116,6 +117,7 @@ public class ContractDslSnippetTests {
then(parsedContract.getResponse().getStatus().getClientValue()).isNotNull();
then(parsedContract.getResponse().getHeaders().getEntries()).isNotEmpty();
then(parsedContract.getResponse().getBody().getClientValue()).isNotNull();
then(parsedContract.getPriority().intValue()).isEqualTo(1);
}
@Test