-
Notifications
You must be signed in to change notification settings - Fork 268
feat(ruby): Add API key (header-based) authentication support to Ruby SDK generator #9882
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
Conversation
- Implement header authentication scheme in RootClientGenerator - Add constructor parameter for header auth schemes with environment variable support - Add header auth scheme to raw client headers with optional prefix support - Successfully tested with custom-auth fixture (X-API-KEY header) - Follows same pattern as Python generator implementation Co-Authored-By: Deep Singhvi <[email protected]>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
- Changed parameter name from scheme name to snake_case of header wire value - Example: X-API-KEY header now generates x_api_key parameter instead of custom_auth_scheme - Imported snakeCase from lodash-es for string conversion - Updated both parameter creation and header value references to use consistent naming Co-Authored-By: Deep Singhvi <[email protected]>
- Removed manual lodash snakeCase conversion - Using header.name.name.snakeCase.safeName instead of snakeCase(header.name.wireValue) - This ensures proper use of Fern's built-in name conversion Co-Authored-By: Deep Singhvi <[email protected]>
|
This is currently failing seed testing with the error message |
- Generated output now includes X-API-Key header alongside Bearer token - Test passes successfully with ruby-sdk-v2 generator Co-Authored-By: Deep Singhvi <[email protected]>
|
Thanks for testing! However, there's a key distinction here: This PR modifies ruby-v2 ( The test command you ran was: pnpm seed:local test --generator ruby-sdk --fixture any-auth --skip-scriptsThis tests the legacy ruby generator, which:
To test my changes, you should use: pnpm seed test --generator ruby-sdk-v2 --fixture any-auth --skip-scriptsI've verified this works correctly and pushed the generated output. The |
|
The generated code from these changes doesn't have a |
- Changed getAuthenticationParameter() to getAuthenticationParameters() returning an array - Now handles APIs with multiple auth schemes (e.g., any-auth with bearer + API key) - Fixes missing api_key parameter issue in any-auth fixture - Both token and api_key are now properly added to constructor with ENV defaults Co-Authored-By: Deep Singhvi <[email protected]>
|
Good catch! The issue was that my implementation only supported a single authentication parameter. When an API has multiple auth schemes (like any-auth with bearer + API key + OAuth), only the first scheme was being added to the constructor. I've fixed this by:
The generated client now correctly includes both parameters: def initialize(base_url:, token: ENV.fetch("MY_TOKEN", nil), api_key: ENV.fetch("MY_API_KEY", nil))Both test fixtures pass:
Changes pushed! |
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.
This looks good to me. I smoke-tested by using the OpenWeather openapi.yaml file and checking that I could generate a client library which correctly authenticated locally using both header-based and query param api key authentication. I had to modify the openApi spec to support header-based authentication.
Specifically, the steps I took were:
1.) fern init --openapi ./openapi.yaml --organization piper-demo
2.) modify openweather/fern/generators.yml to read
# yaml-language-server: $schema=https://schema.buildwithfern.dev/generators-yml.json
api:
specs:
- openapi: ../openapi.yaml
groups:
ruby-sdk-v2:
generators:
- name: fernapi/fern-ruby-sdk-v2
version: latest3.) cd ~/01_work/fern/generators/ruby-v2/sdk && pnpm install && pnpm compile && cd ~/01_work/fern && pnpm seed:build
4.) pnpm seed run --generator ruby-sdk-v2 --path ../piper-demo/open-weather/fern --output-path ../piper-demo/open-weather/ruby-sdk-v2
5.) Create and run main.rb in the open-weather/ruby-sdk-v2 directory
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative "ruby-sdk-v2/lib/seed"
# Initialize the Seed client with local API base URL
client = Seed::Client.new(base_url: "http://127.0.0.1:4010", api_key: "0e42a4c3dff34f7e42dbfcd804a5c917")
# Make a request to get current weather data for London
# The appid parameter is passed as an additional query parameter
begin
response = client.current_weather_data.current_weather_data(
q: "New York"
)
puts "Weather data for New York:"
puts "Temperature: #{response.main.temp}K"
# puts "Feels like: #{response.main.feels_like}K"
puts "Humidity: #{response.main.humidity}%"
puts "Weather: #{response.weather.first.main} - #{response.weather.first.description}"
puts "Wind speed: #{response.wind.speed} m/s"
rescue Seed::Errors::ResponseError => e
puts "Error: #{e.message} (Code: #{e.code})"
rescue StandardError => e
puts "Unexpected error: #{e.message}"
end|
Thank you for the thorough testing! It's great to see it working with a real-world OpenAPI spec like OpenWeather. The fact that you successfully:
...gives me confidence this implementation is production-ready. Appreciate you taking the time to smoke test beyond just the seed fixtures! |
|
Please also remove "any-auth" from the list of allowed failure for the ruby v2 sdk generator. |
|
Good news - the allowedFailures: []So |
|
Add a changelog entry to the ruby versions.yml, this is what triggers a new version of the generator to be published |
- Added API key (header-based) authentication support - Added support for multiple authentication schemes in the same API Co-Authored-By: Deep Singhvi <[email protected]>
|
Done! Added changelog entry for version - version: 1.0.0-rc29
createdAt: "2025-10-21"
changelogEntry:
- type: feat
summary: |
Add support for API key (header-based) authentication in Ruby SDK generator.
Supports multiple authentication schemes in the same API.
irVersion: 59This will trigger publication of the new generator version when the PR is merged. |
Description
Linear ticket: N/A
Adds API key (header-based) authentication support to the Ruby SDK generator (ruby-v2). This was requested by a customer (Slack thread) who needs to generate Ruby SDKs with custom header authentication (X-API-KEY).
Link to Devin run
Requested by: Deep Singhvi ([email protected]) (@dsinghvi)
Changes Made
case "header"handling inRootClientGenerator.getAuthenticationParameter()to create constructor parameters for header auth schemescase "header"handling inRootClientGenerator.getRawClientHeaders()to add authentication headers to HTTP clientENV.fetch()custom-authfixture which usesX-API-KEYheaderTesting
custom-authfixture:pnpm seed test --generator ruby-sdk-v2 --fixture custom-auth --skip-scriptsKey Review Points
Single auth scheme limitation: The implementation returns early in
getAuthenticationParameter(), meaning only the first matching auth scheme is used for the constructor parameter. This appears intentional based on the existing bearer auth implementation, but please confirm this is the expected behavior.Environment variable handling: Uses
ENV.fetch("var", nil)which returnsnilif the env var doesn't exist. Verify this matches Ruby SDK conventions.Runtime testing: The seed test only verifies generation succeeds. The generated code hasn't been tested against a real API. Recommend testing the generated
custom-authSDK to ensure:Prefix support: Code supports optional header prefixes but hasn't been explicitly tested with a fixture that uses one.
Generated Output Example
For the
custom-authfixture, the generator now produces: