How Do I Test That My Swagger Documentation Is Val

2019-07-30 09:01发布

问题:

It is very easy for Swagger annotations to drift away from the actual code.

E.g.

I could change the return from Response.ok to Response.created and easily forget to update the annotations from 200 to 201.

Or I could change what is valid as null or not null and the end user will get confusing rejection messages even though their payload matches exactly what the documentation says.

How do I test that my swagger documentation matches what my code is doing?

  • A test that uses selenium and phantomjs to parse the swagger-ui web page?
  • Reflection to parse the annotations?
  • Get the generated swagger.json and parse it?
  • Something else?

Unfortunately I can't switch to Spring REST Docs.

回答1:

You can check the following library which contains a couple of projects that deal with API validation. I think the one you're looking for is called "swagger-request-validator-mockmvc", but you might find the other libraries useful as well. Good luck.