-
Notifications
You must be signed in to change notification settings - Fork 39
feat(openapi): add sorted format style #2118
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
+489
−18
Merged
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
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,116 @@ | ||
| package openapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRunFormat_ReadableStyle(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tempDir := t.TempDir() | ||
| inputPath := filepath.Join(tempDir, "input.json") | ||
| outputPath := filepath.Join(tempDir, "output.json") | ||
| input := `{"paths":{},"openapi":"3.0.0","info":{"version":"1","title":"T"}}` | ||
| require.NoError(t, os.WriteFile(inputPath, []byte(input), 0o600)) | ||
|
|
||
| err := runFormat(context.Background(), formatFlags{ | ||
| Schema: inputPath, | ||
| Out: outputPath, | ||
| Style: "readable", | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| actual, err := os.ReadFile(outputPath) | ||
| require.NoError(t, err) | ||
| assert.JSONEq(t, input, string(actual), "readable formatting should preserve the document") | ||
| } | ||
|
|
||
| func TestRunFormat_ReadableUppercaseYAMLOutput(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tempDir := t.TempDir() | ||
| inputPath := filepath.Join(tempDir, "input.json") | ||
| input := `{"paths":{},"openapi":"3.0.0","info":{"version":"1","title":"T"}}` | ||
| require.NoError(t, os.WriteFile(inputPath, []byte(input), 0o600)) | ||
|
|
||
| for _, outputName := range []string{"output.YML", "output.YAML"} { | ||
| outputPath := filepath.Join(tempDir, outputName) | ||
| err := runFormat(context.Background(), formatFlags{ | ||
| Schema: inputPath, | ||
| Out: outputPath, | ||
| Style: "readable", | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| actual, err := os.ReadFile(outputPath) | ||
| require.NoError(t, err) | ||
| assert.Contains(t, string(actual), "openapi: 3.0.0", "uppercase YAML extensions should emit YAML") | ||
| } | ||
| } | ||
|
|
||
| func TestRunFormat_SortedYAMLInput(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tempDir := t.TempDir() | ||
| inputPath := filepath.Join(tempDir, "input.yaml") | ||
| outputPath := filepath.Join(tempDir, "output.json") | ||
| require.NoError(t, os.WriteFile(inputPath, []byte("openapi: 3.0.0\npaths:\n /z: {}\n /a: {}\n"), 0o600)) | ||
|
|
||
| err := runFormat(context.Background(), formatFlags{ | ||
| Schema: inputPath, | ||
| Out: outputPath, | ||
| Style: "sorted", | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| actual, err := os.ReadFile(outputPath) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, `{ | ||
| "openapi": "3.0.0", | ||
| "paths": { | ||
| "/a": {}, | ||
| "/z": {} | ||
| } | ||
| } | ||
| `, string(actual)) | ||
| } | ||
|
|
||
| func TestRunFormat_SortedRejectsYAMLOutputBeforeCreate(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tempDir := t.TempDir() | ||
| inputPath := filepath.Join(tempDir, "input.json") | ||
| require.NoError(t, os.WriteFile(inputPath, []byte(`{"openapi":"3.0.0"}`), 0o600)) | ||
|
|
||
| for _, outputName := range []string{"output.yaml", "output.YML"} { | ||
| outputPath := filepath.Join(tempDir, outputName) | ||
| err := runFormat(context.Background(), formatFlags{ | ||
| Schema: inputPath, | ||
| Out: outputPath, | ||
| Style: "sorted", | ||
| }) | ||
| require.EqualError(t, err, "sorted formatting only supports JSON output") | ||
| _, statErr := os.Stat(outputPath) | ||
| require.ErrorIs(t, statErr, os.ErrNotExist, "rejected output should not be created") | ||
| } | ||
| } | ||
|
|
||
| func TestRunFormat_RejectsUnknownStyleBeforeCreate(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| outputPath := filepath.Join(t.TempDir(), "output.json") | ||
| err := runFormat(context.Background(), formatFlags{ | ||
| Schema: "unused.json", | ||
| Out: outputPath, | ||
| Style: "unknown", | ||
| }) | ||
| require.EqualError(t, err, `unsupported format style "unknown"`) | ||
| _, statErr := os.Stat(outputPath) | ||
| require.ErrorIs(t, statErr, os.ErrNotExist, "rejected output should not be created") | ||
| } |
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 @@ | ||
| {"paths":{"/z":{"get":{"operationId":"z","responses":{"200":{"description":"ok"}}}},"/a":{"get":{"operationId":"a","responses":{"200":{"description":"ok"}}}}},"openapi":"3.0.0","info":{"version":"1","title":"T"},"components":{"schemas":{"Z":{"type":"object","required":["z","a"],"properties":{"z":{"type":"string"},"a":{"type":"string"}}},"A":{"type":"string"}}}} |
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,31 @@ | ||
| paths: | ||
| /z: | ||
| get: | ||
| responses: | ||
| "200": | ||
| description: ok | ||
| operationId: z | ||
| /a: | ||
| get: | ||
| responses: | ||
| "200": | ||
| description: ok | ||
| operationId: a | ||
| openapi: 3.0.0 | ||
| info: | ||
| version: "1" | ||
| title: T | ||
| components: | ||
| schemas: | ||
| Z: | ||
| type: object | ||
| required: | ||
| - z | ||
| - a | ||
| properties: | ||
| z: | ||
| type: string | ||
| a: | ||
| type: string | ||
| A: | ||
| type: string |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P3: The new sorted-style check lowercases the output path (
strings.ToLower(flags.Out)) and so rejectsout.YML/out.YAMLas YAML, but the sharedsetupOutputcomputesyamlOut := utils.HasYAMLExt(out)on the raw path without normalizing case. As a result, the two format styles make opposite decisions for the same uppercase extension:--style sorted --out out.YMLis rejected, while--style readable --out out.YMLis treated as JSON and writes JSON into a.YMLfile. Normalize case insetupOutput(or a shared helper) so both styles handle uppercase YAML extensions consistently.Prompt for AI agents