RestDocs 对请求参数做双文档
RestDocs does double documentation on request parameters
使用RestDocs version 1.1.1.RELEASE和SpringBoot 1.4.0.RELEASE,使用"httpRequest"时参数双倍记录。
以下代码:
@RunWith(SpringRunner.class)
@WebMvcTest(ProductResource.class)
@AutoConfigureRestDocs("target/generated-snippets")
public class ProductResourceDocumentation {
private static final String PROPERTY_ID_VALUE = "d7612";
private static final String SALES_MARKET_VALUE = "999";
private static final String SEASON_VALUE = "2016";
private static final String SECTIONS_VALUE = "buildings";
private static final String SHOW_DESCRIPTIONS_VALUE = "false";
private static final String STRING = "string";
private static final Integer NUMBER = 1;
private static final String NOT_FOUND = "404 Not Found";
private static final String BAD_REQUEST = "400 Bad Request";
private RestDocumentationResultHandler documentationResultHandler;
@MockBean
private ProductHandler productHandler;
@Autowired
private MockMvc mockMvc;
@Before
public void setup() {
this.documentationResultHandler = document("{class-name}/{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()));
}
@Test
public void listProducts() throws Exception {
Product product = getBasicProductsRepresentation();
when(productHandler.getProduct(PROPERTY_ID_VALUE, SALES_MARKET_VALUE, SEASON_VALUE, null, null)).thenReturn(product);
mockMvc.perform(RestDocumentationRequestBuilders.get("/products/{propertyId}", PROPERTY_ID_VALUE)
.param("salesMarket", SALES_MARKET_VALUE)
.param("season", SEASON_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.propertyID", is(PROPERTY_ID_VALUE)))
.andExpect(jsonPath("$.season", is(SEASON_VALUE)))
.andDo(documentationResultHandler.document(
pathParameters(
parameterWithName("propertyId").description(PATH_PARAM__PROPERTY_ID)
),
requestParameters(
parameterWithName("salesMarket").description(REQUEST_PARAM__SALES_MARKET),
parameterWithName("season").description(REQUEST_PARAM__SEASON)
),
httpRequest(), httpResponse()
));
}
生成以下文档("salesMarket=999&season=2016" 被记录了两次):
GET /products/d7612? salesMarket=999&season=2016&salesMarket=999&season=2016 HTTP/1.1
有没有人经历过类似的事情或知道问题出在哪里?
a bug 已在 1.1.2.RELEASE 中修复。升级将解决问题。升级时确保您获得 spring-restdocs-mockmvc
和 spring-restdocs-core
的 1.1.2。后一个模块是修复的地方。
当我们将查询参数(参见下面的块)设置为空字符串 ("") 时,我们得到 "double documentation":
GET /Thing/OtherThing/Foo/?thinger=FOO&block=&bla=5&block= HTTP/1.1
我们使用 Spring Boot 1.5.2 和 Spring REST Docs 1.1.2.RELEASE.
使用RestDocs version 1.1.1.RELEASE和SpringBoot 1.4.0.RELEASE,使用"httpRequest"时参数双倍记录。
以下代码:
@RunWith(SpringRunner.class)
@WebMvcTest(ProductResource.class)
@AutoConfigureRestDocs("target/generated-snippets")
public class ProductResourceDocumentation {
private static final String PROPERTY_ID_VALUE = "d7612";
private static final String SALES_MARKET_VALUE = "999";
private static final String SEASON_VALUE = "2016";
private static final String SECTIONS_VALUE = "buildings";
private static final String SHOW_DESCRIPTIONS_VALUE = "false";
private static final String STRING = "string";
private static final Integer NUMBER = 1;
private static final String NOT_FOUND = "404 Not Found";
private static final String BAD_REQUEST = "400 Bad Request";
private RestDocumentationResultHandler documentationResultHandler;
@MockBean
private ProductHandler productHandler;
@Autowired
private MockMvc mockMvc;
@Before
public void setup() {
this.documentationResultHandler = document("{class-name}/{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()));
}
@Test
public void listProducts() throws Exception {
Product product = getBasicProductsRepresentation();
when(productHandler.getProduct(PROPERTY_ID_VALUE, SALES_MARKET_VALUE, SEASON_VALUE, null, null)).thenReturn(product);
mockMvc.perform(RestDocumentationRequestBuilders.get("/products/{propertyId}", PROPERTY_ID_VALUE)
.param("salesMarket", SALES_MARKET_VALUE)
.param("season", SEASON_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.propertyID", is(PROPERTY_ID_VALUE)))
.andExpect(jsonPath("$.season", is(SEASON_VALUE)))
.andDo(documentationResultHandler.document(
pathParameters(
parameterWithName("propertyId").description(PATH_PARAM__PROPERTY_ID)
),
requestParameters(
parameterWithName("salesMarket").description(REQUEST_PARAM__SALES_MARKET),
parameterWithName("season").description(REQUEST_PARAM__SEASON)
),
httpRequest(), httpResponse()
));
}
生成以下文档("salesMarket=999&season=2016" 被记录了两次):
GET /products/d7612? salesMarket=999&season=2016&salesMarket=999&season=2016 HTTP/1.1
有没有人经历过类似的事情或知道问题出在哪里?
a bug 已在 1.1.2.RELEASE 中修复。升级将解决问题。升级时确保您获得 spring-restdocs-mockmvc
和 spring-restdocs-core
的 1.1.2。后一个模块是修复的地方。
当我们将查询参数(参见下面的块)设置为空字符串 ("") 时,我们得到 "double documentation":
GET /Thing/OtherThing/Foo/?thinger=FOO&block=&bla=5&block= HTTP/1.1
我们使用 Spring Boot 1.5.2 和 Spring REST Docs 1.1.2.RELEASE.