Skip to content

Provide an option to switch off the curl snippet's use of -i #552

Open
@ghost

Description

Situation
In case that a REST Endpoint provide a binary content ex. application/zip. The generated curl request snippet should not contains the curl options -i. Because it include HTTP headers in the curl response stream and processing of that content will fail.

HINT
Most user will copy that curl command and save the binary content with curl option -o export.zip.

@RestController
@RequestMapping("/foo")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class FooResource {

	@GetMapping(path = "/export", produces = "application/zip")
	public ResponseEntity<byte[]> export ()
			throws IOException {

		ZipFile zipFile = ....
		return ResponseEntity.ok(toByteArray(zipFile.asInputStream()));
	}
}
curl "https://application/foo/export" -i -X GET \
    -H 'Accept: application/zip'

Proposal
In case the response payload is not text based the curl option -i will not added in the snippet

Workaround
Create a custom CurlRequestSnippet which remove the options -i and register it

private static class CustomCurlRequestSnippet extends CurlRequestSnippet {

	protected CustomCurlRequestSnippet (CommandFormatter commandFormatter) {

		super(commandFormatter);
	}

	@Override
	protected Map<String, Object> createModel(Operation operation) {
		
		Map<String, Object> model = super.createModel(operation);
		MediaType responseContentType = operation.getResponse().getHeaders().getContentType();
		if (responseContentType != null
				&& operation.getResponse().getContent().length > 0
				&& !responseContentType.isCompatibleWith(APPLICATION_JSON)) {
			
			String options = (String)model.get("options");
			model.put("options", options.replace("-i ", ""));
		}
		return model;
	}
}
MockMvcRestDocumentation.documentationConfiguration(restDocumentation)
	.uris()
		.withScheme("https")
		.withHost("foo")
		.withPort(443)
	.and()
	.snippets()
		.withAdditionalDefaults(new CustomCurlRequestSnippet(CliDocumentation.multiLineFormat()))
	.and()
	.operationPreprocessors()
		.withRequestDefaults(prettyPrint(), replaceBinaryContentWithBashDataVariable())
		.withResponseDefaults(prettyPrint(), replaceBinaryContentWithBashDataVariable());

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: enhancementEnhancement that adds a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions