Skip to content

Commit

Permalink
fix(openapi): handle when custom config for filename is optional (#4750)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Sep 25, 2024
1 parent f81aab5 commit b36fd9a
Show file tree
Hide file tree
Showing 25 changed files with 354 additions and 13 deletions.
2 changes: 1 addition & 1 deletion generators/openapi/src/customConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GeneratorConfig } from "@fern-api/generator-commons";
export interface FernOpenapiCustomConfig {
format: "yaml" | "json";
customOverrides: Record<string, unknown>;
filename: string;
filename?: string;
}

const DEFAULT_FERN_OPENAPI_CUSTOM_CONFIG: FernOpenapiCustomConfig = {
Expand Down
29 changes: 18 additions & 11 deletions generators/openapi/src/writeOpenApi.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { IntermediateRepresentation } from "@fern-fern/ir-sdk/api";
import * as IrSerialization from "@fern-fern/ir-sdk/serialization";
import { readFile, writeFile } from "fs/promises";
import { writeFile } from "fs/promises";
import yaml from "js-yaml";
import merge from "lodash-es/merge";
import path from "path";
import { convertToOpenApi } from "./convertToOpenApi";
import { getCustomConfig } from "./customConfig";
import {
GeneratorNotificationService,
GeneratorExecParsing,
GeneratorUpdate,
ExitStatusUpdate,
parseGeneratorConfig,
Expand Down Expand Up @@ -41,28 +39,37 @@ export async function writeOpenApi(mode: Mode, pathToConfig: string): Promise<vo
ir,
mode
});
// eslint-disable-next-line no-console
console.log(`openapi before override ${JSON.stringify(openapi)}`);

if (customConfig.customOverrides != null) {
openapi = await mergeWithOverrides({
data: openapi,
overrides: customConfig.customOverrides
});
// eslint-disable-next-line no-console
console.log(`openapi after override ${JSON.stringify(openapi)}`);
}

let filename: string = customConfig.filename ?? "openapi.yml";
if (customConfig.format === "json") {
await writeFile(
path.join(config.output.path, replaceExtension(customConfig.filename, "json")),
JSON.stringify(openapi, undefined, 2)
);
filename = path.join(config.output.path, replaceExtension(filename, "json"));
await writeFile(filename, JSON.stringify(openapi, undefined, 2));
} else {
const filename =
customConfig.filename.endsWith("yml") || customConfig.filename.endsWith("yaml")
? customConfig.filename
: replaceExtension(customConfig.filename, "yml");
filename =
filename.endsWith("yml") || filename.endsWith("yaml")
? filename
: replaceExtension(filename, "yml");
await writeFile(path.join(config.output.path, filename), yaml.dump(openapi));
}
await generatorLoggingClient.sendUpdate(GeneratorUpdate.exitStatusUpdate(ExitStatusUpdate.successful({})));
} catch (e) {
if (e instanceof Error) {
// eslint-disable-next-line no-console
console.log((e as Error)?.message);
// eslint-disable-next-line no-console
console.log((e as Error)?.stack);
}
await generatorLoggingClient.sendUpdate(
GeneratorUpdate.exitStatusUpdate(
ExitStatusUpdate.error({
Expand Down
9 changes: 9 additions & 0 deletions generators/openapi/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
- version: 0.1.4
createdAt: '2024-09-25'
changelogEntry:
- type: fix
summary: |
The generator now handles `config.filename` being null so that the
configuration for the generator can continue to be backwards compatible.
irVersion: 53

- version: 0.1.3
createdAt: '2024-09-25'
changelogEntry:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
187 changes: 187 additions & 0 deletions seed/openapi/enum/custom-overrides/openapi.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions seed/openapi/enum/no-custom-config/.mock/definition/path-param.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b36fd9a

Please sign in to comment.