-
Notifications
You must be signed in to change notification settings - Fork 166
feat: enhance openapi docs for agents #1674
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
Open
3kh0
wants to merge
1
commit into
main
Choose a base branch
from
openapi
base: main
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Publishes the OpenAPI description of Hackatime's public API at the | ||
| # conventional, machine-discoverable URLs (`/openapi.json`, `/openapi.yaml` | ||
| # and their `/api` aliases). The Scalar reference at `/api-docs` is the human | ||
| # entry point; these endpoints are what API clients, SDK generators and agents | ||
| # fetch. Both formats render the same document that `rswag` generates into | ||
| # `swagger/v1/swagger.yaml`, so there is a single source of truth. | ||
| class OpenapiController < ApplicationController | ||
| SPEC_PATH = Rails.root.join("swagger", "v1", "swagger.yaml").freeze | ||
| CACHE_MAX_AGE = 1.hour | ||
|
|
||
| class << self | ||
| def json_document | ||
| @json_document ||= "#{JSON.pretty_generate(parsed_document)}\n" | ||
| end | ||
|
|
||
| def yaml_document | ||
| @yaml_document ||= SPEC_PATH.read | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def parsed_document | ||
| YAML.safe_load(yaml_document, aliases: true) | ||
| end | ||
| end | ||
|
|
||
| def show_json | ||
| render_spec self.class.json_document, "application/json" | ||
| end | ||
|
|
||
| def show_yaml | ||
| # RFC 9512 registers `application/yaml` as the media type for YAML. | ||
| render_spec self.class.yaml_document, "application/yaml" | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def render_spec(body, content_type) | ||
| # Overrides ApplicationController's `no-store` default: the document is | ||
| # public, identical for every caller and only changes on deploy. | ||
| response.headers["Cache-Control"] = "public, max-age=#{CACHE_MAX_AGE.to_i}" | ||
| # Browser-based API consoles need to fetch the spec cross-origin. | ||
| response.headers["Access-Control-Allow-Origin"] = "*" | ||
|
|
||
| render plain: body, content_type: content_type | ||
| end | ||
| end |
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
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,57 @@ | ||
| require 'swagger_helper' | ||
|
|
||
| RSpec.describe 'Api::Openapi', type: :request do | ||
| path '/openapi.json' do | ||
| get('Fetch the OpenAPI description of this API') do | ||
| tags 'Discovery' | ||
| description <<~DESC | ||
| Returns this document. No authentication is required. | ||
|
|
||
| `/api/openapi.json` is an alias for the same resource, and the YAML | ||
| representation is available at `/openapi.yaml` (aliased at | ||
| `/api/openapi.yaml`). | ||
| DESC | ||
| security [] | ||
| produces 'application/json' | ||
|
|
||
| response(200, 'successful') do | ||
| schema type: :object, | ||
| description: 'An OpenAPI 3.0 document describing the Hackatime API.', | ||
| properties: { | ||
| openapi: { type: :string, example: '3.0.1' }, | ||
| info: { type: :object }, | ||
| paths: { type: :object }, | ||
| components: { type: :object }, | ||
| servers: { type: :array, items: { type: :object } } | ||
| }, | ||
| required: %w[openapi info paths] | ||
|
|
||
| run_test! do |response| | ||
| body = JSON.parse(response.body) | ||
| expect(body['openapi']).to eq('3.0.1') | ||
| expect(body.dig('info', 'title')).to eq('Hackatime API') | ||
| expect(body['paths']).to be_a(Hash) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| path '/openapi.yaml' do | ||
| get('Fetch the OpenAPI description of this API as YAML') do | ||
| tags 'Discovery' | ||
| description 'The YAML representation of `/openapi.json`. No authentication is required.' | ||
| security [] | ||
| produces 'application/yaml' | ||
|
|
||
| response(200, 'successful') do | ||
| schema type: :string, description: 'An OpenAPI 3.0 document, serialised as YAML.' | ||
|
|
||
| run_test! do |response| | ||
| body = YAML.safe_load(response.body, aliases: true) | ||
| expect(body['openapi']).to eq('3.0.1') | ||
| expect(body.dig('info', 'title')).to eq('Hackatime API') | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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
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.
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.
When a generated client selects
ApiKeyAuthfor one of these five stats operations, it sends?api_key=..., butStatsController#set_useraccepts API keys only from the Bearer header. The query key is ignored, causing private-user requests to return 403 ormylookups to return 404 despite following the published authentication contract; removeApiKeyAuthfrom these declarations or update the shared authentication path to accept query keys.Prompt To Fix With AI