|
1 | 1 | import fs from "node:fs";
|
2 |
| -import type { OpenAPI3 } from "../src/types.js"; |
3 | 2 | import openapiTS from "../dist/index.js";
|
| 3 | +import type { OpenAPI3 } from "../src/types.js"; |
4 | 4 |
|
5 | 5 | const BOILERPLATE = `/**
|
6 | 6 | * This file was auto-generated by openapi-typescript.
|
@@ -457,7 +457,7 @@ export interface external {
|
457 | 457 |
|
458 | 458 | export type operations = Record<string, never>;
|
459 | 459 | `);
|
460 |
| - }) |
| 460 | + }); |
461 | 461 | });
|
462 | 462 |
|
463 | 463 | describe("3.1", () => {
|
@@ -546,7 +546,7 @@ export type operations = Record<string, never>;
|
546 | 546 | `);
|
547 | 547 | });
|
548 | 548 |
|
549 |
| - test("discriminator (oneOf)", async () => { |
| 549 | + test("discriminator with explicit mapping (oneOf)", async () => { |
550 | 550 | const schema: OpenAPI3 = {
|
551 | 551 | openapi: "3.1",
|
552 | 552 | info: { title: "test", version: "1.0" },
|
@@ -625,6 +625,93 @@ export interface components {
|
625 | 625 |
|
626 | 626 | export type external = Record<string, never>;
|
627 | 627 |
|
| 628 | +export type operations = Record<string, never>; |
| 629 | +`); |
| 630 | + }); |
| 631 | + |
| 632 | + test("discriminator with implicit mapping (oneOf)", async () => { |
| 633 | + const schema: OpenAPI3 = { |
| 634 | + openapi: "3.1", |
| 635 | + info: { title: "test", version: "1.0" }, |
| 636 | + components: { |
| 637 | + schemas: { |
| 638 | + Pet: { |
| 639 | + oneOf: [{ $ref: "#/components/schemas/Cat" }, { $ref: "#/components/schemas/Dog" }, { $ref: "#/components/schemas/Lizard" }], |
| 640 | + discriminator: { |
| 641 | + propertyName: "petType", |
| 642 | + }, |
| 643 | + } as any, |
| 644 | + Cat: { |
| 645 | + type: "object", |
| 646 | + properties: { |
| 647 | + name: { type: "string" }, |
| 648 | + petType: { type: "string", enum: ["cat"] }, |
| 649 | + }, |
| 650 | + required: ["petType"], |
| 651 | + }, |
| 652 | + Dog: { |
| 653 | + type: "object", |
| 654 | + properties: { |
| 655 | + bark: { type: "string" }, |
| 656 | + petType: { type: "string", enum: ["dog"] }, |
| 657 | + }, |
| 658 | + required: ["petType"], |
| 659 | + }, |
| 660 | + Lizard: { |
| 661 | + type: "object", |
| 662 | + properties: { |
| 663 | + lovesRocks: { type: "boolean" }, |
| 664 | + petType: { type: "string", enum: ["lizard"] }, |
| 665 | + }, |
| 666 | + required: ["petType"], |
| 667 | + }, |
| 668 | + Person: { |
| 669 | + type: "object", |
| 670 | + required: ["pet"], |
| 671 | + properties: { |
| 672 | + pet: { oneOf: [{ $ref: "#/components/schemas/Pet" }] }, |
| 673 | + }, |
| 674 | + }, |
| 675 | + }, |
| 676 | + }, |
| 677 | + }; |
| 678 | + const generated = await openapiTS(schema); |
| 679 | + expect(generated).toBe(`${BOILERPLATE} |
| 680 | +export type paths = Record<string, never>; |
| 681 | +
|
| 682 | +export type webhooks = Record<string, never>; |
| 683 | +
|
| 684 | +export interface components { |
| 685 | + schemas: { |
| 686 | + Pet: components["schemas"]["Cat"] | components["schemas"]["Dog"] | components["schemas"]["Lizard"]; |
| 687 | + Cat: { |
| 688 | + name?: string; |
| 689 | + /** @enum {string} */ |
| 690 | + petType: "cat"; |
| 691 | + }; |
| 692 | + Dog: { |
| 693 | + bark?: string; |
| 694 | + /** @enum {string} */ |
| 695 | + petType: "dog"; |
| 696 | + }; |
| 697 | + Lizard: { |
| 698 | + lovesRocks?: boolean; |
| 699 | + /** @enum {string} */ |
| 700 | + petType: "lizard"; |
| 701 | + }; |
| 702 | + Person: { |
| 703 | + pet: components["schemas"]["Pet"]; |
| 704 | + }; |
| 705 | + }; |
| 706 | + responses: never; |
| 707 | + parameters: never; |
| 708 | + requestBodies: never; |
| 709 | + headers: never; |
| 710 | + pathItems: never; |
| 711 | +} |
| 712 | +
|
| 713 | +export type external = Record<string, never>; |
| 714 | +
|
628 | 715 | export type operations = Record<string, never>;
|
629 | 716 | `);
|
630 | 717 | });
|
|
0 commit comments