-
Notifications
You must be signed in to change notification settings - Fork 351
Fix OpenAPI import: preserve description and decorators for anyOf/oneOf with null #8514
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f67bc38
Initial plan
Copilot e2052f1
Initial analysis and test for anyOf union null issue
Copilot 55d2fe4
Fix anyOf/oneOf union with null metadata extraction
Copilot e70b4a2
Merge branch 'main' into copilot/fix-8513
baywet d1b3c6c
docs: adds chronus changelog for anyof/oneof import
baywet 3e8df61
fix: types misalignments
baywet 1bc06dc
chore: formatting
baywet ad245ab
docs: updates changelog section
baywet 30086f4
chore: linting
baywet 8e2f452
chore: adds comments to contextualize the code
baywet 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 hidden or 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,7 @@ | ||
| --- | ||
| changeKind: fix | ||
| packages: | ||
| - "@typespec/openapi3" | ||
| --- | ||
|
|
||
| [converter] anyOf/oneOf type + type:null gets imported properly and maintains decorators, documentation,... |
This file contains hidden or 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 hidden or 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
181 changes: 181 additions & 0 deletions
181
packages/openapi3/test/tsp-openapi3/union-anyof-with-null.test.ts
This file contains hidden or 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,181 @@ | ||
| import OpenAPIParser from "@apidevtools/swagger-parser"; | ||
| import { OpenAPI } from "openapi-types"; | ||
| import { beforeAll, describe, expect, it } from "vitest"; | ||
| import { generateDataType } from "../../src/cli/actions/convert/generators/generate-model.js"; | ||
| import { TypeSpecUnion } from "../../src/cli/actions/convert/interfaces.js"; | ||
| import { transformComponentSchemas } from "../../src/cli/actions/convert/transforms/transform-component-schemas.js"; | ||
| import { createContext } from "../../src/cli/actions/convert/utils/context.js"; | ||
| import { OpenAPI3Document } from "../../src/types.js"; | ||
|
|
||
| describe("tsp-openapi: union anyOf with null", () => { | ||
baywet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let parser: OpenAPIParser; | ||
| let doc: OpenAPI.Document<{}>; | ||
|
|
||
| beforeAll(async () => { | ||
| parser = new OpenAPIParser(); | ||
| doc = await parser.bundle({ | ||
| openapi: "3.1.0", | ||
| info: { title: "Test", version: "1.0.0" }, | ||
| paths: {}, | ||
| components: { | ||
| schemas: { | ||
| ReasoningEffort: { | ||
| anyOf: [ | ||
| { | ||
| type: "string", | ||
| enum: ["minimal", "low", "medium", "high"], | ||
| default: "medium", | ||
| description: `Constrains effort on reasoning for | ||
| [reasoning models](https://platform.openai.com/docs/guides/reasoning). | ||
| Currently supported values are \`minimal\`, \`low\`, \`medium\`, and \`high\`. Reducing | ||
| reasoning effort can result in faster responses and fewer tokens used | ||
| on reasoning in a response.`, | ||
| }, | ||
| { | ||
| type: "null", | ||
| }, | ||
| ], | ||
| }, | ||
| NullableNumberWithConstraints: { | ||
| oneOf: [ | ||
| { | ||
| type: "number", | ||
| minimum: 0, | ||
| maximum: 100, | ||
| description: "A percentage value between 0 and 100", | ||
| }, | ||
| { | ||
| type: "null", | ||
| }, | ||
| ], | ||
| }, | ||
| ModelOrNull: { | ||
| anyOf: [ | ||
| { | ||
| $ref: "#/components/schemas/SomeModel", | ||
| }, | ||
| { | ||
| type: "null", | ||
| }, | ||
| ], | ||
| }, | ||
| SomeModel: { | ||
| type: "object", | ||
| properties: { | ||
| id: { type: "string" }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("generates proper TypeSpec code with description and null", () => { | ||
| const context = createContext(parser, doc as OpenAPI3Document); | ||
| const types: TypeSpecUnion[] = []; | ||
| transformComponentSchemas(context, types); | ||
|
|
||
| const union = types.find( | ||
| (t) => t.name === "ReasoningEffort" && t.kind === "union", | ||
| ) as TypeSpecUnion; | ||
| expect(union).toBeDefined(); | ||
| expect(union.doc).toBeTruthy(); | ||
|
|
||
| // Generate the actual TypeSpec code | ||
| const generatedCode = generateDataType(union, context); | ||
|
|
||
| // Verify the generated code contains all expected elements | ||
| expect(generatedCode).toContain("/**"); | ||
| expect(generatedCode).toContain("Constrains effort on reasoning"); | ||
| expect(generatedCode).toContain("*/"); | ||
| expect(generatedCode).toContain("union ReasoningEffort {"); | ||
| expect(generatedCode).toContain('"minimal"'); | ||
| expect(generatedCode).toContain('"low"'); | ||
| expect(generatedCode).toContain('"medium"'); | ||
| expect(generatedCode).toContain('"high"'); | ||
| expect(generatedCode).toContain("null"); | ||
| expect(generatedCode).toContain("}"); | ||
| }); | ||
|
|
||
| it("preserves description from oneOf members with constraints when one is null", () => { | ||
| const context = createContext(parser, doc as OpenAPI3Document); | ||
| const types: TypeSpecUnion[] = []; | ||
| transformComponentSchemas(context, types); | ||
|
|
||
| const union = types.find( | ||
| (t) => t.name === "NullableNumberWithConstraints" && t.kind === "union", | ||
| ) as TypeSpecUnion; | ||
| expect(union).toBeDefined(); | ||
| expect(union.doc).toBe("A percentage value between 0 and 100"); | ||
|
|
||
| // Check that decorators from the number schema are preserved | ||
| expect(union.decorators).toBeDefined(); | ||
| const hasMinConstraint = union.decorators.some((d) => d.name === "minValue"); | ||
| const hasMaxConstraint = union.decorators.some((d) => d.name === "maxValue"); | ||
| expect(hasMinConstraint).toBe(true); | ||
| expect(hasMaxConstraint).toBe(true); | ||
| }); | ||
|
|
||
| it("handles reference + null anyOf correctly", () => { | ||
| const context = createContext(parser, doc as OpenAPI3Document); | ||
| const types: TypeSpecUnion[] = []; | ||
| transformComponentSchemas(context, types); | ||
|
|
||
| const union = types.find( | ||
| (t) => t.name === "ModelOrNull" && t.kind === "union", | ||
| ) as TypeSpecUnion; | ||
| expect(union).toBeDefined(); | ||
| // For reference + null, there's no description on the ref itself, so doc should be undefined | ||
| expect(union.doc).toBeUndefined(); | ||
|
|
||
| const generatedCode = generateDataType(union, context); | ||
| expect(generatedCode).toContain("SomeModel"); | ||
| expect(generatedCode).toContain("null"); | ||
| }); | ||
|
|
||
| it("handles oneOf with null type array properly", async () => { | ||
| const docWithTypeArray = await parser.bundle({ | ||
| openapi: "3.1.0", | ||
| info: { title: "Test", version: "1.0.0" }, | ||
| paths: {}, | ||
| components: { | ||
| schemas: { | ||
| NullableString: { | ||
| type: ["string", "null"], | ||
| enum: ["value1", "value2", null], | ||
| default: "value1", | ||
| description: "A nullable string with enum values", | ||
| }, | ||
| NullableInteger: { | ||
| type: ["integer", "null"], | ||
| minimum: 1, | ||
| maximum: 10, | ||
| description: "A nullable integer between 1 and 10", | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const context = createContext(parser, docWithTypeArray as OpenAPI3Document); | ||
| const types: TypeSpecUnion[] = []; | ||
| transformComponentSchemas(context, types); | ||
|
|
||
| const stringUnion = types.find( | ||
| (t) => t.name === "NullableString" && t.kind === "union", | ||
| ) as TypeSpecUnion; | ||
| expect(stringUnion).toBeDefined(); | ||
| expect(stringUnion.doc).toBe("A nullable string with enum values"); | ||
|
|
||
| const integerUnion = types.find( | ||
| (t) => t.name === "NullableInteger" && t.kind === "union", | ||
| ) as TypeSpecUnion; | ||
| expect(integerUnion).toBeDefined(); | ||
| expect(integerUnion.doc).toBe("A nullable integer between 1 and 10"); | ||
|
|
||
| // Check that constraints are preserved for the integer union | ||
| const hasMinConstraint = integerUnion.decorators.some((d) => d.name === "minValue"); | ||
| const hasMaxConstraint = integerUnion.decorators.some((d) => d.name === "maxValue"); | ||
| expect(hasMinConstraint).toBe(true); | ||
| expect(hasMaxConstraint).toBe(true); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.