forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenAPI3: Fix more issues with inline properties (microsoft#2884)
fix microsoft#2882 Fix more issues when properties that should be inline are wrapped in `allOf` --------- Co-authored-by: Mark Cowlishaw <[email protected]>
- Loading branch information
1 parent
8ed1d82
commit 9726b3d
Showing
8 changed files
with
175 additions
and
62 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
.changeset/fix-openapi3-more-circular-inline-issues-2024-1-5-16-53-4.md
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,5 @@ | ||
--- | ||
"@typespec/compiler": minor | ||
--- | ||
|
||
Emitter framework: `ObjectBuilder` will keep track when built using a `Placeholder` allowing data to be carried over when chaining `ObjectBuilder` |
5 changes: 5 additions & 0 deletions
5
.changeset/fix-openapi3-more-circular-inline-issues-2024-1-5-16-55-16.md
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,5 @@ | ||
--- | ||
"@typespec/openapi3": patch | ||
--- | ||
|
||
Fix issues with `nullable` properties used in a cycle being wrapped in `allOf` when not needed |
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
48 changes: 48 additions & 0 deletions
48
packages/compiler/test/emitter-framework/object-builder.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,48 @@ | ||
import { beforeEach, describe, expect, it } from "vitest"; | ||
import { ObjectBuilder } from "../../src/emitter-framework/builders/object-builder.js"; | ||
import { Placeholder } from "../../src/emitter-framework/placeholder.js"; | ||
|
||
describe("ObjectBuilder", () => { | ||
it("create simple builder", () => { | ||
const builder = new ObjectBuilder({ | ||
foo: "bar", | ||
}); | ||
expect(builder.foo).toBe("bar"); | ||
}); | ||
|
||
it("create adds elements to builder", () => { | ||
const builder = new ObjectBuilder({ | ||
foo: "bar", | ||
}); | ||
builder.set("bar", "baz"); | ||
expect(builder.foo).toBe("bar"); | ||
expect(builder.bar).toBe("baz"); | ||
}); | ||
|
||
describe("when working with placeholders", () => { | ||
let placeholder: Placeholder<any>; | ||
|
||
beforeEach(() => { | ||
placeholder = new Placeholder(); | ||
}); | ||
|
||
it("has no values to start with", () => { | ||
const builder = new ObjectBuilder(placeholder); | ||
expect(builder.foo).toBeUndefined(); | ||
}); | ||
|
||
it("resolve values from placeholder", () => { | ||
const builder = new ObjectBuilder(placeholder); | ||
expect(builder.foo).toBeUndefined(); | ||
placeholder.setValue({ foo: "bar" }); | ||
expect(builder.foo).toBe("bar"); | ||
}); | ||
|
||
it("create object builder from another object builder with a placeholder", () => { | ||
const builder1 = new ObjectBuilder(placeholder); | ||
const builder2 = new ObjectBuilder(builder1); | ||
placeholder.setValue({ foo: "bar" }); | ||
expect(builder2.foo).toBe("bar"); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { deepStrictEqual, ok } from "assert"; | ||
import { describe, expect, it } from "vitest"; | ||
import { OpenAPI3SchemaProperty } from "../src/types.js"; | ||
import { oapiForModel, openApiFor } from "./test-host.js"; | ||
|
||
describe("openapi3: nullable properties", () => { | ||
it("makes nullable schema when union with null", async () => { | ||
const res = await openApiFor( | ||
` | ||
model Thing { | ||
id: string; | ||
properties: Thing | null; | ||
} | ||
op doStuff(): Thing; | ||
` | ||
); | ||
deepStrictEqual(res.components.schemas.Thing.properties.properties, { | ||
type: "object", | ||
allOf: [{ $ref: "#/components/schemas/Thing" }], | ||
nullable: true, | ||
}); | ||
}); | ||
|
||
it("handles a nullable enum", async () => { | ||
const res = await oapiForModel( | ||
"X", | ||
` | ||
enum A { | ||
a: 1 | ||
} | ||
model X { | ||
prop: A | null | ||
} | ||
` | ||
); | ||
deepStrictEqual(res.schemas.X.properties.prop.oneOf, [ | ||
{ | ||
$ref: "#/components/schemas/A", | ||
}, | ||
]); | ||
ok(res.schemas.X.properties.prop.nullable); | ||
}); | ||
|
||
describe("when used in circular references", () => { | ||
async function expectInCircularReference(ref: string, value: OpenAPI3SchemaProperty) { | ||
const res = await openApiFor( | ||
` | ||
model Test { | ||
children: ${ref} | null; | ||
} | ||
op test(filters: ${ref}): {}[]; | ||
` | ||
); | ||
expect(res.components.schemas.Test.properties.children).toEqual(value); | ||
} | ||
it("keep nullable array inline", async () => { | ||
await expectInCircularReference("Test[]", { | ||
type: "array", | ||
items: { $ref: "#/components/schemas/Test" }, | ||
nullable: true, | ||
}); | ||
}); | ||
|
||
it("keep nullable Record<T> inline", async () => { | ||
await expectInCircularReference("Record<Test>", { | ||
type: "object", | ||
additionalProperties: { $ref: "#/components/schemas/Test" }, | ||
nullable: true, | ||
}); | ||
}); | ||
}); | ||
}); |
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