Skip to content

Commit

Permalink
Added Python and Ruby spec validators.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <dblock@amazon.com>
  • Loading branch information
dblock committed Oct 25, 2024
1 parent 1db1840 commit 8cf1c31
Show file tree
Hide file tree
Showing 12 changed files with 592 additions and 2 deletions.
1 change: 1 addition & 0 deletions .cspell
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Oversample
performanceanalyzer
permissionsinfo
pipefail
pipenv
preconfigure
preconfigured
prefilter
Expand Down
File renamed without changes.
34 changes: 34 additions & 0 deletions .github/workflows/validate-spec-py.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Validate Spec (Python)

on: [pull_request,push]

jobs:
validate-spec-py:
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Build
run: npm ci && npm run merge

- name: Set Up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install Dependencies
working-directory: tools/src/validate-spec-py
run: |
pip install --user pipenv
pipenv install
- name: Validate Spec
working-directory: tools/src/validate-spec-py
run: |
pipenv run python validate.py ../../../build/opensearch-openapi.yaml
28 changes: 28 additions & 0 deletions .github/workflows/validate-spec-ruby.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Validate Spec (Ruby)

on: [pull_request,push]

jobs:
validate-spec-ruby:
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/tools/src/validate-spec-ruby/Gemfile
steps:
- name: Checkout the repo
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Build
run: npm ci && npm run merge

- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
bundler-cache: true

- name: Validate Spec
run: |
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Added API spec for `adjust_pure_negative` for bool queries ([#641](https://github.com/opensearch-project/opensearch-api-specification/pull/641))
- Added a spec style checker [#620](https://github.com/opensearch-project/opensearch-api-specification/pull/620).
- Added `remote_store` to node `Stats` ([#643](https://github.com/opensearch-project/opensearch-api-specification/pull/643))
- Added Python and Ruby spec validators ([#646](https://github.com/opensearch-project/opensearch-api-specification/pull/646))

### Changed

Expand Down
9 changes: 7 additions & 2 deletions DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
- [Comment on PR](#comment-on-pr)
- [Test Tools (Unit)](#test-tools-unit)
- [Test Tools (Integration)](#test-tools-integration)
- [Validate Spec](#validate-spec)
- [Validate Spec (Lint)](#validate-spec-lint)
- [Validate Spec (Python)](#validate-spec-python)
<!-- TOC -->

# Developer Guide
Expand Down Expand Up @@ -375,6 +376,10 @@ This workflow runs on PRs to invoke the [tools' unit tests](tools/tests), upload

This workflow runs on PRs to invoke the [tools' integration tests](tools/tests) that require a running instance of OpenSearch to ensure there are no breakages in behavior.

### [Validate Spec](.github/workflows/validate-spec.yml)
### [Validate Spec (Lint)](.github/workflows/validate-spec-lint.yml)

This workflow runs on PRs to invoke the [spec linter](#spec-linter) and ensure the multi-file spec is correct and follows the design guidelines.

### [Validate Spec (Python)](.github/workflows/validate-spec-py.yml)

This workflow runs on PRs to invoke the [Python openapi-spec-validator](https://pypi.org/project/openapi-spec-validator/) to ensure that the resulting spec can be loaded by Python tools.
12 changes: 12 additions & 0 deletions tools/src/validate-spec-py/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
openapi_spec_validator = "*"

[dev-packages]

[requires]
python_version = "3"
455 changes: 455 additions & 0 deletions tools/src/validate-spec-py/Pipfile.lock

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions tools/src/validate-spec-py/validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import sys

from openapi_spec_validator import validate
from openapi_spec_validator.readers import read_from_filename
from openapi_spec_validator.validation.exceptions import OpenAPIValidationError

if len(sys.argv) < 2:
print("syntax: validate.py [spec]")
exit(1)

spec = sys.argv[1]
print(f'Validating {spec} ...')

spec_dict, base_uri = read_from_filename(spec)

try:
validate(spec_dict)
except OpenAPIValidationError as err:
print(err)
exit(2)
3 changes: 3 additions & 0 deletions tools/src/validate-spec-ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem 'openapi3_parser'
16 changes: 16 additions & 0 deletions tools/src/validate-spec-ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
GEM
remote: https://rubygems.org/
specs:
commonmarker (0.23.10)
openapi3_parser (0.9.2)
commonmarker (~> 0.17)

PLATFORMS
arm64-darwin-21
ruby

DEPENDENCIES
openapi3_parser

BUNDLED WITH
2.5.2
15 changes: 15 additions & 0 deletions tools/src/validate-spec-ruby/validate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'openapi3_parser'

raise "syntax: validate [spec]" unless ARGV.length >= 1

spec = ARGV[0]
puts "Validating #{spec} ..."

parsed_spec = Openapi3Parser.load_file(spec)
exit 0 if parsed_spec.valid?

parsed_spec.errors.each do |error|
puts "#{error.context}: #{error}"
end

exit 1

0 comments on commit 8cf1c31

Please sign in to comment.