Skip to content

Commit

Permalink
(fix): Importer handles adding imports from api.yml (#3100)
Browse files Browse the repository at this point in the history
(fix): Importer handles adding imports from api.yml
  • Loading branch information
dsinghvi authored Mar 3, 2024
1 parent 11d7306 commit f43cca2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
14 changes: 13 additions & 1 deletion packages/cli/openapi-ir-to-fern/src/FernDefnitionBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbsoluteFilePath, dirname, relative, RelativeFilePath } from "@fern-api/fs-utils";
import { OpenApiIntermediateRepresentation } from "@fern-api/openapi-ir-sdk";
import { FERN_PACKAGE_MARKER_FILENAME } from "@fern-api/project-configuration";
import { FERN_PACKAGE_MARKER_FILENAME, ROOT_API_FILENAME } from "@fern-api/project-configuration";
import { RawSchemas, RootApiFileSchema, visitRawEnvironmentDeclaration } from "@fern-api/yaml-schema";
import { camelCase } from "lodash-es";
import { basename, extname } from "path";
Expand Down Expand Up @@ -177,6 +177,18 @@ export class FernDefinitionBuilderImpl implements FernDefinitionBuilder {
return undefined;
}
const importPrefix = camelCase(basename(fileToImport, extname(fileToImport)).replaceAll("__package__", "root"));

if (file === RelativeFilePath.of(ROOT_API_FILENAME)) {
if (this.rootApiFile.imports == null) {
this.rootApiFile.imports = {};
}
this.rootApiFile.imports[importPrefix] = relative(
dirname(AbsoluteFilePath.of(`/${file}`)),
AbsoluteFilePath.of(`/${fileToImport}`)
);
return importPrefix;
}

const fernFile = this.getOrCreateFile(file);
if (fernFile.imports == null) {
fernFile.imports = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3528,6 +3528,9 @@ For recurring transfer schedules, \`endsOn\`, \`count\`, and \`amountLimit\` are
"error-discrimination": {
"strategy": "status-code",
},
"imports": {
"root": "__package__.yml",
},
"name": "api",
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ exports[`open api parser buzzshot simple 1`] = `
{
"definitionFiles": {
"api.yml": {
"imports": {
"root": "__package__.yml",
},
"service": {
"auth": false,
"base-path": "",
Expand Down Expand Up @@ -901,6 +898,9 @@ exports[`open api parser buzzshot simple 1`] = `
"error-discrimination": {
"strategy": "status-code",
},
"imports": {
"root": "__package__.yml",
},
"name": "api",
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ exports[`open api parser suger simple 1`] = `
{
"definitionFiles": {
"api.yml": {
"imports": {
"root": "__package__.yml",
},
"service": {
"auth": false,
"base-path": "",
Expand Down Expand Up @@ -7389,6 +7386,9 @@ the same customer, dimension, and time, but a different quantity.",
"error-discrimination": {
"strategy": "status-code",
},
"imports": {
"root": "__package__.yml",
},
"name": "api",
},
}
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/openapi-ir-to-fern/src/buildGlobalHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { RelativeFilePath } from "@fern-api/fs-utils";
import { FERN_PACKAGE_MARKER_FILENAME } from "@fern-api/project-configuration";
import { ROOT_API_FILENAME } from "@fern-api/project-configuration";
import { RawSchemas } from "@fern-api/yaml-schema";
import { buildHeader } from "./buildHeader";
import { OpenApiIrConverterContext } from "./OpenApiIrConverterContext";
import { getEndpointLocation } from "./utils/getEndpointLocation";
import { wrapTypeReferenceAsOptional } from "./utils/wrapTypeReferenceAsOptional";

class HeaderWithCount {
Expand Down Expand Up @@ -30,11 +31,12 @@ export function buildGlobalHeaders(context: OpenApiIrConverterContext): void {
if (HEADERS_TO_IGNORE.has(header.name)) {
continue;
}
const { file } = getEndpointLocation(endpoint);
let headerWithCount = globalHeaders[header.name];
if (headerWithCount == null) {
const convertedHeader = buildHeader({
header,
fileContainingReference: RelativeFilePath.of(FERN_PACKAGE_MARKER_FILENAME),
fileContainingReference: RelativeFilePath.of(ROOT_API_FILENAME),
context
});
headerWithCount = new HeaderWithCount(convertedHeader);
Expand Down

0 comments on commit f43cca2

Please sign in to comment.