-
Notifications
You must be signed in to change notification settings - Fork 2
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
wip: foundation for OpenApi Parser #1814
Open
RohinBhargava
wants to merge
15
commits into
main
Choose a base branch
from
rohin/openapi-parser-foundation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,812
−213
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
16da963
started openapi parser
RohinBhargava 4437b3e
composable api node working
RohinBhargava 66c2d29
small addition
RohinBhargava 1332df3
demo uts
RohinBhargava 3913b36
added zod
RohinBhargava cf42860
add primitive and objects
RohinBhargava 6327b74
Merge branch 'main' into rohin/openapi-parser-foundation
RohinBhargava a430433
depcheck fixes
RohinBhargava 7a201f3
removed temporary files
RohinBhargava 21c12d7
unit tests updated
RohinBhargava c9bf23b
addressed comments
RohinBhargava 452c87a
Merge branch 'main' into rohin/openapi-parser-foundation
RohinBhargava f467f83
remove zod for now
RohinBhargava 792096d
update logger path
RohinBhargava c3768ba
remove unused dep
RohinBhargava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
openapi/shared/temporary |
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,16 @@ | ||
import { vi } from "vitest"; | ||
import { ApiNodeContext } from "../openapi/ApiNode"; | ||
|
||
import { createLogger } from "@fern-api/logger"; | ||
|
||
export function createMockContext(): ApiNodeContext { | ||
return { | ||
orgId: "orgId", | ||
apiId: "apiId", | ||
logger: createLogger(() => undefined), | ||
errorCollector: { | ||
addError: vi.fn(), | ||
errors: [], | ||
}, | ||
}; | ||
} |
87 changes: 87 additions & 0 deletions
87
packages/parsers/__test__/shared/nodes/object.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,87 @@ | ||
import { FdrAPI } from "@fern-api/fdr-sdk"; | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { ObjectNode } from "../../../openapi/shared/nodes/object.node"; | ||
import { SchemaObject } from "../../../openapi/shared/openapi.types"; | ||
import { createMockContext } from "../../createMockContext.util"; | ||
|
||
describe("ObjectNode", () => { | ||
const mockContext = createMockContext(); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("constructor", () => { | ||
it("should handle object with no properties or extends", () => { | ||
const input: SchemaObject = { | ||
type: "object", | ||
}; | ||
const node = new ObjectNode(mockContext, input, []); | ||
expect(node.properties).toEqual([]); | ||
expect(node.extends).toEqual([]); | ||
expect(node.extraProperties).toBeUndefined(); | ||
}); | ||
|
||
it("should handle object with properties", () => { | ||
const input: SchemaObject = { | ||
type: "object", | ||
properties: { | ||
name: { type: "string" }, | ||
age: { type: "integer" }, | ||
}, | ||
}; | ||
const node = new ObjectNode(mockContext, input, []); | ||
expect(node.properties).toHaveLength(2); | ||
}); | ||
|
||
it("should handle object with allOf/extends", () => { | ||
const input: SchemaObject = { | ||
type: "object", | ||
allOf: [{ $ref: "TypeA" }, { $ref: "TypeB" }], | ||
}; | ||
const node = new ObjectNode(mockContext, input, []); | ||
// This needs to change to the computed generated type id for FDR | ||
expect(node.extends).toEqual([FdrAPI.TypeId("TypeA"), FdrAPI.TypeId("TypeB")]); | ||
}); | ||
|
||
it("should filter out non-reference allOf items", () => { | ||
const input: SchemaObject = { | ||
type: "object", | ||
allOf: [{ $ref: "TypeA" }, { type: "object" }], | ||
}; | ||
const node = new ObjectNode(mockContext, input, []); | ||
expect(node.extends).toEqual([FdrAPI.TypeId("TypeA")]); | ||
}); | ||
}); | ||
|
||
describe("toFdrShape", () => { | ||
it("should output shape with no properties", () => { | ||
const node = new ObjectNode(mockContext, { type: "object" }, []); | ||
expect(node.toFdrShape()).toEqual({ | ||
extends: [], | ||
properties: [], | ||
extraProperties: undefined, | ||
}); | ||
}); | ||
|
||
it("should output shape with multiple properties and extends", () => { | ||
const input: SchemaObject = { | ||
type: "object", | ||
properties: { | ||
firstName: { type: "string" }, | ||
lastName: { type: "string" }, | ||
age: { type: "integer" }, | ||
height: { type: "number" }, | ||
id: { type: "string" }, | ||
score: { type: "number" }, | ||
}, | ||
allOf: [{ $ref: "BaseType" }, { $ref: "PersonType" }], | ||
}; | ||
const node = new ObjectNode(mockContext, input, []); | ||
const shape = node.toFdrShape(); | ||
expect(shape?.extends).toEqual([FdrAPI.TypeId("BaseType"), FdrAPI.TypeId("PersonType")]); | ||
expect(shape?.properties).toHaveLength(6); | ||
expect(shape?.extraProperties).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
packages/parsers/__test__/shared/nodes/objectProperty.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,61 @@ | ||
import { FdrAPI } from "@fern-api/fdr-sdk"; | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { ObjectPropertyNode } from "../../../openapi/shared/nodes/objectProperty.node"; | ||
import { ReferenceObject, SchemaObject } from "../../../openapi/shared/openapi.types"; | ||
import { createMockContext } from "../../createMockContext.util"; | ||
|
||
describe("ObjectPropertyNode", () => { | ||
const mockContext = createMockContext(); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("constructor", () => { | ||
it("should handle basic schema object", () => { | ||
const input: SchemaObject = { | ||
type: "string", | ||
description: "test description", | ||
}; | ||
const node = new ObjectPropertyNode("testKey", mockContext, input, []); | ||
expect(node.description).toBe("test description"); | ||
}); | ||
|
||
it("should handle reference object", () => { | ||
const input: ReferenceObject = { | ||
$ref: "TypeA", | ||
}; | ||
const node = new ObjectPropertyNode("testKey", mockContext, input, []); | ||
expect(node.valueShape).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe("toFdrShape", () => { | ||
it("should output shape with primitive type", () => { | ||
const input: SchemaObject = { | ||
type: "string", | ||
description: "test description", | ||
}; | ||
const node = new ObjectPropertyNode("testKey", mockContext, input, []); | ||
const shape = node.toFdrShape(); | ||
expect(shape).toBeDefined(); | ||
expect(shape?.key).toEqual(FdrAPI.PropertyKey("testKey")); | ||
expect(shape?.description).toBe("test description"); | ||
expect(shape?.availability).toBeUndefined(); | ||
}); | ||
|
||
it("should return undefined if valueShape is undefined and collect error", () => { | ||
const input: SchemaObject = { | ||
type: "invalid", | ||
}; | ||
const node = new ObjectPropertyNode("testKey", mockContext, input, []); | ||
vi.spyOn(node.valueShape, "toFdrShape").mockReturnValue(undefined); | ||
expect(node.toFdrShape()).toBeUndefined(); | ||
// this should show up, but since the examples are terse and non-exhaustive, we do not have any validation checking | ||
// expect(mockContext.errorCollector.addError).toHaveBeenCalledWith( | ||
// "Failed to generate shape for property testKey", | ||
// [], | ||
// ); | ||
}); | ||
}); | ||
}); |
100 changes: 100 additions & 0 deletions
100
packages/parsers/__test__/shared/nodes/primitives/number.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,100 @@ | ||
import { expect } from "vitest"; | ||
|
||
import { beforeEach, describe, it, vi } from "vitest"; | ||
import { NumberNode } from "../../../../openapi/shared/nodes/primitives/number.node"; | ||
import { FloatNode } from "../../../../openapi/shared/nodes/primitives/number/float.node"; | ||
import { IntegerNode } from "../../../../openapi/shared/nodes/primitives/number/integer.node"; | ||
import { createMockContext } from "../../../createMockContext.util"; | ||
|
||
describe("NumberNode", () => { | ||
const mockContext = createMockContext(); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("constructor", () => { | ||
it("should handle valid integer input", () => { | ||
const input = { | ||
type: "integer", | ||
minimum: 1, | ||
maximum: 10, | ||
default: 5, | ||
}; | ||
const node = new NumberNode(mockContext, input, []); | ||
expect(node.typeNode).toBeInstanceOf(IntegerNode); | ||
expect(node.minimum).toBe(1); | ||
expect(node.maximum).toBe(10); | ||
expect(node.default).toBe(5); | ||
expect(mockContext.errorCollector.addError).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle valid number input", () => { | ||
const input = { | ||
type: "number", | ||
minimum: 1.5, | ||
maximum: 10.5, | ||
default: 5.5, | ||
}; | ||
const node = new NumberNode(mockContext, input, []); | ||
expect(node.typeNode).toBeInstanceOf(FloatNode); | ||
expect(node.minimum).toBe(1.5); | ||
expect(node.maximum).toBe(10.5); | ||
expect(node.default).toBe(5.5); | ||
expect(mockContext.errorCollector.addError).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle invalid type", () => { | ||
const input = { type: "string" }; | ||
const node = new NumberNode(mockContext, input, []); | ||
expect(node.typeNode).toBeUndefined(); | ||
expect(mockContext.errorCollector.addError).toHaveBeenCalledWith( | ||
'Expected type "integer" or "number" for numerical primitive, but got "string"', | ||
[], | ||
undefined, | ||
); | ||
}); | ||
}); | ||
|
||
describe("toFdrShape", () => { | ||
it("should return undefined when typeNode shape is undefined", () => { | ||
const input = { type: "string" }; | ||
const node = new NumberNode(mockContext, input, []); | ||
expect(node.toFdrShape()).toBeUndefined(); | ||
}); | ||
|
||
it("should return complete shape for integer type", () => { | ||
const input = { | ||
type: "integer", | ||
minimum: 1, | ||
maximum: 10, | ||
default: 5, | ||
}; | ||
const node = new NumberNode(mockContext, input, []); | ||
const shape = node.toFdrShape(); | ||
expect(shape).toEqual({ | ||
type: "integer", | ||
minimum: 1, | ||
maximum: 10, | ||
default: 5, | ||
}); | ||
}); | ||
|
||
it("should return complete shape for number type", () => { | ||
const input = { | ||
type: "number", | ||
minimum: 1.5, | ||
maximum: 10.5, | ||
default: 5.5, | ||
}; | ||
const node = new NumberNode(mockContext, input, []); | ||
const shape = node.toFdrShape(); | ||
expect(shape).toEqual({ | ||
type: "double", | ||
minimum: 1.5, | ||
maximum: 10.5, | ||
default: 5.5, | ||
}); | ||
}); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
packages/parsers/__test__/shared/nodes/primitives/number/float.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,60 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { ApiNodeContext } from "../../../../../openapi/ApiNode"; | ||
import { FloatNode } from "../../../../../openapi/shared/nodes/primitives/number/float.node"; | ||
import { createMockContext } from "../../../../createMockContext.util"; | ||
|
||
describe("FloatNode", () => { | ||
const mockContext = createMockContext(); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("should handle valid number input with float format", () => { | ||
const input = { type: "number", format: "float" }; | ||
const node = new FloatNode(mockContext, input, []); | ||
expect(node.type).toBe("double"); | ||
expect(node.toFdrShape()).toEqual({ type: "double" }); | ||
expect(mockContext.errorCollector.addError).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle valid number input with double format", () => { | ||
const input = { type: "number", format: "double" }; | ||
const node = new FloatNode(mockContext, input, []); | ||
expect(node.type).toBe("double"); | ||
expect(node.toFdrShape()).toEqual({ type: "double" }); | ||
expect(mockContext.errorCollector.addError).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle valid number input with no format", () => { | ||
const input = { type: "number" }; | ||
const node = new FloatNode(mockContext, input, []); | ||
expect(node.type).toBe("double"); | ||
expect(node.toFdrShape()).toEqual({ type: "double" }); | ||
expect(mockContext.errorCollector.addError).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle invalid type", () => { | ||
const input = { type: "string" }; | ||
const node = new FloatNode(mockContext, input, []); | ||
expect(node.type).toBeUndefined(); | ||
expect(node.toFdrShape()).toBeUndefined(); | ||
expect(mockContext.errorCollector.addError).toHaveBeenCalledWith( | ||
'Expected type "number" for numerical primitive, but got "string"', | ||
[], | ||
undefined, | ||
); | ||
}); | ||
|
||
it("should handle invalid format", () => { | ||
const input = { type: "number", format: "invalid" }; | ||
const node = new FloatNode(mockContext as ApiNodeContext, input, []); | ||
expect(node.type).toBeUndefined(); | ||
expect(node.toFdrShape()).toBeUndefined(); | ||
expect(mockContext.errorCollector.addError).toHaveBeenCalledWith( | ||
'Expected format for number primitive, but got "invalid"', | ||
[], | ||
undefined, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mega nit: i think it might be nicer to put these test files through out the code base as opposed to the top, that way you can see
object.node.ts
andobject.node.test.ts
side-by-side