An API Validation Aggravation
Tags: api, openapi, opensource, swagger
Published: 2022-08-25 09:48:00

## API specification validation

API specification validation is the process of verifying that an API meets all the requirements specified in its documentation. This includes making sure that the API responds correctly to all requests, that it gives back the right data, and that it doesn't give back any data that wasn't expected.

It is an important part of the development process as it ensures that the API behaves as expected and that it meets all the requirements of its users. It is also a good way to catch any errors or bugs in the API before they are released to the public.

API specification validation can be done manually or automatically. By running a series of tests against an API and looking at the results, automated testing tools can make it easier to check if it meets its requirements.

## Hand testing API

An API can also be tested by hand, but this can take more time and may require more knowledge of the API and how it works.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yoncunilo33dkl28w7ei.png)

API validation is an important part of developing and releasing a new API. It helps to ensure that the API behaves as expected and that it meets all the requirements of its users. Validating an API can be made easier with automated testing tools and [CI/CD integrated validation](https://github.com/blst-security/cherrybomb) 💡 tools, but it can also be done by hand.

When it comes to documenting APIs, there are few tools as widely used and supported as Swagger. In fact, Swagger has become so popular that it is now known as the de facto standard for API documentation.

## The swagger flaws

But Swagger is not without its flaws, and there’s a new kid on the block that promises to address some of the shortcomings of Swagger 2.0: OpenAPI 3.0.

**In this article, we’ll also take a look at the key differences between Swagger 2.0 and OpenAPI 3.0.**

OpenAPI 3.0 is the latest version of the OpenAPI Specification, and it’s not backwards compatible with Swagger 2.0.

One of the most significant changes in OpenAPI 3.0 is the addition of a new required field: info. This field takes the place of the old swagger.info field. It has information about the API, like the title, description, and how to get in touch with the developers.

**Here are some of the changes.**

- "Host" and "BasePath" combine to form a "Target". In Swagger 2, you declare a single host and base path for all of your resources and operations.
- The parameters are defined at the top level of the resources. In Swagger 2, you define parameters in the operations.
- You can define multiple paths for a resource. In Swagger 2, you can only define one path per resource.
- You can define multiple operations for a resource. In Swagger 2, you can only define one operation per resource.
- You can define multiple responses for an operation. In Swagger 2, you can only define one response per operation.

Another notable change is the removal of the consume and produce fields in favor of a new field called requestBody. This new field is used to describe the request body of an API operation.

In OpenAPI 3.0, the term "schema" has been replaced with the term "media type". This change was made to be more consistent with the way media types are used in HTTP.

## Significant change in OpenAPI 3.0

Perhaps the most significant change in OpenAPI 3.0 is the addition of the Components object. This object contains a collection of reusable objects that can be used in the specification. Examples of objects that can be stored in the Components object include schemas, responses, parameters, and examples.

One of the main goals of OpenAPI 3.0 is to be more easily readable and understandable by humans. To that end, the specification includes a new feature called links. Links are used to describe relationships between resources.

Another goal of OpenAPI 3.0 is to make it easier for tools to generate code and documentation from the specification. To that end, OpenAPI 3.0 includes the concept of callbacks. Callbacks are functions that the API provider can use when certain events happen.

Overall, OpenAPI 3.0 is a major evolution of the OpenAPI Specification. Even though it doesn't work with Swagger 2.0, it fixes many of the problems with its predecessor. 

## Not everything is fairytale

There are a few reasons why it might not be possible to convert a Swagger file to OpenAPI 3.0 specification:

1. The Swagger file might be using features that are not yet supported in OpenAPI 3.0. For example, Swagger 2.0 supports arrays and objects as data types, but these are not yet supported in OpenAPI 3.0.
2. The Swagger file might be using features that are no longer supported in OpenAPI 3.0. For example, Swagger 2.0 supported the "any" data type, but this has been replaced with the more specific "oneOf" in OpenAPI 3.0.
3. The Swagger file might be using deprecated features that have been replaced in OpenAPI 3.0. For example, the "paths" object in Swagger 2.0 has been replaced by the "paths" and "servers" objects in OpenAPI 3.0.
4. The Swagger file might be using a different file format than OpenAPI 3.0. Swagger 2.0 used the "swagger.json" or "swagger.yaml" format, while OpenAPI 3.0 uses the "openapi.json" or "openapi.yaml" format.
5. The Swagger file might be using a different version of the Swagger specification than OpenAPI 3.0. For example, Swagger 2.0 is based on the Swagger specification version 2.0, while OpenAPI 3.0 is based on the Swagger specification version 3.0.0.

## Conclusion

Even if all of these factors are accounted for, it might still not be possible to convert a Swagger file to OpenAPI 3.0 due to incompatibilities between the two specifications. Swagger 2.0 and OpenAPI 3.0 are both based on the same underlying data model, but there are a number of subtle differences between the two specifications. These differences can lead to problems when converting files from one format to the other.

Don't forget to visit my [API Security](https://apimike.com) website