Micronaut OpenAPI服务端代码生成时请求体配置报错
取消OpenAPI中
requestBody注释后Micronaut服务端代码生成报错 问题场景
取消OpenAPI规范里requestBody部分的注释后,执行Micronaut OpenAPI服务端代码生成时触发空指针错误。
OpenAPI规范代码
paths: /rentals: post: tags: - rentals operationId: create # requestBody: # content: # "application/json": # schema: # $ref: "#/components/schemas/RentalInfo" parameters: - name: name required: false in: query schema: type: string minLength: 3 responses: "201": description: Created content: "application/json": schema: $ref: "#/components/schemas/RentalInfo" "400": description: Bad Request components: schemas: RentalInfo: type: object properties: uuid: { type: string } name: { type: string } required: [ "uuid", "name" ]
报错信息
Error finalizing type visitor [io.micronaut.openapi.visitor.OpenApiApplicationVisitor@2268ad32]: Cannot invoke "String.equals(Object)" because the return value of "io.swagger.v3.oas.models.parameters.Parameter.getIn()" is null
调试排查结果
- 请求体对应的方法参数被错误识别为参数,但其
in字段为null(该参数本质是请求体,并非普通参数) - 修改方法参数名,使其与OpenAPI契约定义不匹配时,同样会触发该错误
控制器实现代码
@Controller class RentalsController implements RentalsApi { @Override public HttpResponse<RentalInfo> create(final String name, final RentalInfo rentalInfo) { return null; } @Override public List<RentalInfo> search(final String name) { return List.of( new RentalInfo(UUID.randomUUID().toString(), "Rental 1"), new RentalInfo(UUID.randomUUID().toString(), "Rental 2") ); } }
补充说明
- 使用Micronaut OpenAPI服务端代码生成功能,即使采用官方指南提供的YAML示例,也会触发该错误
- 移除上述控制器类后,错误不再出现
内容的提问来源于stack exchange,提问作者jjrz
相关产品推荐
相关产品推荐

