Search code examples
spring-restdocsspring-cloud-contract

How to generate pretty print docs while using spring cloud contract


Without introducing spring cloud contract, I customized the configuration of restdocs as below,

  @Rule
  public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();


  protected WebTestClient http;
  @Autowired
  private ApplicationContext context;

  /**
   * setup.
   */
  @Before
  public void before() {
    this.http = WebTestClient.bindToApplicationContext(context)
        .configureClient()
        .baseUrl("http://theserver")
        .filter(WebTestClientRestDocumentation
            .documentationConfiguration(this.restDocumentation)
            .operationPreprocessors()
            .withRequestDefaults(prettyPrint())
            .withResponseDefaults(prettyPrint())
        )
        .build();
  }

However while using spring restdocs and cloud contract together, I have to use the annotation to enable rest docs and cloud contract,

@AutoConfigureRestDocs(uriHost = "theserver", uriPort = 80)
@AutoConfigureWebTestClient
public abstract class BaseTest {

Any advice how to generate pretty print docs while generating cloud contract stubs?


Solution

  • What you can do is not to use the @AutoConfigureRestDocs but use the API to pass to WebTestClientRestDocumentation.documentationConfiguration(...) the .snippets().withAdditionalDefaults(new WireMockSnippet()) line. That way by default you will start producing WireMock snippets and all of your previous configuration will not be discarded.