首页 > 解决方案 > Swagger 声明 schema = @Schema(implementation = Map.class) 将 Schema 表示为 swagger-ui 中的 String

问题描述

我正在尝试创建springdoc招摇的文档,并且我想以Map<String, Object>更好的可读方式为客户表示具有数据类型的请求正文。但是当我宣布@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class)Schema 即将到来时String(附上下面的屏幕截图)

在此处输入图像描述

方法声明

        @Operation(security = {@SecurityRequirement(name = "bearer-key")}, summary = "Create Data", operationId = "createData", description = "Create createData for the **`type`**. ")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "Data created", content = @Content(schema = @Schema(implementation = Map.class),
                    examples = {@ExampleObject(value = "{\n" +
                            "    \"id\": \"927d810c-3ac5-4584-ba58-7c11befabf54\",\n" +
                            "}")})),
            @ApiResponse(responseCode = "400", description = "BAD Request")})
    @PostMapping(value = "/data/type", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
    @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class),
            examples = {@ExampleObject(value = "{\n" +
                    "            \"label\":\"tourism\",\n" +
                    "            \"location\":\"France\"\n" +
                    "         }")}))
    ResponseEntity<Map<String, Object>> createData(@Parameter(name = "type", required = true) @PathVariable("type") String type, @Parameter(name = "request payload") @Valid @RequestBody Map<String, Object> body);

虽然 Spring boot 会根据方法签名自动推断类型,但数据类型并不清楚Map。例如,默认情况下,类型 Map<String, Object> 将被推断如下 在此处输入图像描述

但我想以更容易理解的方式为引用我的 API 的客户展示 Schema。我可以看到在Github中有一张没有适当解决方案的封闭票。根据我的要求,请求正文应该是类型不可知的动态键值对,因此除了将请求接收为Map<String, Object>. 有没有人用类型实现了更好的方法Map而不是创建自定义请求/响应模型?

标签: spring-bootswaggerspringdocspringdoc-ui

解决方案


我有一个 API 端点,请求正文需要一个 HashMap。关于如何解决“示例值”问题的信息不多。 Prasanth 的回答把我带到了正确的地方。我正在发布我的完整解决方案,但所有功劳归他所有。(PS:我试图赞成他的回答,但我没有足够的“积分”)

配置方面:

@Configuration
@OpenAPIDefinition
public class DocsConfiguration {
    @Bean
    public OpenAPI customOpenAPI() {
        Schema newUserSchema = new Schema<Map<String, Object>>()
                .addProperties("name",new StringSchema().example("John123"))
                .addProperties("password",new StringSchema().example("P4SSW0RD"))
                .addProperties("image",new StringSchema().example("https://robohash.org/John123.png"));

        return new OpenAPI()
                //.servers(servers)
                .info(new Info()
                        .title("Your app title")
                        .description("App description")
                        .version("1.0")
                        .license(new License().name("GNU/GPL").url("https://www.gnu.org/licenses/gpl-3.0.html"))
                )
                .components(new Components()
                        .addSchemas("NewUserBody" , newUserSchema)
                );
    }
}

控制器端:

    @io.swagger.v3.oas.annotations.parameters.RequestBody(
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                    schema = @Schema(ref = "#/components/schemas/NewUserBody")))
    @PostMapping("/v1/users")
    public Response<User> upsertUser(@RequestBody HashMap<String,Object> user) {
         //Your code here
    }

推荐阅读