0
我想驗證swagger文件(實際上是我的gradle構建的一部分)。 我已經嘗試過使用swagger-parser,但是雖然這可行,並且如果沒有根本解析的話,會出現錯誤,但似乎並不像http://editor.swagger.io/#/那樣捕捉問題。 (這是我正在尋找)驗證一個招搖桿
當然,這是一個常見的問題,必須有一種方法來做到這一點呢?
我已經試過:
的build.gradle:
compile("io.swagger:swagger-parser:1.0.31")
一個簡單的招搖着一些明顯的問題:
swagger: '2.0'
info:
description: 'This is a sample server Petstore server.'
version: 1.0.0
title: Swagger Petstore
termsOfService: http://swagger.io/terms/
contact:
email: [email protected]
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
host: petstore.swagger.io
basePath: /v2
paths:
/pet:
post:
tags:
- pet
summary: Add a new pet to the store
description: ''
operationId: addPet
parameters:
- in: body
name: body
description: Pet object that needs to be added to the store
required: true
schema:
type: '#/definitions/Pet'
responses:
'405':
description: Invalid input
security:
- petstore_auth:
- write:pets
- read:pets
definitions:
Category:
type: petObject
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: Category
Pet:
type: object
required:
- name
- photoUrls
properties:
id:
type: integer
format: int64
category:
$ref: '#/definitions/Category'
name:
type: string
example: doggie
xml:
name: Pet
和一個簡單的類來測試它(與意圖最終將其移至gradle):
class SwaggerTest {
public static void main(String[] args) {
SwaggerTest st = new SwaggerTest();
st.validate();
}
private void validate() {
try {
String content = new String(Files.readAllBytes(Paths.get("petstore.yml")));
SwaggerParser swaggerParser = new SwaggerParser();
SwaggerDeserializationResult result = swaggerParser.readWithInfo(content);
swaggerParser.parse(content);
Swagger swagger = result.getSwagger();
if (swagger == null) {
System.out.println("Unable to validate swagger");
}else {
System.out.println("Read the swagger");
}
List<String> messageList = result.getMessages();
for(String message: messageList) {
System.out.println(message);
}
} catch(Exception e) {
System.out.println("Error!! "+ e.getMessage());
}
}
}
上面的大錯有多個問題,你可以在http://editor.swagger.io/中看到,但是,swagger-parser並沒有告訴我這些問題。