Skip to content

Inherit parameters from path item to its operations. #530

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

Merged
merged 4 commits into from
Mar 24, 2021
Merged
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
7 changes: 4 additions & 3 deletions src/transform/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OperationObject } from "../types";
import { OperationObject, PathItemObject } from "../types";
import { comment, tsReadonly } from "../utils";
import { transformHeaderObjMap } from "./headers";
import { transformOperationObj } from "./operation";
Expand All @@ -17,7 +17,7 @@ export function transformAll(schema: any, { immutableTypes, rawSchema, version }

let output = "";

let operations: Record<string, OperationObject> = {};
let operations: Record<string, { operation: OperationObject; pathItem: PathItemObject }> = {};

// --raw-schema mode
if (rawSchema) {
Expand Down Expand Up @@ -128,9 +128,10 @@ export function transformAll(schema: any, { immutableTypes, rawSchema, version }

output += `export interface operations {\n`; // open operations
if (Object.keys(operations).length) {
Object.entries(operations).forEach(([operationId, operation]) => {
Object.entries(operations).forEach(([operationId, { operation, pathItem }]) => {
if (operation.description) output += comment(operation.description); // handle comment
output += ` ${readonly}"${operationId}": {\n ${transformOperationObj(operation, {
pathItem,
globalParameters: (schema.components && schema.components.parameters) || schema.parameters,
immutableTypes,
version,
Expand Down
9 changes: 6 additions & 3 deletions src/transform/operation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OperationObject, ParameterObject, RequestBody } from "../types";
import { OperationObject, ParameterObject, PathItemObject, RequestBody } from "../types";
import { comment, isRef, transformRef, tsReadonly } from "../utils";
import { transformParametersArray } from "./parameters";
import { transformResponsesObj } from "./responses";
Expand All @@ -9,8 +9,10 @@ export function transformOperationObj(
{
globalParameters,
immutableTypes,
pathItem = {},
version,
}: {
pathItem?: PathItemObject;
globalParameters?: Record<string, ParameterObject>;
immutableTypes: boolean;
version: number;
Expand All @@ -20,8 +22,9 @@ export function transformOperationObj(

let output = "";

if (operation.parameters) {
output += ` ${readonly}parameters: {\n ${transformParametersArray(operation.parameters, {
if (operation.parameters || pathItem.parameters) {
const parameters = (pathItem.parameters || []).concat(operation.parameters || []);
output += ` ${readonly}parameters: {\n ${transformParametersArray(parameters, {
globalParameters,
immutableTypes,
version,
Expand Down
8 changes: 4 additions & 4 deletions src/transform/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { transformParametersArray } from "./parameters";
interface TransformPathsObjOption {
globalParameters: Record<string, ParameterObject>;
immutableTypes: boolean;
operations: Record<string, OperationObject>;
operations: Record<string, { operation: OperationObject; pathItem: PathItemObject }>;
version: number;
}

Expand Down Expand Up @@ -39,15 +39,15 @@ export function transformPathsObj(

// if operation has operationId, abstract into top-level operations object
if (operation.operationId) {
output += ` ${readonly}"${method}": operations["${operation.operationId}"];\n`;
operations[operation.operationId] = operation;
operations[operation.operationId] = { operation, pathItem };
output += ` ${readonly}"${method}": operations["${operation.operationId}"];\n`;
return;
}

// otherwise, inline operation
output += ` ${readonly}"${method}": {\n ${transformOperationObj(operation, {
globalParameters,
immutableTypes,
pathItem,
version,
})}\n }\n`;
});
Expand Down
112 changes: 112 additions & 0 deletions tests/operation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,115 @@ describe("requestBodies", () => {
);
});
});

describe("parameters", () => {
it("operation parameters only", () => {
expect(
transformOperationObj(
{
parameters: [
{
in: "path",
name: "p1",
schema: {
type: "string",
},
},
],
},
{
version: 3,
immutableTypes: false,
pathItem: {},
}
).trim()
).toBe(`parameters: {
path: {
"p1"?: string;
}

}`);
});

it("inherited path parameters only", () => {
expect(
transformOperationObj(
{},
{
version: 3,
immutableTypes: false,
pathItem: {
parameters: [
{
in: "path",
name: "p1",
schema: {
type: "string",
},
},
],
},
}
).trim()
).toBe(`parameters: {
path: {
"p1"?: string;
}

}`);
});

it("inherited path parameters and operation parameters", () => {
expect(
transformOperationObj(
{
parameters: [
{
in: "path",
name: "p1",
schema: {
type: "string",
},
},
{
in: "path",
name: "p2",
schema: {
type: "number",
},
},
],
},
{
version: 3,
immutableTypes: false,
pathItem: {
parameters: [
{
in: "path",
name: "p2",
schema: {
type: "string",
},
},
{
in: "path",
name: "p3",
schema: {
type: "string",
},
},
],
},
}
).trim()
).toBe(`parameters: {
path: {
"p2"?: number;
"p3"?: string;
"p1"?: string;
}

}`);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tests! 💯

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The tests obviously did work when I ran this locally, but not in CI for some reason. Checking if removing Node 14 stuff solves it.

10 changes: 10 additions & 0 deletions tests/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ describe("transformPathsObj", () => {
expect(transform(parametersSchema)).toBe(`export interface paths {
"/{example}": {
get: {
parameters: {
path: {
example: string;
};
};
responses: {};
};
parameters: {
Expand All @@ -455,6 +460,11 @@ describe("transformPathsObj", () => {
expect(transform(parametersSchema, { immutableTypes: true })).toBe(`export interface paths {
readonly "/{example}": {
readonly get: {
readonly parameters: {
readonly path: {
readonly example: string;
};
};
readonly responses: {};
};
readonly parameters: {
Expand Down