为什么对象查询参数为空?

时间:2019-01-15 14:57:57

标签: java spring rest query-parameters openapi

我在我的Spring Boot应用程序的openapi文件中描述了一个路径。 Openapi由处理Http请求的文件api类生成。 此外,我还使用swagger,它会帮助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'

生成的Java类

@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);

    }

生成的查询参数类

@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

如何改善openapi文件?因为这是我定义的唯一内容。还是什么原因呢?

2 个答案:

答案 0 :(得分:0)

尝试从过滤器对象中删除@RequestParam()

赞:

default ResponseEntity<SelectionTo> _findFilms(@ApiParam() @Valid Filter filter) {
    return findFilms(filter);
}

请求也应为http://localhost/films?genreId=13&year=2021

答案 1 :(得分:0)

据我所知,Spring MVC 不支持解码 OpenAPI deepObject 样式中的嵌套对象查询参数,例如 filter[genreId]=13,至少开箱即用。