Skip to content
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

Fix param $refs #997

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/transform/operation-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export default function transformOperationObject(
else if (p.$ref) {
const parts = parseTSIndex(p.$ref);
const paramI = parts.indexOf("parameters");
if (paramI === -1 || parts[paramI + 1] !== paramIn || !parts[paramI + 2]) continue;
// note: parameters can share names in different parts of the `in` key. `partsParamIn` makes sure we’re not
// referencing the wrong parameter with the same name but a different `in`
const partsParamIn = parts.find((p) => p === "query" || p === "header" || p === "path" || p === "cookie");
if (paramI === -1 || (partsParamIn && partsParamIn !== paramIn)) continue;
const key = parts.pop() as string;
const index = makeTSIndex(parts);
if (!refs[index]) refs[index] = [key];
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export interface ParameterObject extends Extensable {
/** Determines whether the parameter value SHOULD allow reserved characters, as defined by [RFC3986] `:/?#[]@!$&'()*+,;=` to be included without percent-encoding. This property only applies to parameters with an `in` value of `query`. The default value is `false`. */
allowReserved?: boolean;
/** The schema defining the type used for the parameter. */
schema?: SchemaObject;
schema?: SchemaObject | ReferenceObject;
/** Example of the parameter’s potential value. */
example?: any;
/** Examples of the parameter’s potential value. */
Expand Down
7 changes: 7 additions & 0 deletions test/path-item-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe("Path Item Object", () => {
404: { $ref: 'components["responses"]["NotFound"]' },
},
},
parameters: [{ $ref: 'components["parameters"]["PerPage"]' }, { $ref: 'components["parameters"]["PageNumber"]' }],
};
const generated = transformPathItemObject(schema, options);
expect(generated).toBe(`{
Expand Down Expand Up @@ -79,6 +80,12 @@ describe("Path Item Object", () => {
404: components["responses"]["NotFound"];
};
};
parameters?: {
query?: Pick<NonNullable<components["parameters"]>, "PerPage" | "PageNumber">;
header?: Pick<NonNullable<components["parameters"]>, "PerPage" | "PageNumber">;
path?: Pick<components["parameters"], "PerPage" | "PageNumber">;
cookie?: Pick<NonNullable<components["parameters"]>, "PerPage" | "PageNumber">;
};
}`);
});

Expand Down