3

私はspringdocswaggerドキュメントを作成しようとしています.データ型を持つリクエストボディをクライアントにとってより読みやすい方法で表現したいと思いMap<String, Object>ます. しかし@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class)、スキーマが次のようになると宣言すると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 ブートはメソッド シグネチャに基づいて型を自動的に推測しますが、データ型については明確ではありませんMap。たとえば、デフォルトでは、タイプ Map<String, Object> は次のように推測されます。 ここに画像の説明を入力

しかし、API を参照するクライアントにとって、よりわかりやすい方法でスキーマを表示したいと考えています。Githubに適切な解決策がないクローズド チケットがあることがわかりました。私の要件によると、リクエストの本文はタイプにとらわれず、動的なキーと値のペアである必要があるため、リクエストを として受け取る以外に方法はありませんMap<String, Object>Mapカスタムのリクエスト/レスポンスモデルを作成するよりも、タイプを使ってより良い方法を実装した人はいますか?

4

5 に答える 5

3

この問題に対する私の作業アプローチを共有して@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class)、スキーマが文字列の問題として発生するための回避策を実行しました。

以下のように、OpenAPI Bean 宣言で Map というカスタム スキーマを宣言しました。

new OpenAPI()
                .components(new Components()
                        .addSchemas("Map", new Schema<Map<String, Object>>().addProperties("< * >", new ObjectSchema())
                        ))
                    .....
                    .....

以下のスキーマ宣言で上記のスキーマを使用しました

 @io.swagger.v3.oas.annotations.parameters.RequestBody(
            content = @Content(mediaType = APPLICATION_JSON_VALUE, 
                 schema = @Schema(ref = "#/components/schemas/Map"))

上記のアプローチはApiResponse、以下の代わりにも使用できます

 @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200",
            content = @Content(mediaType = APPLICATION_JSON_VALUE, 
                 schema = @Schema(ref = "#/components/schemas/Map"))

: 上記のカスタム スキーマ アプローチを使用する場合、SpringDoc内部で使用している型を変更したり無視したりする必要はありません。

于 2020-12-02T04:40:18.147 に答える