#openapi #hurl #swagger #api-testing #testing #json-file #yaml

app openapi-to-hurl

A tool to create Hurl files (https://hurl.dev/) from openapi specs

4 stable releases

1.2.1 Aug 11, 2024
1.2.0 Jul 28, 2024
1.1.0 Jul 16, 2024
1.0.0 Feb 23, 2024

#506 in Command line utilities

Download history 104/week @ 2024-07-23 29/week @ 2024-07-30 91/week @ 2024-08-06 23/week @ 2024-08-13 31/week @ 2024-09-10 17/week @ 2024-09-17 11/week @ 2024-09-24 20/week @ 2024-10-01

214 downloads per month

MIT license

125KB
3K SLoC

Open API to Hurl

A tool to create Hurl files (https://hurl.dev/) from Open API specifications.

Installation

Cargo

cargo install openapi-to-hurl

Brew

brew tap ethancarlsson/openapi-to-hurl
brew install openapi-to-hurl

NOTE: All binaries are for MacOS, Linux users will need to install using Cargo.

Usage

openapi-to-hurl accepts the path to an Open API 3 specification, produces hurl requests and writes them to stdout.

# Spec can be a JSON file
% openapi-to-hurl ./openapi.json 
# or a YAML file
% openapi-to-hurl ./openapi.yaml

openapi-to-hurl can also accept the specification from stdin

% cat ./openapi.json | openapi-to-hurl
# Or more usefully
% some-tool-generating-specs | openapi-to-hurl

stdout can then be passed into hurl

% cat ./openapi.json | openapi-to-hurl | hurl --variable host=https://example.com

NOTE: "host" is created as a variable by default. This makes it easier to switch hosts between local, staging and production, but it also means that "host" will need to be passed as a variable to hurl.

Producing .hurl Files

One of the main motivations for this tool is to make it easier to start exploring a new API with hurl. For this, it's more convenient that we separate each hurl request into a different file.

% openapi-to-hurl test_files/pet_store.json --out-dir test_hurl_files 
INFO Created or updated 4 hurl files in test_hurl_files
% ls test_hurl_files
addPet.hurl            listPets.hurl          petstore.swagger.io_v1 showPetById.hurl       updatePet.hurl

You can then start running these files with hurl via the CLI.

% hurl test_hurl_files/*.hurl --variables-file=test_hurl_files/petstore.swagger.io_v1

Or you can explore it using a plugin an editor plugin for Neovim or VSCode

Test Generation

Test generation is another very clear use case for this tool.

We can generate assertions to go with the .hurl file like this

% openapi-to-hurl test_files/pet_store.json --validation body
POST {{host}}/pets
{
  "id": 3,
  "inner": {
    "test": "string"
  },
  "name": "string",
  "photo_urls": [
    "https://example.com/img.png",
    "https://example.com/img2.png"
  ],
  "tag": "string"
}

HTTP *
[Asserts]

status < 400
jsonpath "$" isCollection
jsonpath "$.id" isInteger
jsonpath "$.inner" isCollection
jsonpath "$.inner.test" isString
jsonpath "$.name" isString
jsonpath "$.photo_urls" isCollection

Running the following commands will test the responses for the entire API.

openapi-to-hurl test_files/pet_store.json --validation body | hurl --variable host=http://petstore.swagger.io/v1

However, hurl lacks tools for creating assertions on where a property can have multiple types. So whenever this tool detects a property that is not required, is nullable, or can have multiple types for any other reason, it ignores the property.

This means that the command above might be useful for detecting when the API does NOT match the specification, but it wouldn't be able to detect that the API does match the specification.

This means this tool is best used as a starter for tests that are then manually edited afterwards.

openapi-to-hurl test_files/pet_store.json --validation body -o output/directory

Limitations

openapi-to-hurl only works with JSON and plain-text content types.

Changelog

Changelog available at: https://github.com/ethancarlsson/openapi-to-hurl/blob/master/CHANGELOG.md

Documentation

Online man page available at: https://ethancarlsson.github.io/openapi-to-hurl/

Licence

The MIT License (MIT)

Dependencies

~12–27MB
~372K SLoC