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

Produce URI Snippet #299

Open
hughwphamill opened this issue Sep 7, 2016 · 5 comments
Open

Produce URI Snippet #299

hughwphamill opened this issue Sep 7, 2016 · 5 comments
Labels
type: enhancement Enhancement that adds a new feature

Comments

@hughwphamill
Copy link

hughwphamill commented Sep 7, 2016

It would be useful to be able to document a resources URI as part of documenting a given resource, this could use the custom scheme, server and port as configured in mock mvc.

It would be great if it could also include placeholders so output could something like

http://api.example.com/resources

for a collection, or

http://api.example.com/resources/{id}

For an individual resource

Bonus points if it could also produce a path variable table that could describe path variables like {id} above.

This would be obviously useful to document consumers, but also for producers as they wouldn't have to coordinate between the request snippets produced by restdocs and 'hardcoded' URIs in the asciidoc templates.

@wilkinsona
Copy link
Member

Bonus points if it could also produce a path variable table that could describe path variables like {id} above.

This is already possible using the path parameters snippet.

It would be useful to be able to document a resources URI as part of documenting a given resource, this could use the custom scheme, server and port as configured in mock mvc.

Generally speaking, I'm not keen on encouraging people to prominently document URIs. It leads to brittle documentation and can undo all of the hard work and benefits of adopting hypermedia. Unless there's an overwhelming number of people who would like this to be supported out of the box, I'd prefer to leave it as something that you add yourself. It's only a handful of lines:

public class UriSnippet extends TemplatedSnippet {

    public UriSnippet() {
        super("uri", null);
    }

    @Override
    protected Map<String, Object> createModel(Operation operation) {
        Map<String, Object> model = new HashMap<>();
        model.put("uri", operation.getRequest().getUri());
        return model;
    }
}

The accompanying template, named uri.adoc in src/test/resources/org/springframework/restdocs/templates would look something like this:

{{uri}}

The new snippet can then be used like this:

mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andDo(document("uri-snippet-example", new UriSnippet()));

Alternatively, you could configure it as one of the default snippets so that it's always generated.

@hughwphamill
Copy link
Author

Fantastic, thanks for the really useful reply. I guess I thought that seeing as the URI is in all the curl snippets anyway it wouldn't be that much of a stretch to have it on it's own. Well now you've given me the technique to do it, that's great, thanks so much. Love this project BTW, am mandating it's adoption for all internal and external APIs in my organization.

@wilkinsona
Copy link
Member

Given that no one else has requested this, it's simple to roll your own, and explicitly documenting URIs isn't something I'm keen to encourage, I'm going to close this one.

@wilkinsona wilkinsona added status: declined Suggestion or change that we don't want to make at this time type: enhancement Enhancement that adds a new feature labels Oct 13, 2017
@wilkinsona
Copy link
Member

Reopening as this requirement has been raised again (see #497).

@wilkinsona wilkinsona reopened this Mar 28, 2018
@wilkinsona wilkinsona removed the status: declined Suggestion or change that we don't want to make at this time label Mar 29, 2018
@romeara
Copy link

romeara commented Jun 15, 2018

I'm willing to look at creating a PR for this - as far as I understand, the main desired element not currently supported by provided snippets is standardized documentation of the URI (template-style).

I'll look to include that URI, and in addition the verb and accept/content-type headers in the snippet as an initial proposal (from experience thus far, those three elements are what are most important to uniquely identifies a resource on an application)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Enhancement that adds a new feature
Projects
None yet
Development

No branches or pull requests

3 participants