| 1 |
// Copyright 2026 Princess B33f Heavy Industries / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package helpers |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/go-openapi/jsonpointer" |
| 11 |
) |
| 12 |
|
| 13 |
// EscapeJSONPointerSegment escapes a single segment for use in a JSON Pointer (RFC 6901). |
| 14 |
// It replaces '~' with '~0' and '/' with '~1'. |
| 15 |
func EscapeJSONPointerSegment(segment string) string { |
| 16 |
return jsonpointer.Escape(segment) |
| 17 |
} |
| 18 |
|
| 19 |
// ConstructParameterJSONPointer constructs a full JSON Pointer path for a parameter |
| 20 |
// in the OpenAPI specification. |
| 21 |
// Format: /paths/{path}/{method}/parameters/{paramName}/schema/{keyword} |
| 22 |
// The path segment is automatically escaped according to RFC 6901. |
| 23 |
// The keyword can be a simple keyword like "type" or a nested path like "items/type". |
| 24 |
func ConstructParameterJSONPointer(pathTemplate, method, paramName, keyword string) string { |
| 25 |
escapedPath := EscapeJSONPointerSegment(pathTemplate) |
| 26 |
escapedPath = strings.TrimPrefix(escapedPath, "~1") // Remove leading slash encoding |
| 27 |
method = strings.ToLower(method) |
| 28 |
return fmt.Sprintf("/paths/%s/%s/parameters/%s/schema/%s", escapedPath, method, paramName, keyword) |
| 29 |
} |
| 30 |
|
| 31 |
// ConstructResponseHeaderJSONPointer constructs a full JSON Pointer path for a response header |
| 32 |
// in the OpenAPI specification. |
| 33 |
// Format: /paths/{path}/{method}/responses/{statusCode}/headers/{headerName}/{keyword} |
| 34 |
// The path segment is automatically escaped according to RFC 6901. |
| 35 |
func ConstructResponseHeaderJSONPointer(pathTemplate, method, statusCode, headerName, keyword string) string { |
| 36 |
escapedPath := EscapeJSONPointerSegment(pathTemplate) |
| 37 |
escapedPath = strings.TrimPrefix(escapedPath, "~1") // Remove leading slash encoding |
| 38 |
method = strings.ToLower(method) |
| 39 |
return fmt.Sprintf("/paths/%s/%s/responses/%s/headers/%s/%s", escapedPath, method, statusCode, headerName, keyword) |
| 40 |
} |