Skip to content

Commit

Permalink
(fix, openapi): x-fern-global-headers works with predefined types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Jun 27, 2024
1 parent 1e665fd commit ddf99a5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,19 @@ exports[`x-fern-global-headers x-fern-global-headers docs 1`] = `
"headers": {
"another_header": {
"env": "MY_HEADER_ENV",
"name": "anotherHeader",
"type": "string",
"name": undefined,
"type": "optional<string>",
},
"my-api-key": {
"env": undefined,
"name": "apiKeyAlias",
"name": "api-key-alias",
"type": "optional<string>",
},
"version": {
"env": "SQUARE_VERSION",
"name": "Square-Version",
"type": "literal<"2024-06-04">",
},
"x-api-key": {
"name": "apiKey",
"type": "string",
Expand Down Expand Up @@ -296,14 +301,19 @@ exports[`x-fern-global-headers x-fern-global-headers simple 1`] = `
"headers": {
"another_header": {
"env": "MY_HEADER_ENV",
"name": "anotherHeader",
"type": "string",
"name": undefined,
"type": "optional<string>",
},
"my-api-key": {
"env": undefined,
"name": "apiKeyAlias",
"name": "api-key-alias",
"type": "optional<string>",
},
"version": {
"env": "SQUARE_VERSION",
"name": "Square-Version",
"type": "literal<"2024-06-04">",
},
"x-api-key": {
"name": "apiKey",
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ x-fern-global-headers:
- header: another_header
optional: false
env: MY_HEADER_ENV
- header: version
name: Square-Version
env: SQUARE_VERSION
type: literal<"2024-06-04">
paths:
"/user/{userId}":
get:
Expand Down
55 changes: 37 additions & 18 deletions packages/cli/openapi-ir-to-fern/src/buildGlobalHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { ROOT_API_FILENAME } from "@fern-api/configuration";
import { RelativeFilePath } from "@fern-api/fs-utils";
import { GlobalHeader } from "@fern-api/openapi-ir-sdk";
import { RawSchemas } from "@fern-api/yaml-schema";
import { buildHeader } from "./buildHeader";
import { buildTypeReference } from "./buildTypeReference";
import { OpenApiIrConverterContext } from "./OpenApiIrConverterContext";
import { getTypeFromTypeReference } from "./utils/getTypeFromTypeReference";
import { wrapTypeReferenceAsOptional } from "./utils/wrapTypeReferenceAsOptional";

class HeaderWithCount {
Expand All @@ -24,7 +27,9 @@ const GLOBAL_HEADER_PERCENTAGE_THRESHOLD = 0.75;
const HEADERS_TO_IGNORE = new Set(...["Authorization"]);

export function buildGlobalHeaders(context: OpenApiIrConverterContext): void {
const predefinedGlobalHeaders = new Map(context.ir.globalHeaders?.map((header) => [header.header, header]));
const predefinedGlobalHeaders: Record<string, GlobalHeader> = Object.fromEntries(
(context.ir.globalHeaders ?? []).map((header) => [header.header, header])
);

const globalHeaders: Record<string, HeaderWithCount> = {};
for (const endpoint of context.ir.endpoints) {
Expand All @@ -34,7 +39,7 @@ export function buildGlobalHeaders(context: OpenApiIrConverterContext): void {
}
let headerWithCount = globalHeaders[header.name];
if (headerWithCount == null) {
const predefinedHeader = predefinedGlobalHeaders.get(header.name);
const predefinedHeader = predefinedGlobalHeaders[header.name];
const convertedHeader = buildHeader({
header: {
...header,
Expand All @@ -53,25 +58,39 @@ export function buildGlobalHeaders(context: OpenApiIrConverterContext): void {

const globalHeaderThreshold = context.ir.endpoints.length * GLOBAL_HEADER_PERCENTAGE_THRESHOLD;

for (const [headerName, header] of Object.entries(predefinedGlobalHeaders)) {
let schema: RawSchemas.HttpHeaderSchema = "optional<string>";

if (header.name == null && header.env == null && typeof header.schema === "string") {
schema = header.schema;
} else if (header != null) {
schema = {
name: header.name,
env: header.env,
type:
header.schema != null
? getTypeFromTypeReference(
buildTypeReference({
schema: header.schema,
context,
fileContainingReference: RelativeFilePath.of("api.yml")
})
) ?? "optional<string>"
: "optional<string>"
};
}
context.builder.addGlobalHeader({
name: headerName,
schema
});
}

for (const [headerName, header] of Object.entries(globalHeaders)) {
const predefinedHeader = predefinedGlobalHeaders.get(headerName);
let isRequired = header.count === context.ir.endpoints.length;
const predefinedHeader = predefinedGlobalHeaders[headerName];
const isRequired = header.count === context.ir.endpoints.length;
const isOptional = header.count >= globalHeaderThreshold;
if (predefinedHeader != null) {
isRequired = (isRequired && predefinedHeader.optional !== true) || predefinedHeader.optional === false;
let schema: RawSchemas.HttpHeaderSchema;
if (typeof header.schema === "string") {
schema = header.schema;
} else {
schema = {
...header.schema,
env: predefinedHeader.env ?? header.schema.env
};
}
context.builder.addGlobalHeader({
name: headerName,
schema: isRequired ? schema : wrapTypeReferenceAsOptional(schema)
});
continue; // already added
} else if (isRequired) {
context.builder.addGlobalHeader({
name: headerName,
Expand Down

0 comments on commit ddf99a5

Please sign in to comment.