标签: openapi

Swagger codegen to Java Spring 从二进制格式的 OpenAPI 组件生成不正确的文件响应实体

我正在使用 swagger-codegen-maven-plugin 从 OpenAPI 文件(OpenAPI 3.0.2)生成 Spring 接口

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.14</version>
Run Code Online (Sandbox Code Playgroud)

一个rest API的响应应该是PDF文件

components:
    schemas:
        contractFile:
            type: string
            format: binary
Run Code Online (Sandbox Code Playgroud)

生成的 Java REST 接口然后包含以下方法

default ResponseEntity<File> getContract(@ApiParam(value = "File to download",required=true) @PathVariable("uid") String uid) {
...
}
Run Code Online (Sandbox Code Playgroud)

File 类表示文件系统的路径,但我在文件系统上没有文件,只有 Java 内存中的字节,我不想将它们作为文件保存到磁盘。

我希望 getContract 从内存中的 Stream/bytes 返回一些 StreamResource 或其他一些文件表示。是否可以通过 swagger-codegen-maven-plugin 或其他一些选项来实现?

java spring swagger openapi swagger-codegen

7
推荐指数
1
解决办法
1847
查看次数

将 ISchemaFilter 示例转换为 Swashbuckle 5.0

在 Swashbuckle 5 之前,可以定义和注册一个ISchemaFilter可以提供模型的示例实现:

public class MyModelExampleSchemaFilter : ISchemaFilter
{
    public void Apply(Schema schema, SchemaFilterContext context)
    {
        if (context.SystemType.IsAssignableFrom(typeof(MyModel)))
        {
            schema.Example = new MyModel
            {
                Name = "model name",
                value = 42
            };
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所述Schema.Example将采取的任意对象,并且生成的OpenAPI模式时它会适当地串行化。

但是,随着迁移到 .NET Core 3 和 Swashbuckle 5,该Schema.Example属性不再是 anobject并且需要类型Microsoft.OpenApi.Any.IOpenApiAny。似乎没有关于如何提供新示例的文件记录路径。

我已经尝试基于查看 中的代码Microsoft.OpenApi来构建我自己的 an 实现,IOpenApiAny但是任何尝试使用它来生成示例的尝试都Microsoft.OpenApi.Writers.OpenApiWriterAnyExtensions.WriteObject(IOpenApiWriter writer, OpenApiObject entity)在Write调用其方法之前从内部失败。我并不声称下面的代码是完全正确的,但我希望它至少能照亮一条路径以及如何前进。

/// <summary>
///     A class that recursively adapts a unidirectional …
Run Code Online (Sandbox Code Playgroud)

c# swashbuckle openapi asp.net-core-3.0 .net-core-3.0

7
推荐指数
1
解决办法
1761
查看次数

如何使用 Quarkus 在 RestEasy 中为 MultipartFormDataInput 提供 swagger 注释

在使用 RestEasy 框架处理 Quarkus 时,我具有使用MultipartFormDataInput. 此功能按预期工作,但我无法为 swagger UI 提供正确的开放 API 注释。我尝试了多种选择和组合,但都没有结果。请帮我。我在示例代码下方提供。

@Operation(summary = "Upload a single file", description = "Upload a single file")
    @APIResponses({
            @APIResponse(responseCode = "200", description = "Upload file successfully"),
            @APIResponse(name = "500", responseCode = "500", description = "Internal service error") })
    @RequestBody(content = @Content(
            mediaType = MediaType.MULTIPART_FORM_DATA,
            schema = @Schema(type = SchemaType.STRING, format = "binary"),
            encoding = @Encoding(name = "attachment", contentType = "application/octet-stream")))
    @POST
    @Path("/singleFile")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public Response handleFileUpload(@MultipartForm MultipartFormDataInput input) {
        String fileName …
Run Code Online (Sandbox Code Playgroud)

java resteasy swagger openapi quarkus

7
推荐指数
1
解决办法
996
查看次数

从 JavaDoc 生成 OpenAPI 描述

我有一个应用程序,它提供了一个带有JAX-RS 的API(用于 RESTful Web 服务的 Java API/JSR-311)。

出于文档目的,我根据OpenAPI 规范提供了一个 URL,该规范由 Eclipse MicroProfile OpenAPI 生成。

一切正常,除了方法和参数的描述,我需要添加两次 - 在注释和 JavaDoc 中:

/**
 * Finds all resources with the given prefix.
 *
 * @param prefix
 *            the prefix of the resource
 * @return the resources that start with the prefix
 */
@GET
@Path("/find/{prefix}")
@Produces(MediaType.APPLICATION_JSON)
@Operation(description = "Finds all resources with the given prefix")
public List<Resource> find(
        @Parameter(description = "The prefix of the resource") 
        @PathParam("prefix") final String prefix) …
Run Code Online (Sandbox Code Playgroud)

jax-rs swagger openapi

7
推荐指数
1
解决办法
204
查看次数

Autorest 错误 - swagger.json' 不是有效的 OpenAPI 2.0 定义(预期为 'swagger: 2.0')

我的 api 正在使用 Swashbuckle.AspNetCore 5.0.0-rc5 运行 net core 3.0

当我在生成的 swagger.json 文件上运行 autorest 时,我得到:

swagger.json is not a valid OpenAPI 2.0 definition (expected 'swagger: 2.0')
Run Code Online (Sandbox Code Playgroud)

我的 swagger.json 文件确实说“openapi”:“3.0.1”。它曾经是 v2.0 但自从我升级到 net core 3.0 我不得不升级现在用 v3.0.1 创建 json 文件的 Swashbuckle

我运行了“choco install autorest”,所以我应该运行最新版本

如果 autorest 不支持 openapi3.0,那么肯定还有一种方法可以生成 openapi2.0 json 文件吗?

swagger autorest openapi asp.net-core-webapi

6
推荐指数
1
解决办法
2075
查看次数

Spring Boot 应用程序中的错误 - 无法映射重复的端点操作:[MBean call 'topology'] to topologyEndpoint

当我尝试启动 Spring Boot 服务时,出现错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmxMBeanExporter' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/jmx/JmxEndpointAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.jmx.JmxEndpointExporter]: Factory method 'jmxMBeanExporter' threw exception; nested exception is java.lang.IllegalStateException: Unable to map duplicate endpoint operations: [MBean call 'topology'] to topologyEndpoint
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:66)
    at …
Run Code Online (Sandbox Code Playgroud)

spring-boot openapi openapi-generator

6
推荐指数
2
解决办法
2001
查看次数

Swagger Edtior Post 请求示例:无法渲染 n,请参阅控制台

我在 openapi 中呈现示例请求正文时遇到问题。我已经把它提炼成这个简单的例子:

openapi: 3.0.2
info:
  title: Test
  version: "1"
paths:
  /Users:
    post:
      requestBody:
        content:
          application/json:
            example: 
              name: "John"
              
      responses:
        "200":
          description: Fetches them
          content:
            application/json:
              example:
                - name: John Doe
                
Run Code Online (Sandbox Code Playgroud)

https://editor.swagger.io/#和其他一些工具似乎无法呈现请求正文。我得到的是:

Could not render n, see the console.

虽然,响应正文按预期呈现得很好。

我在这里做错了什么

swagger swagger-editor openapi

6
推荐指数
1
解决办法
2196
查看次数

OpenAPI / Swagger 3.0:默认鉴别器值

您如何为每个子类设置默认鉴别器?

例如,采用以下模式:

components:
  schemas:
    Pet:
      type: object
      required:
      - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Cat:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
Run Code Online (Sandbox Code Playgroud)

上述模式的代码生成器将创建一个客户端,其中的petType值必须由程序员显式设置。为什么不能默认设置Cat对象?petTypeCat

我尝试使用defaultvalue 让它工作。但是,生成的代码包含隐藏的属性(子级和父级上的相同属性)。

components:
  schemas:
    Pet:
      type: object
      required:
      - petType
      properties:
        petType:
          type: string …
Run Code Online (Sandbox Code Playgroud)

polymorphism swagger openapi swagger-codegen openapi-generator

6
推荐指数
1
解决办法
572
查看次数

OpenAPI 与 JSON:API

我找不到有关JSON:API和OpenAPI之间用例差异的任何资源

根据我的理解,JSON:API 更侧重于业务数据,而 OpenAPI 更侧重于 REST 本身?

任何指针都会很棒,谢谢!

api rest json-api openapi

6
推荐指数
2
解决办法
1593
查看次数

在 Swagger 中记录 @RequestBody 映射

我正在为各种 API 的文档创建 swagger 文件。在控制器中,我请求一个 RequestParam 和一个 RequestBody:

@PostMapping("/message-now/save-with-params")
    @Timed(value = MetricsTimerConstants.storeMessageWithParamsTimer)
    public ResponseEntity<Object> saveMessageWithParams(@RequestBody Map<String, Object> request,
            @RequestParam List<String> params) { .....
Run Code Online (Sandbox Code Playgroud)

有没有办法在 OpenApi 中定义列表和地图?搜索我在这个主题上一无所获

api rest swagger spring-boot openapi

6
推荐指数
1
解决办法
1468
查看次数