Skip to content
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

openapi3 - fixes bug with circular references in unions #3908

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Fixes bug where circular references in unions caused an empty object to be emitted instead of a ref.
41 changes: 24 additions & 17 deletions packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,25 +521,16 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
}
}

if (schemaMembers.length === 0) {
if (nullable) {
// This union is equivalent to just `null` but OA3 has no way to specify
// null as a value, so we throw an error.
reportDiagnostic(program, { code: "union-null", target: union });
return new ObjectBuilder({});
} else {
// completely empty union can maybe only happen with bugs?
compilerAssert(false, "Attempting to emit an empty union");
}
}

if (schemaMembers.length === 1) {
const wrapWithObjectBuilder = (
schemaMember: { schema: any; type: Type | null },
{ applyNullable }: { applyNullable: boolean }
): ObjectBuilder<OpenAPI3Schema> => {
// we can just return the single schema member after applying nullable
const schema = schemaMembers[0].schema;
const type = schemaMembers[0].type;
const schema = schemaMember.schema;
const type = schemaMember.type;
const additionalProps: Partial<OpenAPI3Schema> = this.#applyConstraints(union, {});

if (nullable) {
if (applyNullable && nullable) {
additionalProps.nullable = true;
}

Expand Down Expand Up @@ -567,10 +558,26 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
return merged;
}
}
};

if (schemaMembers.length === 0) {
if (nullable) {
// This union is equivalent to just `null` but OA3 has no way to specify
// null as a value, so we throw an error.
reportDiagnostic(program, { code: "union-null", target: union });
return new ObjectBuilder({});
} else {
// completely empty union can maybe only happen with bugs?
compilerAssert(false, "Attempting to emit an empty union");
}
}

if (schemaMembers.length === 1) {
return wrapWithObjectBuilder(schemaMembers[0], { applyNullable: true });
}

const schema: OpenAPI3Schema = {
[ofType]: schemaMembers.map((m) => m.schema),
[ofType]: schemaMembers.map((m) => wrapWithObjectBuilder(m, { applyNullable: false })),
};

if (nullable) {
Expand Down
16 changes: 16 additions & 0 deletions packages/openapi3/test/circular-references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ describe("openapi3: circular reference", () => {
},
});
});

it("can reference itself via a union property", async () => {
const res = await oapiForModel(
"Pet",
`
model Pet { parents?: string | Pet };
`
);

deepStrictEqual(res.schemas.Pet, {
type: "object",
properties: {
parents: { anyOf: [{ type: "string" }, { $ref: "#/components/schemas/Pet" }] },
},
});
});
});
Loading