我有这样的网址:
/id/{idnumber}/status
Run Code Online (Sandbox Code Playgroud)
在此URL中,/id/{idnumber}是API基本路径,/status也是资源。
我知道OpenAPI(Swagger)允许在路径中添加参数,如下所示:
/id/{idnumber}/status
Run Code Online (Sandbox Code Playgroud)
但这不是我所需要的,因为/id/{idnumber}它是基本路径,而不是重新分配路径的一部分。
有什么办法可以在基本路径中有参数吗?
paths:
/id/{number}/status:
Run Code Online (Sandbox Code Playgroud) 我正在为嵌套资源(属于交付的内容)定义路径。如果客户端收到404,则可能是因为找不到传递ID,或者传递不包含任何指定类型的内容。
如何使用OpenAPI(YAML)建立模型?
我现在有这个...
paths:
'/deliveries/{id}/content/articles':
get:
summary: Retrieves articles from a delivery
description: Retrieves all articles from a single delivery
[...]
responses:
'200':
description: articles found
schema:
$ref: '#/definitions/Article'
'404':
description: delivery not found
schema:
$ref: '#/definitions/Error'
'404':
description: delivery did not contain any articles
schema:
$ref: '#/definitions/Error'
Run Code Online (Sandbox Code Playgroud)
...但是当我从Swagger编辑器中保存JSON时,它将删除所有404响应(最后一个响应除外)(“交付内容不包含任何文章”)。
在使用swagger2(openAPI)构建rest api时,我想允许查询参数station_id支持以下内容:
我使用下面的大方定义(字符串数组)来尝试完成此操作:
parameters:
- name: station_id
in: query
description: filter stations by station_id
required: false
type: array
items:
type: string
Run Code Online (Sandbox Code Playgroud)
使用此定义,除?station_id = 23以外,所有其他示例均适用,因为挥舞性验证失败并显示以下消息:
{
"message": "Validation errors",
"errors": [
{
"code": "INVALID_REQUEST_PARAMETER",
"errors": [
{
"code": "INVALID_TYPE",
"params": [
"array",
"integer"
],
"message": "Expected type array but found type integer",
"path": [],
"description": "filter stations by station_id"
}
],
"in": "query",
"message": "Invalid parameter (station_id): Value failed …Run Code Online (Sandbox Code Playgroud) 我一直在搜索,但没有找到很多关于何时使用allOf或oneOf在 OpenApi 3.0 中的示例或明确解释。
使用allOfwith discriminator 或 有oneOf什么区别?
我的用例如下:
如果没有定义 deviceType,则无法创建设备。您应该只能创建“移动设备”/“固定设备”/“信标设备”,其中包含取决于其类型的属性以及协议中的 deviceId 和名称。
现在我已经实现了这个allOf版本,它正在运行。但我想知道这是否正是我想要的。
Device:
type: object
required:
- id
- name
- deviceType
discriminator: deviceType
properties:
deviceId:
type: string
description: The id (UUID) of the device
readOnly: true
name:
type: string
description: The name of the device
deviceType:
type: string
description: The type of the device
enum:
- MobileDevice
- PinDevice
- BeaconDevice
mobileDevice:
allOf:
- $ref: '#/definitions/Device'
- …Run Code Online (Sandbox Code Playgroud) 使用swagger-codegen-maven-plugin的Java / Spring服务器存根生成
在我的Spring Boot Java项目中,我正在使用swagger-codegen-maven-plugin从Swagger 2.0 api.yml生成Spring MVC控制器接口(服务器存根)。Maven构建过程中的集成非常简单。就像我以前使用WSDL的jaxws-maven-plugin一样,它的工作原理类似。这是带有swagger-codegen的pom.xml中的示例配置。它生成服务器端模型和mvc接口:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>/api/api-v2.yml</inputSpec>
<language>spring</language>
<apiPackage>io.fischermatte.test.api</apiPackage>
<modelPackage>io.fischermatte.test.api.model</modelPackage>
<output>${project.build.directory}/generated</output>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<sourceFolder>/src/main/java</sourceFolder>
<library>spring-mvc</library>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
但是,如何在客户端角度应用程序的npm构建过程中集成相同的东西呢?
现在,我正在尝试在我的角度应用程序的npm构建过程中集成客户端代码的生成。我有一个api.yml,它基于Swagger 2.0定义我的其余端点。我希望npm构建生成类似于此typescript-angular-v4.3示例的api模块。据我所知,没有npm的官方软件包能像使用maven时swagger-codegen-maven-plugin那样做。
我在这里误会了吗?上面提到的示例已经是结果,但是推荐的达到目标的方法是什么?在基于npm的项目中进行这样的集成不是可行的方法吗?应该手动执行并将生成的打字稿文件检入sources目录吗?
我想以与在此处完成的方式类似的方式验证openapi规范:http://bigstickcarpet.com/swagger-parser/www/index.html 但不同之处在于我使用GO来编写工具代码并且只使用CLI .
我想用这个:
https://github.com/go-openapi/validate
但主要问题是文档几乎不存在.我来到这里寻找以前可能使用过这个库的人的帮助,并且可以给我一个MINIMAL示例,发送一个包含类似规范的文件,并让这个库以与在线Swagger验证器类似的方式抛出所有错误或警告.
我已经可以读取文件并对其中的字段进行一些手动验证,但当然这不是我需要做的,而只是一个样本.
另外,作为第二个问题,我想在他们的GitHub回购中发布同样的问题,但我明白了:
我不知道如何"审查"这些指导方针,所以我可以发表我的问题.
func validate_spec(spec string) []validator_error {
// RULES HERE. Now I am hardcoding since this is just a dummy app. On the real app we will need to use goapenapi plus a schema validator
var errors []validator_error
name_regex, _ := regexp.Compile("^[a-zA-Z]+[ ][a-zA-Z]+")
// Validate _.name field
if ( ! gjson.Get(spec, "name").Exists() ) {
n := validator_error{Path: "_.name", Message: "Does not exist!"}
errors = append(errors,n)
}
if gjson.Get(spec, "name").Exists() …Run Code Online (Sandbox Code Playgroud) 我正在使用go-swagger来生成API服务器.我注意到swagger.yml生成的json正在保留restapi/embedded_spec.go.
公开JSON规范的最佳方法是什么,以便我的ReactJS客户端可以访问它?
到目前为止,我不得不使用swagger serve swagger.yml --port=50000并指向javacript客户端 localhost:50000/swagger.json.我正在寻找一种restapi/embedded_spec.go通过我的API 直接提供JSON的方法.
我使用L5-Swagger 5.7。*软件包(Swagger-php的包装器),并尝试描述Laravel REST API。所以,我的代码是这样的:
/**
* @OA\Post(path="/subscribers",
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="object",
* @OA\Property(property="email", type="string")
* )
* )
* ),
* @OA\Response(response=201,description="Successful created"),
* @OA\Response(response=422, description="Error: Unprocessable Entity")
* )
*/
public function publicStore(SaveSubscriber $request)
{
$subscriber = Subscriber::create($request->all());
return new SubscriberResource($subscriber);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试通过swagger面板发送请求时,我得到了代码:
curl -X POST "https://examile.com/api/subscribers" -H "accept: */*" -H "Content-Type: application/json" -H "X-CSRF-TOKEN: " -d "{\"email\":\"bademail\"}"
Run Code Online (Sandbox Code Playgroud)
如您所见,accept不是application / json,并且Laravel不会将其识别为AJAX请求。因此,当我发送错误的数据并期望实际获得422错误时,我在“会话”中获得200错误代码。通过招摇面板的请求(XHR)也会被错误地处理,CURL代码只是为了清楚起见。
另外,我发现在以前的版本中使用了类似以下内容:
* @SWG\Post(
* ...
* consumes={"multipart/form-data"},
* produces={"text/plain, …Run Code Online (Sandbox Code Playgroud) 该参数未分解为单独的字段,我无法理解为什么。
这是我的Yaml,使用OpenApi 3.0
paths:
/match/started:
post:
tags:
- match
summary: 'Callback for when a game has started.'
operationId: 'App\Http\Controllers\Api\V1\MatchController::started'
requestBody:
description: 'Something something batman!'
required: true
content:
multipart/form-data:
schema:
required:
- match_uuid
properties:
game_uuid:
type: string
player_uuids:
type: array
items:
type: string
type: object
encoding:
player_uuids:
style: form
explode: true
responses:
200:
description: 'success response'
content:
application/json:
schema:
$ref: '#/components/schemas/Api_V1_Match_Started'
Run Code Online (Sandbox Code Playgroud)
这是sw着嘴给我的卷曲要求(不好)
curl -X POST "https://editor.swagger.io/api/v1/match/started" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "game_uuid=test" -F "player_uuids=aaa,bbb,ccc"
您可以在其中看到最后一个参数的地方-F "player_uuids=aaa,bbb,ccc" …
我在尝试运行swashbuckle时遇到此错误。有任何想法吗?
我的ConfigureServices类别中有这个
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "testing",
Version = "v1" });
});
Run Code Online (Sandbox Code Playgroud)
这在我的配置类中
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
Run Code Online (Sandbox Code Playgroud) openapi ×10
swagger ×7
swagger-2.0 ×2
angular ×1
c# ×1
go ×1
go-swagger ×1
inheritance ×1
laravel ×1
node.js ×1
npm ×1
php ×1
rest ×1
swagger-php ×1
swagger-ui ×1
swashbuckle ×1
typescript ×1