We have a Swagger file for our API, but in our API documentation, we want to show valid responses for each endpoint. I coded up this little recursive function in PHP which takes our Swagger file and spits out a valid response.
I use it as follows, for each individual endpoint:
// $swagger is the entire swagger file $swagger = file_get_contents('/path-to-swagger.json');
For the below code within our PHP template,
$endpoint can be assumed to be a single endpoint with method (e.g. a single POST request to a URI) such that json_decode() was used to convert it from the Swagger file to a PHP array.
<pre><?= json_encode(swagger_to_model( $swagger['definitions'][str_replace('#/definitions/','', $endpoint['responses']['200']['schema']['$ref'])]['properties'], $swagger['definitions'], 0, $definitions ), JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES) ?></pre>