Skip to content

Commit

Permalink
fix(cli): generate error examples in OpenAPI importer (#4812)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Oct 6, 2024
1 parent 71a30ac commit 7a0f047
Show file tree
Hide file tree
Showing 57 changed files with 95,088 additions and 7,163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { hasIncompleteExample } from "./hasIncompleteExample";
import { OpenAPIV3ParserContext } from "./OpenAPIV3ParserContext";
import { runResolutions } from "./runResolutions";
import { getSchemas } from "../../utils/getSchemas";
import { ExampleTypeFactory } from "../../schema/examples/ExampleTypeFactory";

export function generateIr({
openApi,
Expand Down Expand Up @@ -187,6 +188,7 @@ export function generateIr({
taskContext.logger.debug(`Converted schema ${key}`);
}

const exampleTypeFactory = new ExampleTypeFactory(schemasWithDiscriminants);
const exampleEndpointFactory = new ExampleEndpointFactory(schemasWithDiscriminants, context.logger);
const endpoints = endpointsWithExample.map((endpointWithExample): Endpoint => {
// if x-fern-examples is not present, generate an example
Expand Down Expand Up @@ -259,27 +261,49 @@ export function generateIr({
}),
examples,
errors: mapValues(endpointWithExample.errors, (error): HttpError => {
const examples = error.fullExamples
?.map((example): ErrorExample | undefined => {
const fullExample = convertToFullExample(example.value);

if (fullExample == null) {
return undefined;
}

return {
name: example.name,
description: example.description,
example: fullExample
};
})
.filter(isNonNullish);

if (examples?.length === 0) {
const generatedExample = exampleTypeFactory.buildExample({
schema: error.schema,
example: undefined,
exampleId: undefined,
skipReadonly: false,
options: {
ignoreOptionals: false,
isParameter: false
}
});
if (generatedExample != null) {
examples.push({
name: undefined,
description: undefined,
example: generatedExample
});
}
}

return {
generatedName: error.generatedName,
nameOverride: error.nameOverride,
schema: convertSchemaWithExampleToSchema(error.schema),
description: error.description,
source: error.source,
examples: error.fullExamples
?.map((example): ErrorExample | undefined => {
const fullExample = convertToFullExample(example.value);

if (fullExample == null) {
return undefined;
}

return {
name: example.name,
description: example.description,
example: fullExample
};
})
.filter(isNonNullish)
examples
};
})
};
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit 7a0f047

Please sign in to comment.