forked from pb33f/libopenapi-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Add centralized JSON Pointer construction helpers for OpenAPI paths #5
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
mjbonifacio
wants to merge
2
commits into
pre-validation-error-cleanup
Choose a base branch
from
json-pointer-helpers
base: pre-validation-error-cleanup
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
2 commits
Select commit
Hold shift + click to select a range
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,40 @@ | ||
| // Copyright 2023 Princess B33f Heavy Industries / Dave Shanley | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package helpers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // EscapeJSONPointerSegment escapes a single segment for use in a JSON Pointer (RFC 6901). | ||
| // It replaces '~' with '~0' and '/' with '~1'. | ||
| func EscapeJSONPointerSegment(segment string) string { | ||
| escaped := strings.ReplaceAll(segment, "~", "~0") | ||
| escaped = strings.ReplaceAll(escaped, "/", "~1") | ||
| return escaped | ||
| } | ||
|
|
||
| // ConstructParameterJSONPointer constructs a full JSON Pointer path for a parameter | ||
| // in the OpenAPI specification. | ||
| // Format: /paths/{path}/{method}/parameters/{paramName}/schema/{keyword} | ||
| // The path segment is automatically escaped according to RFC 6901. | ||
| func ConstructParameterJSONPointer(pathTemplate, method, paramName, keyword string) string { | ||
| escapedPath := EscapeJSONPointerSegment(pathTemplate) | ||
| escapedPath = strings.TrimPrefix(escapedPath, "~1") // Remove leading slash encoding | ||
| method = strings.ToLower(method) | ||
| return fmt.Sprintf("/paths/%s/%s/parameters/%s/schema/%s", escapedPath, method, paramName, keyword) | ||
| } | ||
|
|
||
| // ConstructResponseHeaderJSONPointer constructs a full JSON Pointer path for a response header | ||
| // in the OpenAPI specification. | ||
| // Format: /paths/{path}/{method}/responses/{statusCode}/headers/{headerName}/{keyword} | ||
| // The path segment is automatically escaped according to RFC 6901. | ||
| func ConstructResponseHeaderJSONPointer(pathTemplate, method, statusCode, headerName, keyword string) string { | ||
| escapedPath := EscapeJSONPointerSegment(pathTemplate) | ||
| escapedPath = strings.TrimPrefix(escapedPath, "~1") // Remove leading slash encoding | ||
| method = strings.ToLower(method) | ||
| return fmt.Sprintf("/paths/%s/%s/responses/%s/headers/%s/%s", escapedPath, method, statusCode, headerName, keyword) | ||
| } | ||
|
|
||
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,150 @@ | ||
| // Copyright 2023 Princess B33f Heavy Industries / Dave Shanley | ||
|
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. 2025 |
||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package helpers | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestEscapeJSONPointerSegment(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "no special characters", | ||
| input: "simple", | ||
| expected: "simple", | ||
| }, | ||
| { | ||
| name: "tilde only", | ||
| input: "some~thing", | ||
| expected: "some~0thing", | ||
| }, | ||
| { | ||
| name: "slash only", | ||
| input: "path/to/something", | ||
| expected: "path~1to~1something", | ||
| }, | ||
| { | ||
| name: "both tilde and slash", | ||
| input: "path/with~special/chars~", | ||
| expected: "path~1with~0special~1chars~0", | ||
| }, | ||
| { | ||
| name: "path template", | ||
| input: "/users/{id}", | ||
| expected: "~1users~1{id}", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := EscapeJSONPointerSegment(tt.input) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestConstructParameterJSONPointer(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| pathTemplate string | ||
| method string | ||
| paramName string | ||
| keyword string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "simple path with query parameter type", | ||
| pathTemplate: "/users", | ||
| method: "GET", | ||
| paramName: "limit", | ||
| keyword: "type", | ||
| expected: "/paths/users/get/parameters/limit/schema/type", | ||
| }, | ||
| { | ||
| name: "path with parameter and enum keyword", | ||
| pathTemplate: "/users/{id}", | ||
| method: "POST", | ||
| paramName: "status", | ||
| keyword: "enum", | ||
| expected: "/paths/users~1{id}/post/parameters/status/schema/enum", | ||
| }, | ||
| { | ||
| name: "path with tilde character", | ||
| pathTemplate: "/some~path", | ||
| method: "PUT", | ||
| paramName: "value", | ||
| keyword: "format", | ||
| expected: "/paths/some~0path/put/parameters/value/schema/format", | ||
| }, | ||
| { | ||
| name: "path with multiple slashes", | ||
| pathTemplate: "/api/v1/users/{userId}/posts/{postId}", | ||
| method: "DELETE", | ||
| paramName: "filter", | ||
| keyword: "required", | ||
| expected: "/paths/api~1v1~1users~1{userId}~1posts~1{postId}/delete/parameters/filter/schema/required", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := ConstructParameterJSONPointer(tt.pathTemplate, tt.method, tt.paramName, tt.keyword) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestConstructResponseHeaderJSONPointer(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| pathTemplate string | ||
| method string | ||
| statusCode string | ||
| headerName string | ||
| keyword string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "simple response header", | ||
| pathTemplate: "/health", | ||
| method: "GET", | ||
| statusCode: "200", | ||
| headerName: "X-Request-ID", | ||
| keyword: "required", | ||
| expected: "/paths/health/get/responses/200/headers/X-Request-ID/required", | ||
| }, | ||
| { | ||
| name: "path with parameter", | ||
| pathTemplate: "/users/{id}", | ||
| method: "POST", | ||
| statusCode: "201", | ||
| headerName: "Location", | ||
| keyword: "schema", | ||
| expected: "/paths/users~1{id}/post/responses/201/headers/Location/schema", | ||
| }, | ||
| { | ||
| name: "path with tilde and slash", | ||
| pathTemplate: "/some~path/to/resource", | ||
| method: "PUT", | ||
| statusCode: "204", | ||
| headerName: "ETag", | ||
| keyword: "type", | ||
| expected: "/paths/some~0path~1to~1resource/put/responses/204/headers/ETag/type", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := ConstructResponseHeaderJSONPointer(tt.pathTemplate, tt.method, tt.statusCode, tt.headerName, tt.keyword) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
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
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.
2025