Skip to content

Commit

Permalink
(feat, cli): introduce x-fern-undiscriminated extension (#4048)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Jul 12, 2024
1 parent 8301e1b commit 5a7f726
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,19 @@ export const FernOpenAPIExtension = {
/**
* Allows a user to configure the property name for the schema.
*/
FERN_PROPERTY_NAME: "x-fern-property-name"
FERN_PROPERTY_NAME: "x-fern-property-name",

/**
* Allows a user to configure that a union wiht a discriminant should
* be undiscriminated.
*
* Shape:
* x-fern-undiscriminated: true
* oneOf:
* - $ref: components/schemas/Triangle
* - $ref: components/schemas/Square
*/
IS_UNDISCRIMINATED: "x-fern-undiscriminated"
} as const;

export type FernOpenAPIExtension = Values<typeof FernOpenAPIExtension>;
23 changes: 12 additions & 11 deletions packages/cli/openapi-parser/src/schema/convertSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,33 +451,34 @@ export function convertSchemaObject(

// handle oneOf
if (schema.oneOf != null && schema.oneOf.length > 0) {
const isUndiscriminated = getExtension(schema, FernOpenAPIExtension.IS_UNDISCRIMINATED);
if (
schema.discriminator != null &&
schema.discriminator.mapping != null &&
Object.keys(schema.discriminator.mapping).length > 0
) {
if (!context.options.discriminatedUnionV2) {
return convertDiscriminatedOneOf({
if (context.options.discriminatedUnionV2 || isUndiscriminated) {
return convertUndiscriminatedOneOfWithDiscriminant({
nameOverride,
generatedName,
breadcrumbs,
description,
discriminator: schema.discriminator,
properties: schema.properties ?? {},
required: schema.required,
wrapAsNullable,
context,
groupName
groupName,
discriminator: schema.discriminator
});
} else {
return convertUndiscriminatedOneOfWithDiscriminant({
return convertDiscriminatedOneOf({
nameOverride,
generatedName,
breadcrumbs,
description,
discriminator: schema.discriminator,
properties: schema.properties ?? {},
required: schema.required,
wrapAsNullable,
context,
groupName,
discriminator: schema.discriminator
groupName
});
}
} else if (schema.oneOf.length === 1 && schema.oneOf[0] != null) {
Expand Down Expand Up @@ -516,7 +517,7 @@ export function convertSchemaObject(
}

const maybeDiscriminant = getDiscriminant({ schemas: schema.oneOf, context });
if (maybeDiscriminant != null && !context.options.discriminatedUnionV2) {
if (maybeDiscriminant != null && !context.options.discriminatedUnionV2 && !isUndiscriminated) {
return convertDiscriminatedOneOfWithVariants({
nameOverride,
generatedName,
Expand Down

0 comments on commit 5a7f726

Please sign in to comment.