Allow responses with non-standard status codes to be documented

Fixes gh-639
This commit is contained in:
Andy Wilkinson
2019-09-12 17:03:21 +01:00
parent d586d9f15b
commit 3597ebc35a
17 changed files with 107 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,10 +30,18 @@ public interface OperationResponse {
/**
* Returns the status of the response.
* @return the status
* @return the status or {@code null} if the status is unknown to {@link HttpStatus}
*/
HttpStatus getStatus();
/**
* Returns the status code of the response.
* @return the status code
*/
default int getStatusCode() {
throw new UnsupportedOperationException();
}
/**
* Returns the headers in the response.
* @return the headers

View File

@@ -34,8 +34,14 @@ public class OperationResponseFactory {
* @param headers the request's headers
* @param content the content of the request
* @return the {@code OperationResponse}
* @deprecated since 2.0.4 in favor of {@link #create(int, HttpHeaders, byte[])}
*/
@Deprecated
public OperationResponse create(HttpStatus status, HttpHeaders headers, byte[] content) {
return this.create(status.value(), headers, content);
}
public OperationResponse create(int status, HttpHeaders headers, byte[] content) {
return new StandardOperationResponse(status, augmentHeaders(headers, content), content);
}
@@ -49,8 +55,8 @@ public class OperationResponseFactory {
* @return the new response with the new content
*/
public OperationResponse createFrom(OperationResponse original, byte[] newContent) {
return new StandardOperationResponse(original.getStatus(), getUpdatedHeaders(original.getHeaders(), newContent),
newContent);
return new StandardOperationResponse(original.getStatusCode(),
getUpdatedHeaders(original.getHeaders(), newContent), newContent);
}
/**
@@ -61,7 +67,7 @@ public class OperationResponseFactory {
* @return the new response with the new headers
*/
public OperationResponse createFrom(OperationResponse original, HttpHeaders newHeaders) {
return new StandardOperationResponse(original.getStatus(), newHeaders, original.getContent());
return new StandardOperationResponse(original.getStatusCode(), newHeaders, original.getContent());
}
private HttpHeaders augmentHeaders(HttpHeaders originalHeaders, byte[] content) {

View File

@@ -26,7 +26,7 @@ import org.springframework.http.HttpStatus;
*/
class StandardOperationResponse extends AbstractOperationMessage implements OperationResponse {
private final HttpStatus status;
private final int status;
/**
* Creates a new response with the given {@code status}, {@code headers}, and
@@ -35,13 +35,18 @@ class StandardOperationResponse extends AbstractOperationMessage implements Oper
* @param headers the headers of the response
* @param content the content of the response
*/
StandardOperationResponse(HttpStatus status, HttpHeaders headers, byte[] content) {
StandardOperationResponse(int status, HttpHeaders headers, byte[] content) {
super(content, headers);
this.status = status;
}
@Override
public HttpStatus getStatus() {
return HttpStatus.resolve(this.status);
}
@Override
public int getStatusCode() {
return this.status;
}

View File

@@ -141,7 +141,7 @@ public class UriModifyingOperationPreprocessor implements OperationPreprocessor
@Override
public OperationResponse preprocess(OperationResponse response) {
return this.contentModifyingDelegate.preprocess(new OperationResponseFactory().create(response.getStatus(),
return this.contentModifyingDelegate.preprocess(new OperationResponseFactory().create(response.getStatusCode(),
modify(response.getHeaders()), response.getContent()));
}