Springdoc随机API文档生成

huangapple go评论69阅读模式
英文:

Springdoc random api-docs generation

问题

我正在寻找一种生成接受不同内容类型的 API 的方法。

我面临的问题是,如果我多次运行我的应用程序,会生成不同的输出文档。

@RestController
public class MyRestController {

    @Operation(summary = "GetMyData", operationId = "gettt",
        responses = @ApiResponse(responseCode = "204", content = @Content(mediaType = "application/vnd.something")))
    @GetMapping(produces = "application/vnd.something")
    public ResponseEntity<Void> getSomethingElse() {
        return noContent().build();
    }

    @GetMapping(produces = TEXT_PLAIN_VALUE)
    public String get() {
        return "some text";
    }

    @GetMapping(produces = HAL_JSON_VALUE)
    public EntityModel<JsonResponse> getHal() {
        return EntityModel.of(new JsonResponse(),
            linkTo(MyRestController.class).slash("somelink").withSelfRel()
        );
    }

    @GetMapping(produces = APPLICATION_JSON_VALUE)
    public JsonResponse getJson() {
        return new JsonResponse();
    }
}

目前它生成了错误的 API 文档:

"operationId": "gettt_1_1_1",
"responses": {
    "200": {
        "content": {
            "application/hal+json": {
                "schema": {
                    "$ref": "#/components/schemas/EntityModelJsonResponse"
                }
            },
            "application/json": {
                "schema": {
                    "$ref": "#/components/schemas/JsonResponse"
                }
            },
            "text/plain": {
                "schema": {
                    "type": "string"
                }
            }
        },
        "description": "OK"
    },
    "204": {
        "content": {
            "application/hal+json": {
                "schema": {
                    "$ref": "#/components/schemas/EntityModelJsonResponse"
                }
            },
            "application/vnd.something": {},
            "text/plain": {
                "schema": {
                    "type": "string"
                }
            }
        },
        "description": "No Content"
    }
},

如果我重新启动服务器而不更改代码,则会生成以下响应:

"operationId": "gettt_1",
"responses": {
    "200": {
        "content": {
            "application/hal+json": {
                "schema": {
                    "$ref": "#/components/schemas/EntityModelJsonResponse"
                }
            },
            "application/json": {
                "schema": {
                    "$ref": "#/components/schemas/JsonResponse"
                }
            },
            "text/plain": {
                "schema": {
                    "type": "string"
                }
            }
        },
        "description": "OK"
    },
    "204": {
        "content": {
            "application/vnd.something": {}
        },
        "description": "No Content"
    }
},

我希望重新启动服务器时始终生成相同的文档。

英文:

I am looking to generate an api that take different content type.

The problem I am facing is that if I run several time my application I have different output documentation

@RestController
public class MyRestController {

    @Operation(summary = &quot;GetMyData&quot;, operationId = &quot;gettt&quot;,
        responses = @ApiResponse(responseCode = &quot;204&quot;, content = @Content(mediaType = &quot;application/vnd.something&quot;)))
    @GetMapping(produces = &quot;application/vnd.something&quot;)
    public ResponseEntity&lt;Void&gt; getSomethingElse() {
        return noContent().build();
    }

    @GetMapping(produces = TEXT_PLAIN_VALUE)
    public String get() {
        return &quot;some text&quot;;
    }

    @GetMapping(produces = HAL_JSON_VALUE)
    public EntityModel&lt;JsonResponse&gt; getHal() {
        return EntityModel.of(new JsonResponse(),
            linkTo(MyRestController.class).slash(&quot;somelink&quot;).withSelfRel()
        );
    }

    @GetMapping(produces = APPLICATION_JSON_VALUE)
    public JsonResponse getJson() {
        return new JsonResponse();
    }
}

It currently generate a wrong api-docs

&quot;operationId&quot;: &quot;gettt_1_1_1&quot;,
&quot;responses&quot;: {
	&quot;200&quot;: {
		&quot;content&quot;: {
			&quot;application/hal+json&quot;: {
				&quot;schema&quot;: {
					&quot;$ref&quot;: &quot;#/components/schemas/EntityModelJsonResponse&quot;
				}
			},
			&quot;application/json&quot;: {
				&quot;schema&quot;: {
					&quot;$ref&quot;: &quot;#/components/schemas/JsonResponse&quot;
				}
			},
			&quot;text/plain&quot;: {
				&quot;schema&quot;: {
					&quot;type&quot;: &quot;string&quot;
				}
			}
		},
		&quot;description&quot;: &quot;OK&quot;
	},
	&quot;204&quot;: {
		&quot;content&quot;: {
			&quot;application/hal+json&quot;: {
				&quot;schema&quot;: {
					&quot;$ref&quot;: &quot;#/components/schemas/EntityModelJsonResponse&quot;
				}
			},
			&quot;application/vnd.something&quot;: {},
			&quot;text/plain&quot;: {
				&quot;schema&quot;: {
					&quot;type&quot;: &quot;string&quot;
				}
			}
		},
		&quot;description&quot;: &quot;No Content&quot;
	}
},

If I restart my server without changing the code the following response is generated

&quot;operationId&quot;: &quot;gettt_1&quot;,
&quot;responses&quot;: {
	&quot;200&quot;: {
		&quot;content&quot;: {
			&quot;application/hal+json&quot;: {
				&quot;schema&quot;: {
					&quot;$ref&quot;: &quot;#/components/schemas/EntityModelJsonResponse&quot;
				}
			},
			&quot;application/json&quot;: {
				&quot;schema&quot;: {
					&quot;$ref&quot;: &quot;#/components/schemas/JsonResponse&quot;
				}
			},
			&quot;text/plain&quot;: {
				&quot;schema&quot;: {
					&quot;type&quot;: &quot;string&quot;
				}
			}
		},
		&quot;description&quot;: &quot;OK&quot;
	},
	&quot;204&quot;: {
		&quot;content&quot;: {
			&quot;application/vnd.something&quot;: {}
		},
		&quot;description&quot;: &quot;No Content&quot;
	}
},

I would expect that restarting my server will always generate the same documentation

答案1

得分: 3

你是否查看了文档?

您可以使用swagger-ui属性,而无需覆盖标准排序方式(operationsSorter和tagsSorter)。

例如:

springdoc.swagger-ui.operationsSorter=method
springdoc.swagger-ui.tagsSorter=alpha

如果您想在服务器端进行排序,可以使用OpenApiCustomiser来对元素进行排序。

以下是一个示例代码,您可以根据所需的排序逻辑自定义它,例如按照字母顺序对模式进行排序:

@Bean
public OpenApiCustomiser sortSchemasAlphabetically() {
    return openApi -> {
        Map<String, Schema> schemas = openApi.getComponents().getSchemas();
        openApi.getComponents().setSchemas(new TreeMap<>(schemas));
    };
}

示例按照字母顺序对标签进行排序:

@Bean
public OpenApiCustomiser sortTagsAlphabetically() {
    return openApi -> openApi.setTags(openApi.getTags()
            .stream()
            .sorted(Comparator.comparing(tag -> StringUtils.stripAccents(tag.getName())))
            .collect(Collectors.toList()));
}

您可以完全控制元素的顺序,并根据您的用例进行排序。

英文:

Have you looked at the documentation?

You can use the swagger-ui properties, without having to override the standard way of sorting (operationsSorter and tagsSorter).

For example:

springdoc.swagger-ui.operationsSorter=method
springdoc.swagger-ui.tagsSorter=alpha

If you want a an order on the server side, you can use OpenApiCustomiser, to sort the elements

This is a sample code that you can customize using Comparators, depending on the sorting logic you want:

Example, for alphabetical order sorting of schemas:

@Bean
public OpenApiCustomiser sortSchemasAlphabetically() {
	return openApi -&gt; {
		Map&lt;String, Schema&gt; schemas = openApi.getComponents().getSchemas();
		openApi.getComponents().setSchemas(new TreeMap&lt;&gt;(schemas));
	};
}

Example for sorting tags, in alphabetical order:

@Bean
public OpenApiCustomiser sortTagsAlphabetically() {
    return openApi -&gt; openApi.setTags(openApi.getTags()
            .stream()
            .sorted(Comparator.comparing(tag -&gt; StringUtils.stripAccents(tag.getName())))
            .collect(Collectors.toList()));
}

You can have full control on the elements order, and you can sort them depending on your use case...

答案2

得分: 1

springdoc插件中还有一个标志在这里提到

springdoc:
 writer-with-order-by-keys
英文:

one other flag mentioned here:

springdoc:
 writer-with-order-by-keys

huangapple
  • 本文由 发表于 2020年8月10日 14:47:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63335357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定