Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional attributes with custom snippet #291

melwas opened this issue Aug 17, 2016 · 4 comments


Copy link

@melwas melwas commented Aug 17, 2016


I have a custom snippet as below:





When I set the attribute "constraints" for each field in request fields snippet in Test like this:

key("constraints") .value("Format: YYYY-MM-DD'T'HH:mm:ssZ"))

it works, but when even one field is missing constraints attribute the whole suite fails with:

org.springframework.restdocs.mustache.MustacheException$Context: No method or field with name 'constraints' on line 8
    at org.springframework.restdocs.mustache.Template.checkForMissing(
    at org.springframework.restdocs.mustache.Template.getValue(
    at org.springframework.restdocs.mustache.Template.getValueOrDefault(
    at org.springframework.restdocs.mustache.Mustache$VariableSegment.execute(
    at org.springframework.restdocs.mustache.Template$1.execute(
    at org.springframework.restdocs.mustache.Template$1.execute(
    at org.springframework.restdocs.mustache.Template$Fragment.execute(
    at org.springframework.restdocs.templates.mustache.AsciidoctorTableCellContentLambda.execute(

How to fix this?


This comment has been minimized.

Copy link

@wilkinsona wilkinsona commented Aug 17, 2016

There are a couple of approaches you could take.

You could subclass RequestFieldsSnippet and override createModelFromDescriptor to add a default value for the constraints attribute.

Alternatively, you need to tell Mustache to use a default value when an entry in a template can't be resolved. Unfortunately, Mustache doesn't provide support for doing that in the template itself so you need to configure its compiler with a default value. You can do that like this:

MustacheTemplateEngine templateEngine = new MustacheTemplateEngine(
        new StandardTemplateResourceResolver(TemplateFormats.asciidoctor()),
MockMvcRestDocumentationConfigurer configurer = new MockMvcRestDocumentationConfigurer(
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)

This comment has been minimized.

Copy link

@tasosz tasosz commented Apr 4, 2017

On the snippet above, I've found that you need an extra parameter to the Mustache template engine constructor. Without it I was getting some documentation, but all table cells were empty of values.

Map<String, Object> templateContext = new HashMap<>();
templateContext.put("tableCellContent", new AsciidoctorTableCellContentLambda());

MustacheTemplateEngine templateEngine = new MustacheTemplateEngine(
        new StandardTemplateResourceResolver(TemplateFormats.asciidoctor()),
        Mustache.compiler().escapeHTML(false).defaultValue(""), templateContext);

This comment has been minimized.

Copy link

@helpermethod helpermethod commented Oct 20, 2018

Hi, for anyone reading this:

There IS actually a way to expand the constraints attribute to an empty string in case it's missing


This will output the value if it exists and an empty string if not.


This comment has been minimized.

Copy link

@wilkinsona wilkinsona commented Oct 29, 2018

That's a neat trick, @helpermethod. Thank you.

For anyone else who, like me, was wondering how it works, . is a special variable that's the equivalent of this and #constraints is a section. When constraints exists in the model and isn't false or an empty list, whatever is within the section is rendered. In this case, the contents of the section are a reference to this which is the value of the constraints entry in the model. When constraints does not exist, the content of the section is skipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
4 participants
You can’t perform that action at this time.