为什么对象查询参数为空?
Why is object query parameter null?
我在 spring 引导应用程序的打开 api 文件中描述了一个路径。
Openapi 由处理 http 请求的文件 api class 生成。
我还使用 swagger which hepls 构造一个有效的 url,我也可以在其中放置查询参数。
我想知道,为什么我收到所有这些生成的工作人员而不是预期的空对象。
api.yaml的一部分
/films:
get:
summary: Отфильтрованные фильмы
operationId: findFilms
tags: [selections]
parameters:
- in: query
name: filter
schema:
type: object
properties:
genreId:
type: integer
year:
type: integer
countryId:
type: integer
style: deepObject
explode: false
responses:
200:
description: successfull response
content:
application/json:
schema:
$ref: 'list-schemas.yaml#/components/schemas/SelectionTo'
生成javaclass
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
@Validated
@Api(value = "Selections")
public interface SelectionsApi {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
@ApiOperation(value = "Отфильтрованные фильмы", nickname = "findFilms", , response = SelectionTo.class, tags={ "selections", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful response", response = SelectionTo.class) })
@RequestMapping(value = "/films",
produces = { "application/json" },
method = RequestMethod.GET)
default ResponseEntity<SelectionTo> _findFilms(@ApiParam() @Valid @RequestParam(value = "filter", required = false) Filter filter) {
return findFilms(filter);
}
// Override this method
default ResponseEntity<SelectionTo> findFilms(Filter filter) {
getRequest().ifPresent(request -> {
...
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
生成查询参数class
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Filter {
@JsonProperty("genreId")
private Integer genreId = null;
@JsonProperty("year")
private Integer year = null;
@JsonProperty("countryId")
private Integer countryId = null;
public Filter genreId(Integer genreId) {
this.genreId = genreId;
return this;
}
实现接口
@Override
public ResponseEntity<SelectionTo> findFilms(Filter filterType) {
//here filter is null !
return ResponseEntity.ok(transformer.transform(service.getItemsInfo()));
}
要求
http://localhost/films?filter[genreId]=13&filter[year]=2021
如何改进打开api文件?因为这是我唯一定义的东西。或者还有什么原因?
尝试从过滤器对象中删除 @RequestParam()
。
像这样:
default ResponseEntity<SelectionTo> _findFilms(@ApiParam() @Valid Filter filter) {
return findFilms(filter);
}
据我所知,Spring MVC 不支持解码 OpenAPI deepObject
样式中的嵌套对象查询参数,例如 filter[genreId]=13
,至少开箱即用。
我在 spring 引导应用程序的打开 api 文件中描述了一个路径。 Openapi 由处理 http 请求的文件 api class 生成。 我还使用 swagger which hepls 构造一个有效的 url,我也可以在其中放置查询参数。 我想知道,为什么我收到所有这些生成的工作人员而不是预期的空对象。
api.yaml的一部分
/films:
get:
summary: Отфильтрованные фильмы
operationId: findFilms
tags: [selections]
parameters:
- in: query
name: filter
schema:
type: object
properties:
genreId:
type: integer
year:
type: integer
countryId:
type: integer
style: deepObject
explode: false
responses:
200:
description: successfull response
content:
application/json:
schema:
$ref: 'list-schemas.yaml#/components/schemas/SelectionTo'
生成javaclass
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
@Validated
@Api(value = "Selections")
public interface SelectionsApi {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
@ApiOperation(value = "Отфильтрованные фильмы", nickname = "findFilms", , response = SelectionTo.class, tags={ "selections", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful response", response = SelectionTo.class) })
@RequestMapping(value = "/films",
produces = { "application/json" },
method = RequestMethod.GET)
default ResponseEntity<SelectionTo> _findFilms(@ApiParam() @Valid @RequestParam(value = "filter", required = false) Filter filter) {
return findFilms(filter);
}
// Override this method
default ResponseEntity<SelectionTo> findFilms(Filter filter) {
getRequest().ifPresent(request -> {
...
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
生成查询参数class
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Filter {
@JsonProperty("genreId")
private Integer genreId = null;
@JsonProperty("year")
private Integer year = null;
@JsonProperty("countryId")
private Integer countryId = null;
public Filter genreId(Integer genreId) {
this.genreId = genreId;
return this;
}
实现接口
@Override
public ResponseEntity<SelectionTo> findFilms(Filter filterType) {
//here filter is null !
return ResponseEntity.ok(transformer.transform(service.getItemsInfo()));
}
要求
http://localhost/films?filter[genreId]=13&filter[year]=2021
如何改进打开api文件?因为这是我唯一定义的东西。或者还有什么原因?
尝试从过滤器对象中删除 @RequestParam()
。
像这样:
default ResponseEntity<SelectionTo> _findFilms(@ApiParam() @Valid Filter filter) {
return findFilms(filter);
}
据我所知,Spring MVC 不支持解码 OpenAPI deepObject
样式中的嵌套对象查询参数,例如 filter[genreId]=13
,至少开箱即用。