Skip to content

Conversation

Anmol1696
Copy link
Contributor

Fix params ref for extended swagger

Copy link

@Copilot Copilot AI left a 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.

Comment on lines +221 to +233
// 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;
}
Copy link

Copilot AI Sep 3, 2025

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.

Suggested change
// 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.

Comment on lines +221 to +227
// 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;
Copy link

Copilot AI Sep 3, 2025

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.

Suggested change
// 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.

Comment on lines +231 to +232
const resolved = schema.parameters?.[name];
if (resolved) return resolved as Parameter;
Copy link

Copilot AI Sep 3, 2025

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.

Suggested change
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.

@pyramation pyramation merged commit e15d7fa into main Sep 3, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants