From c8e4b934b58b4caadb08cff68266f0bddab21881 Mon Sep 17 00:00:00 2001 From: Deep Singhvi Date: Tue, 16 Apr 2024 04:18:24 -0400 Subject: [PATCH] (fix): discriminated union schema examples don't contain discriminants (#3386) (fix): discriminated union schema examples dont contain discriminants --- .../src/openapi/v3/generateIr.ts | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/cli/openapi-parser/src/openapi/v3/generateIr.ts b/packages/cli/openapi-parser/src/openapi/v3/generateIr.ts index ce26dd52187..f2968583c6e 100644 --- a/packages/cli/openapi-parser/src/openapi/v3/generateIr.ts +++ b/packages/cli/openapi-parser/src/openapi/v3/generateIr.ts @@ -124,7 +124,19 @@ export function generateIr({ }) .filter((entry) => entry.length > 0) ); - const exampleEndpointFactory = new ExampleEndpointFactory(schemasWithExample, context.logger); + + const schemasWithoutDiscriminants = maybeRemoveDiscriminantsFromSchemas(schemasWithExample, context); + const schemas: Record = {}; + for (const [key, schemaWithExample] of Object.entries(schemasWithoutDiscriminants)) { + const schema = convertSchemaWithExampleToSchema(schemaWithExample); + if (context.isSchemaExcluded(key)) { + continue; + } + schemas[key] = schema; + taskContext.logger.debug(`Converted schema ${key}`); + } + + const exampleEndpointFactory = new ExampleEndpointFactory(schemasWithoutDiscriminants, context.logger); const endpoints = endpointsWithExample.map((endpointWithExample): Endpoint => { // if x-fern-examples is not present, generate an example const extensionExamples = endpointWithExample.examples; @@ -188,16 +200,6 @@ export function generateIr({ }; }); - const schemas: Record = {}; - for (const [key, schemaWithExample] of Object.entries(schemasWithExample)) { - const schema = convertSchemaWithExampleToSchema(schemaWithExample); - if (context.isSchemaExcluded(key)) { - continue; - } - schemas[key] = schema; - taskContext.logger.debug(`Converted schema ${key}`); - } - const groupInfo = getFernGroups({ document: openApi, context }); const ir: OpenApiIntermediateRepresentation = { @@ -220,7 +222,7 @@ export function generateIr({ endpoints, webhooks, channel: [], - schemas: maybeRemoveDiscriminantsFromSchemas(schemas, context), + schemas, securitySchemes, hasEndpointsMarkedInternal: endpoints.some((endpoint) => endpoint.internal), errors: context.getErrors(), @@ -233,10 +235,10 @@ export function generateIr({ } function maybeRemoveDiscriminantsFromSchemas( - schemas: Record, + schemas: Record, context: AbstractOpenAPIV3ParserContext -): Record { - const result: Record = {}; +): Record { + const result: Record = {}; for (const [schemaId, schema] of Object.entries(schemas)) { if (schema.type !== "object") { result[schemaId] = schema; @@ -251,7 +253,7 @@ function maybeRemoveDiscriminantsFromSchemas( continue; } - const schemaWithoutDiscriminants: Schema.Object_ = { + const schemaWithoutDiscriminants: SchemaWithExample.Object_ = { ...schema, type: "object", properties: schema.properties.filter((objectProperty) => {