Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
openapi: 3.0.0
info:
title: Generic API
version: 1.0.0
description: A generic API to test path-level parameters handling
paths:
/items/{itemId}:
parameters:
- name: itemId
in: path
required: true
description: The ID of the item
schema:
type: string
get:
operationId: getItem
summary: Get an item by ID
responses:
'200':
description: Item details
content:
application/json:
schema:
type: object
properties:
id:
type: string
name:
type: string
'404':
description: Item not found
put:
operationId: updateItem
summary: Update an item
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name:
type: string
responses:
'200':
description: Item updated successfully
'404':
description: Item not found
29 changes: 29 additions & 0 deletions packages/generator-openapi/src/test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,35 @@ describe('OpenAPI EventCatalog Plugin', () => {
});
});

describe('path-level parameters handling', () => {
it('should not create redundant PARAMETERS folders when OpenAPI spec has path-level parameters', async () => {
const { getService } = utils(catalogDir);

await plugin(config, {
services: [{ path: join(openAPIExamples, 'path-parameters-example.yml'), id: 'generic-api' }],
});

const service = await getService('generic-api', '1.0.0');
expect(service).toBeDefined();

// Check that only legitimate query folders are created
const queriesDir = join(catalogDir, 'services', 'generic-api', 'queries');
const queriesFolders = await fs.readdir(queriesDir);

// Should only have the actual operations, not PARAMETERS folders
expect(queriesFolders).toEqual(['getItem', 'updateItem']);

// Verify no PARAMETERS folders exist
const parametersFolder = queriesFolders.filter(folder => folder.includes('PARAMETERS'));
expect(parametersFolder).toHaveLength(0);

// Verify the actual operations are properly created
expect(queriesFolders).not.toContain('generic-api_PARAMETERS_v0_items_{itemId}');
expect(queriesFolders).toContain('getItem');
expect(queriesFolders).toContain('updateItem');
});
});

describe('parsing multiple OpenAPI files to the same service', () => {
it('when multiple OpenAPI files are parsed to the same service, the services and messages are written to the correct locations', async () => {
const { getService } = utils(catalogDir);
Expand Down
16 changes: 14 additions & 2 deletions packages/generator-openapi/src/utils/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import SwaggerParser from '@apidevtools/swagger-parser';
import { OpenAPIDocument, OpenAPIOperation, OpenAPIParameter, Operation } from '../types';
import { HTTP_METHOD, HTTP_METHOD_TO_MESSAGE_TYPE } from '../index';
import { OpenAPIDocument, OpenAPIOperation, OpenAPIParameter, Operation } from '../types';
const DEFAULT_MESSAGE_TYPE = 'query';
// Valid HTTP methods in OpenAPI 3.0
const validHttpMethods = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];

export async function getSchemasByOperationId(filePath: string, operationId: string): Promise<OpenAPIOperation | undefined> {
try {
Expand All @@ -20,6 +22,11 @@ export async function getSchemasByOperationId(filePath: string, operationId: str
// Iterate through paths and operations
for (const [path, pathItem] of Object.entries(api.paths)) {
for (const [method, operation] of Object.entries(pathItem)) {
// Skip non-HTTP method properties like 'parameters', 'summary', 'description', '$ref', 'servers'
if (!validHttpMethods.includes(method.toLowerCase())) {
continue;
}

// Cast operation to OpenAPIOperation type
const typedOperation = operation as OpenAPIOperation;

Expand Down Expand Up @@ -85,8 +92,13 @@ export async function getOperationsByType(openApiPath: string, httpMethodsToMess
for (const path in api.paths) {
const pathItem = api.paths[path];

// Iterate through each HTTP method in the path
// Iterate through each HTTP method in the path, but skip non-HTTP method properties
for (const method in pathItem) {
// Skip non-HTTP method properties like 'parameters', 'summary', 'description', '$ref', 'servers'
if (!validHttpMethods.includes(method.toLowerCase())) {
continue;
}

// @ts-ignore
const openAPIOperation = pathItem[method];

Expand Down