Repository navigation
fix(ruby): accept acronyms in module name validation #152
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
Merged
+150
−1
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c08cb54
fix(ruby): accept acronyms in module name validation
2ynn 7f291ec
test(ruby): move module validation test next to GetLanguageConfigFields
2ynn fcffa1c
test(ruby): use templates package in module validation test
2ynn dfbc607
chore: check config field validation patterns compile in Go
2ynn 8f6797e
test: name validation regex tests after what they check
2ynn 74d506c
test: declare validation regex examples next to config fields
2ynn dfc5455
test: reject unknown keys in config field definitions
2ynn 7fbe8a7
test: fail when a config field has validation examples but no pattern
2ynn a05f7f1
Merge branch 'main' into fix/ruby/module-name-acronyms
2ynn 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,10 @@ | ||
| id: 1793024465-e6de07b9 | ||
| features: | ||
| - core | ||
| targets: | ||
| - ruby | ||
| type: fix | ||
| bump: patch | ||
| description: accept acronyms such as SDK or API in the module name | ||
| author: 2ynn | ||
| date: "2026-10-26" |
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,124 @@ | ||
| package templates | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/mitchellh/mapstructure" | ||
| "github.com/speakeasy-api/openapi-generation/v2/internal/executor" | ||
| "github.com/speakeasy-api/openapi-generation/v2/internal/types" | ||
| config "github.com/speakeasy-api/sdk-gen-config" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // goja's RegExp.source emits an unescaped "/" as the Unicode escape below, which the CLI rewrites before compiling the pattern with Go's regexp. | ||
| const escapedSlash = `\u002f` | ||
|
|
||
| type validationField struct { | ||
| config.SDKGenConfigField `mapstructure:",squash"` | ||
| ValidationExamples struct { | ||
| Valid []string | ||
| Invalid []string | ||
| } | ||
| // Declared on some fields in config.ts but read by neither the generator nor the CLI. | ||
| ValidationFunc any | ||
| } | ||
|
|
||
| func TestValidationRegexes(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("common", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| e, err := executor.New(types.NewTargetFromTemplate("common"), "common/config.ts") | ||
| require.NoError(t, err) | ||
|
|
||
| for _, newSDK := range []bool{false, true} { | ||
| rawFields, err := e.Run("getCommonConfigFields", newSDK) | ||
| require.NoError(t, err) | ||
|
|
||
| checkValidationFields(t, decodeValidationFields(t, rawFields), newSDK) | ||
| } | ||
| }) | ||
|
|
||
| for _, name := range GetAvailableTemplates() { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| target := types.NewTargetFromTemplate(name) | ||
| e, err := executor.New(target, "config.ts") | ||
| require.NoError(t, err) | ||
|
|
||
| for _, newSDK := range []bool{false, true} { | ||
| _, err := GetLanguageConfigFields(target, newSDK) | ||
| require.NoError(t, err) | ||
|
|
||
| commonFields, err := getCommonConfigFields(newSDK) | ||
| require.NoError(t, err) | ||
|
|
||
| rawFields, err := e.Run("getConfigFields", commonFields, newSDK) | ||
| require.NoError(t, err) | ||
|
|
||
| checkValidationFields(t, decodeValidationFields(t, rawFields), newSDK) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func decodeValidationFields(t *testing.T, rawFields any) map[string]validationField { | ||
| t.Helper() | ||
|
|
||
| rawFieldMap, ok := rawFields.(map[string]any) | ||
| require.True(t, ok, "config fields are %T", rawFields) | ||
|
|
||
| fields := map[string]validationField{} | ||
| for key, rawField := range rawFieldMap { | ||
| // Common fields passed through unchanged arrive as Go structs and are checked by the "common" subtest. | ||
| if _, ok := rawField.(map[string]any); !ok { | ||
| continue | ||
| } | ||
|
|
||
| var field validationField | ||
| decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ErrorUnused: true, Result: &field}) | ||
| require.NoError(t, err) | ||
| require.NoError(t, decoder.Decode(rawField), "field %q", key) | ||
| fields[key] = field | ||
| } | ||
|
|
||
| return fields | ||
| } | ||
|
|
||
| func checkValidationFields(t *testing.T, fields map[string]validationField, newSDK bool) { | ||
| t.Helper() | ||
|
|
||
| for key, field := range fields { | ||
| if field.ValidationRegex == nil { | ||
| assert.Empty(t, field.ValidationExamples.Valid, "field %q (newSDK=%v): valid examples without a ValidationRegex", key, newSDK) | ||
| assert.Empty(t, field.ValidationExamples.Invalid, "field %q (newSDK=%v): invalid examples without a ValidationRegex", key, newSDK) | ||
| continue | ||
| } | ||
|
|
||
| pattern := strings.ReplaceAll(*field.ValidationRegex, escapedSlash, "/") | ||
| regex, err := regexp.Compile(pattern) | ||
| require.NoError(t, err, "field %q (newSDK=%v)", key, newSDK) | ||
|
|
||
| if field.DefaultValue != nil { | ||
| defaultValue := fmt.Sprintf("%v", *field.DefaultValue) | ||
| assert.True(t, regex.MatchString(defaultValue), | ||
| "field %q (newSDK=%v): default %q does not match %s", key, newSDK, defaultValue, pattern) | ||
| } | ||
|
|
||
| for _, value := range field.ValidationExamples.Valid { | ||
| assert.True(t, regex.MatchString(value), | ||
| "field %q (newSDK=%v): valid example %q does not match %s", key, newSDK, value, pattern) | ||
| } | ||
|
|
||
| for _, value := range field.ValidationExamples.Invalid { | ||
| assert.False(t, regex.MatchString(value), | ||
| "field %q (newSDK=%v): invalid example %q matches %s", key, newSDK, value, pattern) | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.