swagger OpenApi如何从资源文件中添加示例@RequestBody ->@Content ->@Schema ->示例

5gfr0r5j  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(362)

我正在开发一个基于服务的应用程序,我正在为该应用程序添加基于openapi的注解,例如@Schema中的@RequestBody, @Parameter, @Schema。我有一个example字段,我可以为该字段提供String的示例模板。
我已经提供了example JSON string,但是JSON内容太大了,所以我想从resources文件夹中的file添加它。但是我目前无法加载它。有人能告诉我如何从文件而不是字符串添加示例内容吗?
我试着看了一下,发现有一个字段externalValue,但我不知道如何使它工作。下面是文档的链接。
下面是我的代码,它是完美的工作:

  1. @Path("/generate")
  2. @POST
  3. @Consumes(MediaType.APPLICATION_JSON)
  4. @Produces(MediaType.APPLICATION_JSON)
  5. @RequestBody(description = "InputTemplate body",
  6. content = @Content(schema = @Schema(implementation = InputTemplate.class, example = "{\n" +
  7. " \"names\":[\n" +
  8. " \"Batman\",\n" +
  9. " \"Superman\",\n" +
  10. " \"Ironman\"\n" +
  11. " ],\n" +
  12. " \"jobs\":[\n" +
  13. " \"Fighting\",\n" +
  14. " \"Fyling\",\n" +
  15. " \"Teching\"\n" +
  16. " ]\n" +
  17. "}")))
  18. public Multi<String> generate(final Map<String, Object> input) throws CustomException {
  19. }

我想将example中的JSON内容替换为resources文件夹中的外部文件的内容。
在尝试了很多东西之后,我知道我需要使用@ExampleObject,但是如果我添加了相应的注解并试图打开我的Swagger UI,那么我就不会得到我添加的文件的内容。相反,它为我提供了来自InputTemplate.class的数据。
以下是修改后的代码:

  1. @RequestBody(description = "InputTemplate body",
  2. content = @Content(schema = @Schema(implementation = InputTemplate.class), examples = {
  3. @ExampleObject(name = "Example-1",
  4. description = "Example-1 for InputTemplate.",
  5. ref = "#/resources/Example1.json"), externalValue = "#/resources/Example2.json"
  6. @ExampleObject(name = "Example-2",
  7. description = "Example-2 for InputTemplate.",
  8. ref = "#/resources/Example1.json") //externalValue = "#/resources/Example1.json"
  9. }))

我试图调查类似的问题,但提供的回答对我不起作用:

  1. How to refrence files in SpringDoc OpenAPI3?
  2. https://github.com/springdoc/springdoc-openapi/issues/1432
  3. https://github.com/springdoc/springdoc-openapi/issues/17
nzkunb0c

nzkunb0c1#

就我所知,ref值似乎期望一个可以找到架构的url?我看到有人建议创建一个端点来返回示例?这对我来说似乎有点过分...
我决定最简单的方法就是添加一些东西,从文件中提取示例并将它们插入OpenApi对象。
我在我的spring配置中实现了一个OpenApiCustomiser,这允许我指向apps resources文件夹中的文件以获取响应示例。
我对Controller方法进行了如下注解:

  1. @ApiResponses(value = {
  2. @ApiResponse(responseCode = "200",
  3. content = { @Content(mediaType = "application/json",
  4. schema = @Schema(implementation = SomeResponse.class,
  5. name = "YourResponse"),
  6. examples = {@ExampleObject(value = "@your_data_200_response.json")}) })
  7. })

要使上述内容正常工作,请添加以下OpenApiCustomiser配置bean:

  1. @Bean
  2. public OpenApiCustomiser applyStandardOpenAPIModifications() {
  3. return openApi -> {
  4. Paths paths = new Paths();
  5. openApi.getPaths().entrySet().stream()
  6. .sorted(Map.Entry.comparingByKey())
  7. .forEach(stringPathItemEntry -> {
  8. paths.addPathItem(stringPathItemEntry.getKey(), addExamples(stringPathItemEntry.getValue()));
  9. });
  10. openApi.setPaths(paths);
  11. };
  12. }
  13. private PathItem addExamples(PathItem pathItem) {
  14. if(pathItem.getPost() !=null) {
  15. //Note you can also Do this to APIResponses to insert info from a file into examples in say, a 200 response.
  16. pathItem.getPost().getRequestBody().getContent().values().stream()
  17. .forEach(c ->{
  18. String fileName = c.getExample().toString().replaceFirst("@","");
  19. ObjectNode node = null;
  20. try {
  21. //load file from where you want. also don't insert is as a string, it wont format properly
  22. node = (ObjectNode) new ObjectMapper().readTree(methodToReadInFileToString(fileName));
  23. } catch (JsonProcessingException e) {
  24. throw new RuntimeException(e);
  25. }
  26. c.setExample(node);
  27. }
  28. );
  29. }
  30. return pathItem;
  31. }

我只是从包含.json文件的/resources文件夹中加载文件。

展开查看全部

相关问题