Skip to content

Commit 4912bc7

Browse files
Refactor getComponentByRef method and add security scheme test
1 parent 8ac9653 commit 4912bc7

3 files changed

Lines changed: 43 additions & 10 deletions

File tree

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@murat/openapi",
33
"exports": "./mod.ts",
4+
"version": "0.1.2",
45
"tasks": {
56
"lint": "deno lint",
67
"test": "deno test --allow-all",

src/OpenAPI.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,18 +401,31 @@ class OpenAPI {
401401
return undefined;
402402
}
403403

404-
const path = ref.substring(1).split("/");
405-
// deno-lint-ignore no-explicit-any
406-
let current: Record<string, any> = this._openAPI as any;
407-
408-
for (const segment of path) {
409-
if (!current || typeof current !== "object" || !(segment in current)) {
410-
return undefined;
411-
}
412-
current = current[segment];
404+
const parts = ref.substring(2).split('/');
405+
if (parts.length !== 3) {
406+
return undefined;
407+
}
408+
409+
// parts[0] should be "components"
410+
// parts[1] should be the component type (e.g., "securitySchemes")
411+
// parts[2] should be the component name (e.g., "ApiKey")
412+
413+
const componentType = parts[1] as keyof OpenAPIComponents;
414+
const componentName = parts[2];
415+
416+
// Check if components exists
417+
if (!this._openAPI.components) {
418+
return undefined;
419+
}
420+
421+
// Check if the specific component collection exists
422+
const collection = this._openAPI.components[componentType];
423+
if (!collection) {
424+
return undefined;
413425
}
414426

415-
return current;
427+
// Return the component from the collection if it exists
428+
return (collection as Record<string, unknown>)[componentName];
416429
}
417430
}
418431

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { OpenAPI } from "../../src/OpenAPI.ts";
2+
3+
// Create test API
4+
const api = new OpenAPI({
5+
title: "Test API",
6+
version: "1.0.0"
7+
});
8+
9+
// Add a security scheme
10+
const apiKeyRef = api.addApiKeySecurity("ApiKey", {
11+
in: "header",
12+
parameterName: "X-API-Key",
13+
description: "API Key authentication"
14+
});
15+
16+
// Debug information
17+
console.log("Reference:", apiKeyRef.$ref);
18+
console.log("Components structure:", JSON.stringify(api.getJSON().components, null, 2));
19+
console.log("Result of getComponentByRef:", api.getComponentByRef(apiKeyRef.$ref));

0 commit comments

Comments
 (0)