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

Closed
melwas opened this issue Aug 17, 2016 · 4 comments

Comments

@melwas
Copy link

@melwas melwas commented Aug 17, 2016

Hi,

I have a custom snippet as below:

|===
|Path|Type|Description|Constraints

{{#fields}}

|{{#tableCellContent}}{{path}}{{/tableCellContent}}
|{{#tableCellContent}}{{type}}{{/tableCellContent}}
|{{#tableCellContent}}{{description}}{{/tableCellContent}}
|{{#tableCellContent}}{{constraints}}{{/tableCellContent}}

{{/fields}}
|===

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(Template.java:270)
    at org.springframework.restdocs.mustache.Template.getValue(Template.java:178)
    at org.springframework.restdocs.mustache.Template.getValueOrDefault(Template.java:223)
    at org.springframework.restdocs.mustache.Mustache$VariableSegment.execute(Mustache.java:787)
    at org.springframework.restdocs.mustache.Template$1.execute(Template.java:131)
    at org.springframework.restdocs.mustache.Template$1.execute(Template.java:124)
    at org.springframework.restdocs.mustache.Template$Fragment.execute(Template.java:59)
    at org.springframework.restdocs.templates.mustache.AsciidoctorTableCellContentLambda.execute(AsciidoctorTableCellContentLambda.java:36)

How to fix this?

@wilkinsona

This comment has been minimized.

Copy link
Member

@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()),
        Mustache.compiler().escapeHTML(false).defaultValue(""));
MockMvcRestDocumentationConfigurer configurer = new MockMvcRestDocumentationConfigurer(
        this.restDocumentation).templateEngine(templateEngine);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
        .apply(configurer).build();
@tasosz

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);
@helpermethod

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

{{#constraints}}{{.}}{{/constraints}}

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

@wilkinsona

This comment has been minimized.

Copy link
Member

@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
Projects
None yet
4 participants
You can’t perform that action at this time.