我正在使用 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 或其他一些选项来实现?
在 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) 在使用 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) 我有一个应用程序,它提供了一个带有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) 我的 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 文件吗?
当我尝试启动 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) 我在 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.
虽然,响应正文按预期呈现得很好。
我在这里做错了什么
您如何为每个子类设置默认鉴别器?
例如,采用以下模式:
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
我正在为各种 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 中定义列表和地图?搜索我在这个主题上一无所获
openapi ×10
swagger ×7
api ×2
java ×2
rest ×2
spring-boot ×2
autorest ×1
c# ×1
jax-rs ×1
json-api ×1
polymorphism ×1
quarkus ×1
resteasy ×1
spring ×1
swashbuckle ×1