我正在尝试为HTTP API编写一个swagger规范,该API被查询为/relationships?contact_id=12345
并返回一个JSON数组,其关系对象看起来像[{"code": "ABC", "org": "MyOrg"}, ...]
。我有以下内容:
swagger: '2.0'
paths:
/relationships:
get:
parameters:
-
name: contact_id
in: query
required: true
type: integer
responses:
200:
schema:
type: array
items:
-
type: object
properties:
code:
type: string
org:
type: string
但在尝试使用它时(使用java -jar swagger-codegen-cli.jar generate -i test.swagger -l python
),我会收到一组意外的错误:
[main] ERROR io.swagger.codegen.DefaultCodegen - String to be sanitized is null. Default to ERROR_UNKNOWN
[main] ERROR io.swagger.codegen.DefaultCodegen - unexpected missing property for name response
[main] WARN io.swagger.codegen.DefaultCodegen - skipping invalid property {
"type" : "array"
}
[main] ERROR io.swagger.codegen.DefaultCodegen - unexpected missing property for name response
Exception in thread "main" java.lang.RuntimeException: Could not process operation:
Tag: default
Operation: null
Resource: get /relationships
Definitions: {}
Exception: null
at io.swagger.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:720)
at io.swagger.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:612)
at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:339)
at io.swagger.codegen.cmd.Generate.run(Generate.java:223)
at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:36)
Caused by: java.lang.NullPointerException
at io.swagger.codegen.DefaultCodegen.fromResponse(DefaultCodegen.java:1646)
at io.swagger.codegen.DefaultCodegen.fromOperation(DefaultCodegen.java:1440)
at io.swagger.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:668)
... 4 more
我在这里做错了什么?我不知道“名称响应的意外缺失属性”是什么意思。
答案 0 :(得分:1)
试试这个:
swagger: '2.0'
info:
version: 'v0'
title: Relationships
description: My Relationships API
paths:
/relationships:
get:
parameters:
-
name: contact_id
in: query
required: true
type: integer
responses:
200:
description: Ok
schema:
type: array
items:
-
type: object
properties:
code:
type: string
org:
type: string
根据http://swagger.io/specification,顶部的info
块是必需的。我还在description
响应中添加了200
字段。不幸的是,我发现Swagger错误并不总是有用。 : - (