This guide aims to provide the necessary instructions to verify OpenAPI interfaces to ensure they adhere to the rules of the Technical Interoperability Model for the Italian Public Administration (PA).
For more information on the model, you can download the document here and open it with Adobe Reader to view the attachments. The attachment with the rules is titled Implementation Recommendations.
To ensure an OpenAPI interface is compliant with the requirements of the Model for Technical Interoperability for the PA, the verification must return zero errors after checking with the rules spectral-modi.yml, also known as Italian Guidelines.
- 🌐 First Method: Website
- 🖥️ Second Method: IDE Extension
- 💻 Third Method: Command Line (CLI)
- ⚙️ Fourth Method: GitHub Action
Italian OpenAPI Checker is a web app that allows you to verify OpenAPI interfaces directly from the browser, identifying all the present errors.
To perform the verification, simply:
- Open an OpenAPI specification file in a text editor
- Copy and paste the content into the site's box.
- Select the Italian Guidelines ruleset if not already selected.
- Click on Check.
- Mandatory to correct the errors marked in red.
- Optionally correct the warnings marked in orange.
- Gray suggestions are useful and also optional.
Spectral, the tool behind the website, is also available as an extension for Visual Studio Code and other IDEs.
What is Spectral?
Spectral is an open-source linting tool designed for the verification, formatting, and cleaning of JSON and YAML files. It is particularly useful for verifying OpenAPI specifications, ensuring they adhere to defined standards and best practices.
For more information, visit the official Spectral website.
The extension can be downloaded from the integrated store in your IDE, such as Visual Studio Code. The extension for the latter is available here.
From the extension settings in the IDE, you need to configure the rules file. You can choose between two options:
- Have the rules file locally on your computer and enter the respective path.
- Enter the remote URL (e.g., GitHub) for the rules file, such as the Italian Guidelines spectral-modi.yml.
For other rules files, refer to the official repository.
The extension reports errors and warnings detected in real-time in the open OpenAPI file (file .yaml, .yml, or .json).
Spectral can also be used from the command line (CLI) for bulk verification of OpenAPIs.
On GitHub, at this link, there is a guide for installing Spectral locally via npm (more info here) and yarn (more info here).
To verify an OpenAPI file, use the following command in the terminal:
spectral lint file_path_openapi\
–e utf8\
–D\
–f json\
–o file_path_output\
–r file_path_rules\
-v
file_path_openapi
: the path to the file containing the OpenAPI interface to be verified;file_path_output
: the path to the JSON output file that will contain all identified errors;file_path_rules
: the path to the rules file for verification, also remote (such as the Italian Guidelines, spectral-modi.yml).
By removing the –D
parameter, the tool will also output warnings and suggestions to be even more compliant with interoperability best practices and the OpenAPI 3 standard.
The JSON output file contains a list of all instances where the rules have been violated.
Alternatively, you can avoid installing Spectral by using Docker:
docker run\
--rm\
--entrypoint=sh\
-v $(pwd)/api:/locale\
stoplight/spectral:5.9.1\
-c "spectral lint /locale/file_openapi –e utf8 –D –f json –o percorso_file_output –r percorso_file_regole -v"
A GitHub Action has been created that is easily integrable and allows verifying OpenAPI interfaces with Spectral for every push and pull request on a repository. This solution automates the verification process, ensuring that every change is checked in accordance with the established rules.
The action can be further customized by modifying, for example, the folder where to look for OpenAPIs and the branches subject to the linter. This way, the action can be adapted to the specific needs of your project.
At the link resources/github-action.yml, there is a fully working example of a GitHub Action. The Action always downloads the latest published ruleset, ensuring that the verification is always up-to-date with the latest available rules. At the end of the execution, at the bottom of the execution page, there is an archive with the results of the Spectral analysis on the potential OpenAPI files.
We hope this guide is useful to ensure your OpenAPI interfaces meet the required standards for technical interoperability in the Italian Public Administration. Happy verifying!