-
Notifications
You must be signed in to change notification settings - Fork 1
feature: extend swagger params, allow $ref
#6
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
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.
Pull Request Overview
This PR adds support for resolving $ref
references in OpenAPI parameter definitions. It introduces a new resolveParameterRef
function that handles parameter references and updates the parameter processing functions to use resolved parameters throughout the codebase.
Key changes:
- Adds parameter reference resolution functionality
- Updates parameter processing to resolve
$ref
references before use - Adds comprehensive test coverage for extended swagger scenarios
Reviewed Changes
Copilot reviewed 3 out of 7 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
packages/schema-sdk/src/openapi.ts | Implements parameter reference resolution and updates parameter processing functions |
packages/schema-sdk/tests/openapi.extended.generate.test.ts | Adds test cases for extended swagger functionality with parameter references |
fixtures/output/swagger-extended-definitions.json | Generated output file containing schema definitions for testing |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
// Resolve a parameter $ref against the spec's global parameters collection | ||
function resolveParameterRef( | ||
schema: OpenAPISpec, | ||
param: any | ||
): Parameter { | ||
if (param && typeof param === 'object' && (param as any).$ref) { | ||
const ref: string = (param as any).$ref; | ||
if (ref.includes('/parameters/')) { | ||
const parts = ref.split('/'); | ||
const name = parts[parts.length - 1]; | ||
const resolved = schema.parameters?.[name]; | ||
if (resolved) return resolved as Parameter; | ||
} |
Copilot
AI
Sep 3, 2025
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.
The function assumes all parameter references follow the format '/parameters/{name}', but OpenAPI allows more flexible reference formats like '#/components/parameters/{name}' or relative references. Consider using a more robust reference resolution approach or validating the reference format more strictly.
// Resolve a parameter $ref against the spec's global parameters collection | |
function resolveParameterRef( | |
schema: OpenAPISpec, | |
param: any | |
): Parameter { | |
if (param && typeof param === 'object' && (param as any).$ref) { | |
const ref: string = (param as any).$ref; | |
if (ref.includes('/parameters/')) { | |
const parts = ref.split('/'); | |
const name = parts[parts.length - 1]; | |
const resolved = schema.parameters?.[name]; | |
if (resolved) return resolved as Parameter; | |
} | |
// Helper to resolve a JSON pointer $ref within the schema | |
function resolveRef(schema: any, ref: string): any { | |
if (!ref.startsWith('#/')) return undefined; | |
// Remove the initial '#/' and split by '/' | |
const path = ref.slice(2).split('/').map(decodeURIComponent); | |
let result = schema; | |
for (const segment of path) { | |
if (result && typeof result === 'object' && segment in result) { | |
result = result[segment]; | |
} else { | |
return undefined; | |
} | |
} | |
return result; | |
} | |
// Resolve a parameter $ref against the spec's global parameters collection or components | |
function resolveParameterRef( | |
schema: OpenAPISpec, | |
param: any | |
): Parameter { | |
if (param && typeof param === 'object' && (param as any).$ref) { | |
const ref: string = (param as any).$ref; | |
const resolved = resolveRef(schema, ref); | |
if (resolved) return resolved as Parameter; |
Copilot uses AI. Check for mistakes.
// Resolve a parameter $ref against the spec's global parameters collection | ||
function resolveParameterRef( | ||
schema: OpenAPISpec, | ||
param: any | ||
): Parameter { | ||
if (param && typeof param === 'object' && (param as any).$ref) { | ||
const ref: string = (param as any).$ref; |
Copilot
AI
Sep 3, 2025
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.
Multiple (param as any)
type assertions indicate the function parameter type may be too broad. Consider defining a more specific union type like Parameter | { $ref: string }
to improve type safety.
// Resolve a parameter $ref against the spec's global parameters collection | |
function resolveParameterRef( | |
schema: OpenAPISpec, | |
param: any | |
): Parameter { | |
if (param && typeof param === 'object' && (param as any).$ref) { | |
const ref: string = (param as any).$ref; | |
// Type for a parameter reference object | |
type ParameterRef = { $ref: string }; | |
// Resolve a parameter $ref against the spec's global parameters collection | |
function resolveParameterRef( | |
schema: OpenAPISpec, | |
param: Parameter | ParameterRef | |
): Parameter { | |
if (param && typeof param === 'object' && '$ref' in param) { | |
const ref: string = param.$ref; |
Copilot uses AI. Check for mistakes.
const resolved = schema.parameters?.[name]; | ||
if (resolved) return resolved as Parameter; |
Copilot
AI
Sep 3, 2025
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.
If the referenced parameter is not found in schema.parameters
, the function silently falls through and returns the original $ref
object as a Parameter, which could cause runtime errors. Consider throwing an error or logging a warning when a reference cannot be resolved.
const resolved = schema.parameters?.[name]; | |
if (resolved) return resolved as Parameter; | |
if (resolved) return resolved as Parameter; | |
throw new Error(`Could not resolve parameter reference: ${ref}`); |
Copilot uses AI. Check for mistakes.
Fix params ref for extended swagger