Any tools to validate requests against a swagger doc?

3,749 views
Skip to first unread message

Laurie Hallowes

unread,
Oct 1, 2014, 12:39:20 PM10/1/14
to swagger-sw...@googlegroups.com
Hi all,

I have a requirement to validate requests against a swagger defintion (preferably yaml file) using Java. I have had a look to see if something that does this already exists but there are so many swagger related projets around that im not sure if I have misunderstood the purpose of various projects or missed some. Most of the java projects I have looked at are focused on generating the documentation from the code but we using a contract first approach where the contract is written in yaml up front.

Are there any projects out there that will validate a request against the swagger definition, or even as a partial step any projects that will go from a yaml defintion to a set of java objects?

Thanks


Ron

unread,
Oct 1, 2014, 3:03:02 PM10/1/14
to swagger-sw...@googlegroups.com
The only implementation I'm aware of is for Swagger 1.2 and specific for PHP - https://github.com/gong023/swagger-assert.

I would advise not to rely on YAML as a source of truth. The Swagger specification is JSON-based, and the YAML is used by the editor for convenience.
I don't think library developers should be forced to support both. Since the editor can export the YAML to JSON, that should be used for all other tools.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tony tam

unread,
Oct 1, 2014, 4:50:26 PM10/1/14
to swagger-sw...@googlegroups.com
Do you mean something like this?


That will verify that your spec is valid and return you information.  It's online, and you can run it locally yourself since it's all OSS.

Laurie Hallowes

unread,
Oct 2, 2014, 4:33:07 AM10/2/14
to swagger-sw...@googlegroups.com
Thanks guys,

Ok I didnt realise that you saw the yaml syntax only as nicer way of getting to the json, I will bear that in mind and try to work from the json.

Tony that is useful for validating that the definition is a valid swagger document but as far as I can see you cant validate requests against the definition using it. Apigee 127/swagger-tools looks closer to what I want but its all node based.  I guess I will start writing my own.

Peng Zhang

unread,
Nov 23, 2015, 8:40:58 AM11/23/15
to Swagger
Laurie, I have the same need to validate request against yaml/json files. Have you written any code? Which language do you use and can you share some experience? I am using Scala and Play. So, one idea is to generate BodyParser from yaml file. Thanks!

Tony Tam

unread,
Nov 23, 2015, 12:10:10 PM11/23/15
to swagger-sw...@googlegroups.com
New to the swagger inflector project is request/response validation:


It uses the same schema that defines your api. 

Peng Zhang

unread,
Nov 24, 2015, 3:48:18 AM11/24/15
to Swagger
Thanks Tony! This project looks very good, and I will try it. A couple of questions:
- my project already has yaml file, so is it possible to integrate swagger-infector as input/payload validation based on the swagger.yaml? For new project, I can use swagger-infector from beginning
- my API has many GET methods with query parameters, does swagger-infector support the validation of GET query parameters? 
- my project is Scala/play, btw
 
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

tony tam

unread,
Nov 24, 2015, 4:10:07 PM11/24/15
to swagger-sw...@googlegroups.com
Hi, to answer your questions:

* Yes, you can just reuse your existing YAML file.  No need to use the default one, lives in src/main/swagger
* Yes, it will validate the query parameters based on your swagger definition
* You can call any controller function you like, and I see no reason why scala won’t work


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Peng Zhang

unread,
Nov 25, 2015, 7:47:35 AM11/25/15
to Swagger
Thanks Tony!
I was able to run the example and validate in browser pointing localhost:8080. However, when I tried to do this with my own yaml, I had some problem. Here is what I did so far
- Put my own swagger.yaml file in src/main/swagger, I was able to run editor.sh and saw no error
- Then, I thought next step is to generate source files from yaml file, (I did not change any configuration in pom.xml, inflector.yaml), however, running mvn package or mvn generate-sources did not generate any sources in src/gen/java, is there anything wrong with codegen? or whats the step to do? Thanks!

tony tam

unread,
Nov 27, 2015, 9:42:03 PM11/27/15
to Swagger
Hi Peng, please see in-line


On Wednesday, November 25, 2015 at 4:47:35 AM UTC-8, Peng Zhang wrote:
Thanks Tony!
I was able to run the example and validate in browser pointing localhost:8080. However, when I tried to do this with my own yaml, I had some problem. Here is what I did so far
- Put my own swagger.yaml file in src/main/swagger, I was able to run editor.sh and saw no error
- Then, I thought next step is to generate source files from yaml file, (I did not change any configuration in pom.xml, inflector.yaml), however, running mvn package or mvn generate-sources did not generate any sources in src/gen/java, is there anything wrong with codegen? or whats the step to do? Thanks!


Take a look at the readme for the inflector project.  There is really no codegen required, you can use the /debug.json path to see what methods are implemented, and where.
Reply all
Reply to author
Forward
0 new messages