Skip to content

Instantly share code, notes, and snippets.

@vladdancer
Created November 21, 2022 20:57
Show Gist options
  • Save vladdancer/8bc1c089830cf85e62ebd0977e69188b to your computer and use it in GitHub Desktop.
Save vladdancer/8bc1c089830cf85e62ebd0977e69188b to your computer and use it in GitHub Desktop.
Generate PicturePark PHP classes from OpenAPI spec (swagger)
#!/bin/bash -x
# Run this script from the module's root dir:
# chmod +x ./tools/generate_classes.sh && ./tools/generate_classes.sh
PP_SPEC_FILE=./tools/PictureparkSwagger.json
PP_SPEC_URL="https://api-cp-ch01.picturepark.com/docs/Rest/PictureparkSwagger.json"
if [ ! -f "$PP_SPEC_FILE" ]; then
echo "$PP_SPEC_FILE does not exist. Downloading from upstream..."
wget -O "$PP_SPEC_FILE" "$PP_SPEC_URL"
fi
# Prevent error in Jane.
echo "Fixing $PP_SPEC_FILE..."
sed_cmd='sed'
if [[ "$(uname)" == 'Darwin' ]]; then
sed_cmd='gsed'
fi
# Use sed to replace all empty descriptions.
# But better approach is parsing json with php,
# and fix only specific json objects,
# where empty description and content is application/json and schema has $ref.
#$sed_cmd -i 's/"description": null/"description": "null"/g' "$PP_SPEC_FILE"
# The API spec is huge, so let's disable cache and memory limits.
echo "Re-generating classes..."
time php -d memory_limit=-1 -d zend.enable_gc=0 ./vendor/bin/jane-openapi generate --config-file=tools/jane_config.php
<?php
/**
* @file
* tools/jane_config.php
*
* Config file for janephp tool.
*/
return [
'openapi-file' => __DIR__ . '/PictureparkSwagger.json',
'namespace' => 'Drupal\risd_pp\Picturepark\Api\Generated',
'directory' => './src/Picturepark/Api/generated',
'strict' => false,
'skip-required-fields' => false,
'skip-null-values' => false,
// We need only specified endpoints.
//'whitelisted-paths' => [
// '\/v1\/Shares\/.*$',
// '\/v1\/Contents\/.*$',
//],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment