Skip to content

Commit

Permalink
Add script to create JSON schema for react-jsonschema-form
Browse files Browse the repository at this point in the history
  • Loading branch information
elsand committed Sep 15, 2024
1 parent 14ed82a commit fccc985
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tools/extract_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

# Script to dereference and convert an OpenAPI specification to JSON Schema,
# then extract a specific schema from the JSON Schema file, and clean up temporary files.

# Usage: ./extract_schema.sh <OpenAPI_Spec_URL>

# Exit immediately if a command exits with a non-zero status
set -e

# Check if the correct number of arguments are provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <Dialogporten_OpenAPI_Spec_URL>"
exit 1
fi

# Assign arguments to variables
OPENAPI_URL="$1"
SCHEMA_NAME="CreateDialogCommand"
TARGET_FILE="creates-a-new-dialog-POST-request-schema.json"

# Define filenames and directories
OPENAPI_FILE="openapi.json"
MODIFIED_OPENAPI_FILE="openapi_modified.json"
BUNDLED_OPENAPI_FILE="openapi_dereferenced.json"
FINAL_SCHEMA_DIR="json_schema"
FINAL_SCHEMA_FILE="schema.json"

# Check for required commands
for cmd in curl jq swagger-cli openapi2jsonschema; do
if ! command -v "$cmd" &> /dev/null; then
echo "Error: '$cmd' command not found. Please install it before running this script."
exit 1
fi
done

# Step 1: Download the OpenAPI specification
echo "Downloading OpenAPI specification from $OPENAPI_URL..."
curl -sSL "$OPENAPI_URL" -o "$OPENAPI_FILE"

# Step 2: Remove the "subParties" property from "AuthorizedPartyDto"
echo "Removing 'subParties' property from 'AuthorizedPartyDto'..."
jq 'del(.components.schemas.AuthorizedPartyDto.properties.subParties)' "$OPENAPI_FILE" > "$MODIFIED_OPENAPI_FILE"

# Step 3: Dereference the modified OpenAPI specification
echo "Dereferencing OpenAPI specification..."
swagger-cli bundle --dereference "$MODIFIED_OPENAPI_FILE" --outfile "$BUNDLED_OPENAPI_FILE" --type json

echo "Dereferenced OpenAPI specification saved to $BUNDLED_OPENAPI_FILE."

# Step 4: Convert the entire dereferenced OpenAPI spec to JSON Schema and output to a directory
echo "Converting entire OpenAPI spec to JSON Schema..."
mkdir -p "$FINAL_SCHEMA_DIR"
openapi2jsonschema --clean --output "$FINAL_SCHEMA_DIR" --input "$BUNDLED_OPENAPI_FILE" >/dev/null

echo "Converted JSON Schema files saved to $FINAL_SCHEMA_DIR."

# Step 5: Find the specific schema file for the specified component
find . -name "*$TARGET_FILE" -exec mv {} $FINAL_SCHEMA_FILE.tmp \;

echo "Schema '$SCHEMA_NAME' extracted and saved to $FINAL_SCHEMA_FILE."

# Step 7: Clean up temporary files and directory
echo "Cleaning up temporary files..."
rm -rf *.json $FINAL_SCHEMA_DIR
mv $FINAL_SCHEMA_FILE.tmp $FINAL_SCHEMA_FILE

echo "Process completed successfully."

0 comments on commit fccc985

Please sign in to comment.