我正在为具有 OpenAPI (Swagger) 定义的 REST API 构建模糊器。
我想测试 OpenAPI 定义中的所有可用路径,生成数据以测试服务器,分析响应代码和内容,并验证响应是否符合 API 定义。
我正在寻找一种从模型定义生成数据(JSON 对象)的方法。
例如,给定这个模型:
...
"Pet": {
"type": "object",
"required": [
"name",
"photoUrls"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"category": {
"$ref": "#/definitions/Category"
},
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"items": {
"$ref": "#/definitions/Tag"
}
},
"status": {
"type": "string",
"description": "pet status in the store"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想生成随机数据并得到如下内容: …
codegen没有生成任何构造函数.我提到了petstore swagger文件,使用了最新的swagger codegen jar文件.
但是只生成了默认构造函数.
它不是基于其字段生成构造函数.
如何启用它?
有没有办法记录以下查询?
GET api/v1/users?name1=value1&name2=value
Run Code Online (Sandbox Code Playgroud)
其中查询参数名称是动态的,将从客户端接收.
我正在使用最新的Swagger API.
我在yaml文件中标记了这些字段(swagger规范)
MyType:
type: object
required:
- name
- amount
Run Code Online (Sandbox Code Playgroud)
我正在使用带有这些配置的swagger codegen maven插件:
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.3</version>
<language>spring</language>
<library>spring-mvc</library>
Run Code Online (Sandbox Code Playgroud)
我希望在生成的类中也需要规范中的必填字段.但目前还没有发生这种情况.
有没有配置选项可以做到这一点?我有,<useBeanValidation>true</useBeanValidation>但这似乎对我不起作用.
我在Swagger-codegen GitHub页面上的Definitions中看到了一个类似的请求执行"必需"字段,其中建议是使用的useBeanValidation,我确实拥有它,但它仍然无效.
在Swagger-codegen GitHub页面上创建了此请求:Swagger-codegen强制生成的模型类中的"required"字段
我有一个像这样的 OpenAPI 合约:
openapi: 3.0.1
info:
title: Internal API
version: ''
tags:
- name: Calendar
description: Api for Calendar resource
paths:
'/api/v1/appointments/{id}':
get:
tags:
- Calendar
summary: Get the given appointment
description: Get the given appointment
operationId: findById
parameters:
- name: id
in: path
description: The appointment Id
required: true
schema:
type: integer
format: int64
responses:
'200':
description: Successful operation
content:
application/vnd.widget+json:
schema:
$ref: '#/components/schemas/AppointmentWidgetDto'
application/json:
schema:
$ref: '#/components/schemas/Appointment'
components:
schemas:
AppointmentWidgetDto:
required:
- contactEmail
- contactName
- contactPhone
- …Run Code Online (Sandbox Code Playgroud) 为什么 swagger-codegen 生成的项目缺少依赖项?
跑步:
java -jar swagger-codegen-cli.jar generate -l java -i swagger.json
例如生成一个项目:
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2021-04-27T18:37:06.211+08:00")
public class Table {
@SerializedName("requiredIndexColumns")
private List<Column> requiredIndexColumns = null;
Run Code Online (Sandbox Code Playgroud)
哪里javax.annotation.Generate无法解决。然后编译生成的项目会抛出:Error:(33,18) java: package javax.annotation does not exist
以下是测试方法(使用公开招摇):
wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.19/swagger-codegen-cli-2.4.19.jar -O swagger-codegen-cli.jar
Run Code Online (Sandbox Code Playgroud)
然后运行:
java -jar swagger-codegen-cli.jar generate -l java -i https://petstore.swagger.io/v2/swagger.json -o petstore
Run Code Online (Sandbox Code Playgroud) 我如何从一个 pom.xml 中的同一个模块/项目中为多个 swagger 文件生成代码。
在我的应用程序中,客户端提供了一个 swagger,我们有一个后端 API 被调用,他们提供了 swagger。我想在同一个项目中为这两个生成代码。我想的一种方法是创建单独的模块并单独执行插件,并在主模块中调用这些依赖项。
如何从一个构建插件生成代码?如果是转贴,请指出现有的。我找不到任何。
这是我在 pom.xml 中配置的插件
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/ resources/Service.json</inputSpec><inputSpec>${project.basedir}/src /main/resources/Client.json</inputSpec>
<language>java</language>
<configOptions>
<dateLibrary>java8</dateLibrary>
</configOptions>
<modelPackage>com.service.model</modelPackage>
<environmentVariables>
<models/>
<supportingFiles>false</supportingFiles>
</environmentVariables>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
也试过*.json。在任何时候它只需要一个 json 文件并生成代码。
我需要在用C++编写的遗留API接口上创建HTTP API层.我的计划是生成Swagger或开放API文件,然后将其提供给需要编写客户端(将成为新API的使用者)或服务器(旧API的包装器)的一堆项目.
有没有什么可以加快这个要求(除了手动解析它们和创建swagger规范文件)?即使用C++代码生成swagger JSON/YML文件
我有时会在我的 gradle 构建过程中遇到元空间错误。
在我的 gradle 配置中,我使用 swagger code-gen 来生成很多类(~40)。我想这可能是问题的来源。
这是build.gradle文件的摘录
dependencies {
...
implementation("io.swagger:swagger-annotations:1.5.10")
implementation("io.springfox:springfox-swagger2:2.9.2")
swaggerCodegen("io.swagger.codegen.v3:swagger-codegen-cli:3.0.0")
}
...
swaggerSources {
doe1 {
inputFile = file('src/main/resources/wiremock/gfc/yaml/doe-1.yaml')
code {
language = 'spring'
components = ['models']
additionalProperties = ["modelPackage": "io.swagger.model.doe1", "invokerPackage": "io.swagger.model"]
}
}
doe2 {
inputFile = file('src/main/resources/wiremock/gfc/yaml/doe-2.yaml')
code {
language = 'spring'
components = ['models']
additionalProperties = ["modelPackage": "io.swagger.model.doe2", "invokerPackage": "io.swagger.model"]
}
}
...
doe40 {
inputFile = file('src/main/resources/wiremock/gfc/yaml/doe-40.yaml')
code {
language = 'spring'
components = ['models']
additionalProperties = …Run Code Online (Sandbox Code Playgroud) 我注意到在使用typescript-fetch生成器生成 API 客户端时,生成的接口将所有属性指定为可为空,即使在 swagger/open API 定义中未指定为可为空。此外,将它们设为不可为空的唯一时间是使用required = true.
我还没有发现“必需”和“可空”之间的明确区别,因为它与它应该如何影响代码生成有关,但直观地说,“必需”似乎应该让客户端强制在创建请求时存在一个值,而具有nullable = false(这是默认值)应该在响应对象上生成一个不可为空的属性。
我正在寻找两者之间的明确区别,以便确定 code-gen 在指定字段是否可为空方面是否存在问题。
swagger-codegen ×10
swagger ×8
openapi ×4
java ×3
json ×2
gradle ×1
maven-plugin ×1
swagger-2.0 ×1