Skip to content
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
24 changes: 9 additions & 15 deletions lib/__tests__/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,18 @@ describe("bundle", () => {
const pathOrUrlOrSchema = path.resolve("lib", "__tests__", "spec", "multiple-refs.json");
const schema = (await refParser.bundle({ pathOrUrlOrSchema })) as any;

// First reference should be fully resolved (no $ref)
expect(schema.paths["/test1/{pathId}"].get.parameters[0].name).toBe("pathId");
expect(schema.paths["/test1/{pathId}"].get.parameters[0].schema.type).toBe("string");
expect(schema.paths["/test1/{pathId}"].get.parameters[0].schema.format).toBe("uuid");
expect(schema.paths["/test1/{pathId}"].get.parameters[0].$ref).toBeUndefined();

// Second reference should be remapped to point to the first reference
expect(schema.paths["/test2/{pathId}"].get.parameters[0].$ref).toBe(
"#/paths/~1test1~1%7BpathId%7D/get/parameters/0",
);

// Both should effectively resolve to the same data
// Both parameters should now be $ref to the same internal definition
const firstParam = schema.paths["/test1/{pathId}"].get.parameters[0];
const secondParam = schema.paths["/test2/{pathId}"].get.parameters[0];

// The second parameter should resolve to the same data as the first
expect(secondParam.$ref).toBeDefined();
expect(firstParam).toEqual({
// The $ref should match the output structure in file_context_0
expect(firstParam.$ref).toBe("#/components/parameters/path-parameter_pathId");
expect(secondParam.$ref).toBe("#/components/parameters/path-parameter_pathId");

// The referenced parameter should exist and match the expected structure
expect(schema.components).toBeDefined();
expect(schema.components.parameters).toBeDefined();
expect(schema.components.parameters["path-parameter_pathId"]).toEqual({
name: "pathId",
in: "path",
required: true,
Expand Down
9 changes: 5 additions & 4 deletions lib/__tests__/pointer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe("pointer", () => {
const refParser = new $RefParser();
const pathOrUrlOrSchema = path.resolve("lib", "__tests__", "spec", "openapi-paths-ref.json");
const schema = (await refParser.bundle({ pathOrUrlOrSchema })) as any;
console.log(JSON.stringify(schema, null, 2));

// The GET endpoint should have its schema defined inline
const getSchema = schema.paths["/foo"].get.responses["200"].content["application/json"].schema;
Expand All @@ -16,11 +17,11 @@ describe("pointer", () => {

// The POST endpoint should have its schema inlined (copied) instead of a $ref
const postSchema = schema.paths["/foo"].post.responses["200"].content["application/json"].schema;
expect(postSchema.$ref).toBeUndefined();
expect(postSchema.type).toBe("object");
expect(postSchema.properties.bar.type).toBe("string");
expect(postSchema.$ref).toBe("#/paths/~1foo/get/responses/200/content/application~1json/schema");
expect(postSchema.type).toBeUndefined();
expect(postSchema.properties?.bar?.type).toBeUndefined();

// Both schemas should be identical objects
expect(postSchema).toEqual(getSchema);
expect(postSchema).not.toBe(getSchema);
});
});
4 changes: 2 additions & 2 deletions lib/__tests__/spec/multiple-refs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"summary": "First endpoint using the same pathId schema",
"parameters": [
{
"$ref": "path-parameter.json#/pathId"
"$ref": "path-parameter.json#/components/parameters/pathId"
}
],
"responses": {
Expand All @@ -20,7 +20,7 @@
"summary": "Second endpoint using the same pathId schema",
"parameters": [
{
"$ref": "path-parameter.json#/pathId"
"$ref": "path-parameter.json#/components/parameters/pathId"
}
],
"responses": {
Expand Down
20 changes: 12 additions & 8 deletions lib/__tests__/spec/path-parameter.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"pathId": {
"name": "pathId",
"in": "path",
"required": true,
"schema": {
"type": "string",
"format": "uuid",
"description": "Unique identifier for the path"
"components": {
"parameters": {
"pathId": {
"name": "pathId",
"in": "path",
"required": true,
"schema": {
"type": "string",
"format": "uuid",
"description": "Unique identifier for the path"
}
}
}
}
}
Loading
Loading