-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Global headers and better enum parsing in OpenApi Parser v2 (#1995
- Loading branch information
1 parent
69227e2
commit 4520edf
Showing
9 changed files
with
273 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/parsers/src/openapi/3.1/extensions/XFernGlobalHeadersConverter.node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { isNonNullish } from "@fern-api/ui-core-utils"; | ||
import { OpenAPIV3_1 } from "openapi-types"; | ||
import { FernRegistry } from "../../../client/generated"; | ||
import { | ||
BaseOpenApiV3_1ConverterNode, | ||
BaseOpenApiV3_1ConverterNodeConstructorArgs, | ||
} from "../../BaseOpenApiV3_1Converter.node"; | ||
import { extendType } from "../../utils/extendType"; | ||
import { maybeSingleValueToArray } from "../../utils/maybeSingleValueToArray"; | ||
import { SchemaConverterNode } from "../schemas"; | ||
import { X_FERN_GLOBAL_HEADERS } from "./fernExtension.consts"; | ||
|
||
export declare namespace XFernGlobalHeadersConverterNode { | ||
export interface Input { | ||
[X_FERN_GLOBAL_HEADERS]?: (( | ||
| OpenAPIV3_1.SchemaObject | ||
| OpenAPIV3_1.ReferenceObject | ||
) & { | ||
header: string; | ||
})[]; | ||
} | ||
} | ||
|
||
export class XFernGlobalHeadersConverterNode extends BaseOpenApiV3_1ConverterNode< | ||
unknown, | ||
FernRegistry.api.latest.ObjectProperty[] | ||
> { | ||
globalHeaders?: [string, SchemaConverterNode][] | undefined; | ||
|
||
constructor(args: BaseOpenApiV3_1ConverterNodeConstructorArgs<unknown>) { | ||
super(args); | ||
this.safeParse(); | ||
} | ||
|
||
// This would be used to set a member on the node | ||
parse(): void { | ||
this.globalHeaders = extendType<XFernGlobalHeadersConverterNode.Input>( | ||
this.input | ||
)[X_FERN_GLOBAL_HEADERS]?.map((header) => { | ||
const { header: headerName, ...schema } = header; | ||
return [ | ||
headerName, | ||
new SchemaConverterNode({ | ||
input: schema, | ||
context: this.context, | ||
accessPath: this.accessPath, | ||
pathId: this.pathId, | ||
}), | ||
]; | ||
}); | ||
} | ||
|
||
convert(): FernRegistry.api.latest.ObjectProperty[] | undefined { | ||
return this.globalHeaders | ||
?.flatMap(([headerName, headerSchema]) => { | ||
const convertedSchema = maybeSingleValueToArray(headerSchema.convert()); | ||
|
||
return convertedSchema?.map((schema) => ({ | ||
key: FernRegistry.PropertyKey(headerName), | ||
valueShape: schema, | ||
description: headerSchema.description, | ||
availability: headerSchema.availability?.convert(), | ||
})); | ||
}) | ||
.filter(isNonNullish); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...ages/parsers/src/openapi/3.1/extensions/__test__/XFernGlobalHeadersConverter.node.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { createMockContext } from "../../../__test__/createMockContext.util"; | ||
import { X_FERN_GLOBAL_HEADERS } from "../fernExtension.consts"; | ||
import { XFernGlobalHeadersConverterNode } from "../XFernGlobalHeadersConverter.node"; | ||
|
||
describe("XFernGlobalHeadersConverterNode", () => { | ||
const mockContext = createMockContext(); | ||
const mockAccessPath = ["test", "path"]; | ||
const mockPathId = "test-path"; | ||
|
||
beforeEach(() => { | ||
mockContext.errors.warning.mockClear(); | ||
}); | ||
|
||
it("should parse global headers correctly", () => { | ||
const input = { | ||
[X_FERN_GLOBAL_HEADERS]: [ | ||
{ | ||
header: "X-API-Key", | ||
type: "string", | ||
description: "API Key for authentication", | ||
}, | ||
], | ||
}; | ||
|
||
const converter = new XFernGlobalHeadersConverterNode({ | ||
input, | ||
context: mockContext, | ||
accessPath: mockAccessPath, | ||
pathId: mockPathId, | ||
}); | ||
|
||
expect(converter.globalHeaders).toBeDefined(); | ||
expect(converter.globalHeaders?.length).toBe(1); | ||
expect(converter.globalHeaders?.[0][0]).toBe("X-API-Key"); | ||
}); | ||
|
||
it("should handle empty input", () => { | ||
const input = {}; | ||
|
||
const converter = new XFernGlobalHeadersConverterNode({ | ||
input, | ||
context: mockContext, | ||
accessPath: mockAccessPath, | ||
pathId: mockPathId, | ||
}); | ||
|
||
expect(converter.globalHeaders).toBeUndefined(); | ||
}); | ||
|
||
it("should convert global headers to ObjectProperties", () => { | ||
const input = { | ||
[X_FERN_GLOBAL_HEADERS]: [ | ||
{ | ||
header: "X-API-Key", | ||
type: "string", | ||
description: "API Key for authentication", | ||
}, | ||
], | ||
}; | ||
|
||
const converter = new XFernGlobalHeadersConverterNode({ | ||
input, | ||
context: mockContext, | ||
accessPath: mockAccessPath, | ||
pathId: mockPathId, | ||
}); | ||
|
||
const result = converter.convert(); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result?.length).toBe(1); | ||
expect(result?.[0].key).toBe("X-API-Key"); | ||
expect(result?.[0].description).toBe("API Key for authentication"); | ||
}); | ||
|
||
it("should handle multiple global headers", () => { | ||
const input = { | ||
[X_FERN_GLOBAL_HEADERS]: [ | ||
{ | ||
header: "X-API-Key", | ||
type: "string", | ||
}, | ||
{ | ||
header: "X-Client-ID", | ||
type: "string", | ||
}, | ||
], | ||
}; | ||
|
||
const converter = new XFernGlobalHeadersConverterNode({ | ||
input, | ||
context: mockContext, | ||
accessPath: mockAccessPath, | ||
pathId: mockPathId, | ||
}); | ||
|
||
const result = converter.convert(); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result?.length).toBe(2); | ||
expect(result?.[0].key).toBe("X-API-Key"); | ||
expect(result?.[1].key).toBe("X-Client-ID"); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/parsers/src/openapi/3.1/extensions/fernExtension.consts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.