-
Notifications
You must be signed in to change notification settings - Fork 477
Add OpenAPI 3.0/3.1 support with backward-compatible architecture #969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
numbata
wants to merge
45
commits into
ruby-grape:master
Choose a base branch
from
numbata:oas3_new_attempt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
9b2f763
Add API Model Layer for OpenAPI 3.x support
numbata 4a0ce3d
Add Model Builder layer for converting routes to API Model
numbata 7705397
Add Exporters for Swagger 2.0, OpenAPI 3.0, and 3.1 output
numbata 27b2e9a
Integrate OpenAPI 3.x support with configuration option
numbata 7d8ef4e
Fix requestBody content generation for OAS 3.x
numbata efbe285
Document openapi_version configuration option
numbata 2c05c92
Add comprehensive OAS 3.x tests and fix security/empty array handling
numbata e5c379c
Add formData to requestBody conversion for OAS 3.x
numbata 2a6bc9f
Fix reference_entity_spec.rb test expectations
numbata 1d9430b
Add OAS 3.1 specific features
numbata 7a23a64
Fix test expectations for issues 539 and 962
numbata bcb2e4f
Add comprehensive OpenAPI 3.x integration tests
numbata 62f840f
Add OAS 3.1 configuration options: jsonSchemaDialect and webhooks
numbata ec6d3f2
Document OAS 3.1 configuration options in README
numbata 136ab3c
Add OAS 3.1 type: null support
numbata a84e316
Add OAS 3.0 requestBody param_type: body tests
numbata 0bafe1d
Add OAS 3.0 params array spec
numbata 913c4f0
Add OAS 3.0 type format spec and fix JSON type mapping
numbata 45b310b
Fix rubocop offenses on added files
numbata 5c87e80
Refactor complex methods to fix Metrics rubocop offenses
numbata 81b64a7
Add OAS3 tests for nested body params, response models, and compositi…
numbata dcf563e
Add P2 OAS3 features: additional_properties, discriminator, links, ca…
numbata d59808d
Add nullable handling and port additional OAS3 specs
numbata f567358
Add OAS3 implementation documentation
numbata 9eef83a
Enable DirectSpecBuilder for OAS3 generation with nested entity support
numbata f3f8fee
Update OAS3 docs to document DirectSpecBuilder as primary builder
numbata 7712ecb
Rename ApiModel → OpenAPI and ModelBuilder → OpenAPI::Builder
numbata 3cbcd27
Add Dry-types/Dry-validation to future enhancements
numbata 62eda91
Clarify Dry support is a separate model parser gem
numbata 37192b7
Remove outdated limitation comments from FromRoutes
numbata d050ded
Fix rubocop offenses in OpenAPI module
numbata 5803ab5
Refactor OAS3 builders to reduce complexity and class length
numbata 9c62392
Fix hidden properties appearing in required array
numbata 6653e5a
Refactor DocMethods and fix consumes string handling
numbata 66b96bb
Add Grape 3.1 compatibility for route.attributes and Route constructor
numbata 2591d8b
Fix OAS 3.0 type format test to expect correct types
numbata 7d68c3d
Refactor to parser-agnostic architecture
numbata 01c3975
Fix RouteHelper to honor anchor: false option
numbata d5e0e27
Replace Set with Array in build_tags to avoid missing require
numbata 831deab
Improve code quality in OpenAPI builders
numbata e05a10e
Refactor OpenAPI builder structure and consolidate docs
numbata 0e6c500
Fix nullable extension for Swagger 2.0 compatibility
numbata 0384052
Remove license identifier from Swagger 2.0 output
numbata 9909b17
Add backward compatibility for x: { nullable: true } in OAS 3.x
numbata f4c2f25
Extract builder sections into separate concern modules
numbata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # OpenAPI 3.0/3.1 Support | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```ruby | ||
| # Swagger 2.0 (default, unchanged) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. letting it as default, no option adding for existend apps, is understandable, |
||
| add_swagger_documentation | ||
|
|
||
| # OpenAPI 3.0 | ||
| add_swagger_documentation(openapi_version: '3.0') | ||
|
|
||
| # OpenAPI 3.1 | ||
| add_swagger_documentation(openapi_version: '3.1') | ||
| ``` | ||
|
|
||
| ## Configuration Options | ||
|
|
||
| ```ruby | ||
| add_swagger_documentation( | ||
| openapi_version: '3.1', | ||
| info: { | ||
| title: 'My API', | ||
| version: '1.0', | ||
| description: 'API description', | ||
| license: { | ||
| name: 'MIT', | ||
| url: 'https://opensource.org/licenses/MIT', | ||
| identifier: 'MIT' # OAS 3.1 only (SPDX) | ||
| } | ||
| }, | ||
| security_definitions: { | ||
| bearer: { type: 'http', scheme: 'bearer' } | ||
| }, | ||
| # OAS 3.1 specific | ||
| json_schema_dialect: 'https://json-schema.org/draft/2020-12/schema', | ||
| webhooks: { | ||
| newUser: { | ||
| post: { | ||
| summary: 'New user webhook', | ||
| requestBody: { ... }, | ||
| responses: { '200' => { description: 'OK' } } | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| ``` | ||
|
|
||
| ## Key Differences from Swagger 2.0 | ||
|
|
||
| | Aspect | Swagger 2.0 | OpenAPI 3.x | | ||
| |--------|-------------|-------------| | ||
| | Version | `swagger: '2.0'` | `openapi: '3.0.3'` / `'3.1.0'` | | ||
| | Body params | `in: body` parameter | `requestBody` object | | ||
| | Form params | `in: formData` | `requestBody` with content-type | | ||
| | File upload | `type: file` | `type: string, format: binary` | | ||
| | Schema refs | `#/definitions/X` | `#/components/schemas/X` | | ||
| | Host | `host`, `basePath`, `schemes` | `servers: [{url: "..."}]` | | ||
| | Security defs | `securityDefinitions` | `components/securitySchemes` | | ||
| | Param types | inline `type`, `format` | wrapped in `schema` | | ||
|
|
||
| ## Nullable Fields | ||
|
|
||
| ```ruby | ||
| # Recommended syntax | ||
| params do | ||
| optional :nickname, type: String, documentation: { nullable: true } | ||
| end | ||
|
|
||
| # Also supported (for backward compatibility with Swagger 2.0 code) | ||
| params do | ||
| optional :nickname, type: String, documentation: { x: { nullable: true } } | ||
| end | ||
| ``` | ||
|
|
||
| Both syntaxes produce version-appropriate output: | ||
|
|
||
| | Syntax | Swagger 2.0 | OAS 3.0 | OAS 3.1 | | ||
| |--------|-------------|---------|---------| | ||
| | `documentation: { nullable: true }` | `x-nullable: true` | `nullable: true` | `type: ["string", "null"]` | | ||
| | `documentation: { x: { nullable: true } }` | `x-nullable: true` | `nullable: true` | `type: ["string", "null"]` | | ||
|
|
||
| This means existing Swagger 2.0 code using `x: { nullable: true }` works seamlessly when switching to OAS 3.x. | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| Grape Routes | ||
| │ | ||
| ▼ | ||
| ┌─────────────────────────────┐ | ||
| │ Builder::Spec │ | ||
| │ (lib/grape-swagger/openapi)│ | ||
| └─────────────────────────────┘ | ||
| │ | ||
| ▼ | ||
| ┌─────────────────────────────┐ | ||
| │ OpenAPI Model Layer │ | ||
| │ (Document, Schema, etc.) │ | ||
| └─────────────────────────────┘ | ||
| │ | ||
| ├──────────────┬──────────────┐ | ||
| ▼ ▼ ▼ | ||
| ┌──────────┐ ┌──────────┐ ┌──────────┐ | ||
| │ Swagger2 │ │ OAS30 │ │ OAS31 │ | ||
| │ Exporter │ │ Exporter │ │ Exporter │ | ||
| └──────────┘ └──────────┘ └──────────┘ | ||
| ``` | ||
|
|
||
| ## Backward Compatibility | ||
|
|
||
| - **Default unchanged**: Without `openapi_version`, Swagger 2.0 is generated | ||
| - **All existing options work**: Same configuration for both versions | ||
| - **Model parsers unchanged**: grape-entity, representable, etc. work as before | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice to have this in a more popular place or at least a link in the README