spring-projects / spring-restdocs

Test-driven documentation for RESTful services
https://spring.io/projects/spring-restdocs
Apache License 2.0
1.16k stars 736 forks source link

Simplify writing additional snippets (e.g. json) #206

Closed otrosien closed 8 years ago

otrosien commented 8 years ago

As mentioned before, I am working on wiring spring restdocs and wiremock, so I uploaded a small project that demonstrates the current state: https://github.com/otrosien/restdocs-wiremock

From my point of view quite some boilerplate code is needed for this:

wilkinsona commented 8 years ago

@otrosien Thanks for opening this and for sharing the current state of things.

I've prototyped a new OutputFileResolver abstraction in this branch. However, having now taken a look at your current implementation, I'm not sure that it's necessary. Unless I've missed a subtlety in JsonWriterResolver, I think you can use StandardWriterResolver by doing something like this:

private static final TemplateFormat TEMPLATE_FORMAT = new TemplateFormat() {

    @Override
    public String getId() {
        return "json";
    }

    @Override
    public String getFileExtension() {
        return "json";
    }

};

…

@Override
public void document(Operation operation) throws IOException {
    RestDocumentationContext context = (RestDocumentationContext) operation
            .getAttributes().get(RestDocumentationContext.class.getName());
    WriterResolver writerResolver = new StandardWriterResolver(
            new RestDocumentationContextPlaceholderResolver(context), "UTF-8", TEMPLATE_FORMAT);
    try (Writer writer = writerResolver.resolve(operation.getName(), SNIPPET_NAME, context)) {
        writer.append(toJsonString(operation));
    }
}
otrosien commented 8 years ago

thanks for your feedback. I'll try immediately.

otrosien commented 8 years ago

Yay, it works. :-) BTW I'm at the moment trying to place a slot on springio.net in Barcelona for presenting this topic...

wilkinsona commented 8 years ago

Awesome, on both counts. I've submitted a general Spring REST Docs talk. I haven't heard whether or not it's been accepted yet.

otrosien commented 8 years ago

Alright, would be nice meeting in Barcelona then. :+1:

So, regarding this issue, I think it can be closed - the integration is actually simple enough. I'll be fiddling some more with improving how to autowire a wiremock on a dynamic port on the client-side, but that's a different issue. And maybe publish the existing integration on bintray.

Would you consider integrating such a feature into spring restdocs core?

wilkinsona commented 8 years ago

Alright, would be nice meeting in Barcelona then. :+1:

It would indeed. Fingers crossed.

Would you consider integrating such a feature into spring restdocs core?

Absolutely. As I think I said in another issue, I consider Wiremock JSON to be another form of documentation, just one that's not normally consumed by a person.