Skip to content

Commit

Permalink
chore(openapi): upgrade from ir v23 -> v53 (#4745)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Sep 25, 2024
1 parent 47d5c9d commit 0d03d2e
Show file tree
Hide file tree
Showing 31 changed files with 398 additions and 183 deletions.
2 changes: 1 addition & 1 deletion generators/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"dependencies": {
"@fern-api/fs-utils": "workspace:*",
"@fern-api/generator-commons": "workspace:*",
"@fern-fern/ir-sdk": "0.0.2828",
"@fern-fern/ir-sdk": "53.9.0",
"js-yaml": "^4.1.0",
"lodash-es": "^4.17.21",
"openapi-types": "^12.1.3",
Expand Down
90 changes: 61 additions & 29 deletions generators/openapi/src/converters/servicesConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { isEqual, size } from "lodash-es";
import { OpenAPIV3 } from "openapi-types";
import urlJoin from "url-join";
import { isNonNullish } from "../../../commons/node_modules/@fern-api/core-utils/src";
import { getDeclaredTypeNameKey, getErrorTypeNameKey } from "../convertToOpenApi";
import { Mode } from "../writeOpenApi";
import { convertObject } from "./convertObject";
Expand Down Expand Up @@ -109,19 +110,36 @@ function convertHttpEndpoint({
fullPath = !fullPath.startsWith("/") ? `/${fullPath}` : fullPath;
const convertedHttpMethod = convertHttpMethod(httpEndpoint.method);
const convertedGlobalPathParameters = ir.pathParameters.map((pathParameter) =>
convertPathParameter({ pathParameter, examples: httpEndpoint.examples })
convertPathParameter({
pathParameter,
examples: httpEndpoint.userSpecifiedExamples.map((ex) => ex.example).filter(isNonNullish) ?? []
})
);
const convertedServicePathParameters = httpService.pathParameters.map((pathParameter) =>
convertPathParameter({ pathParameter, examples: httpEndpoint.examples })
convertPathParameter({
pathParameter,
examples: httpEndpoint.userSpecifiedExamples.map((ex) => ex.example).filter(isNonNullish) ?? []
})
);
const convertedEndpointPathParameters = httpEndpoint.pathParameters.map((pathParameter) =>
convertPathParameter({ pathParameter, examples: httpEndpoint.examples })
convertPathParameter({
pathParameter,
examples: httpEndpoint.userSpecifiedExamples.map((ex) => ex.example).filter(isNonNullish) ?? []
})
);
const convertedQueryParameters = httpEndpoint.queryParameters.map((queryParameter) =>
convertQueryParameter({ queryParameter, typesByName, examples: httpEndpoint.examples })
convertQueryParameter({
queryParameter,
typesByName,
examples: httpEndpoint.userSpecifiedExamples.map((ex) => ex.example).filter(isNonNullish) ?? []
})
);
const convertedHeaders = httpEndpoint.headers.map((header) =>
convertHeader({ httpHeader: header, typesByName, examples: httpEndpoint.examples })
convertHeader({
httpHeader: header,
typesByName,
examples: httpEndpoint.userSpecifiedExamples.map((ex) => ex.example).filter(isNonNullish) ?? []
})
);
const parameters: OpenAPIV3.ParameterObject[] = [
...convertedGlobalPathParameters,
Expand All @@ -144,15 +162,13 @@ function convertHttpEndpoint({
].join("_"),
tags: [tag],
parameters,
responses: {
...convertResponse({
httpResponse: httpEndpoint.response,
responseErrors: httpEndpoint.errors,
errorsByName,
errorDiscriminationStrategy,
examples: httpEndpoint.examples
})
},
responses: convertResponse({
httpResponse: httpEndpoint.response,
responseErrors: httpEndpoint.errors,
errorsByName,
errorDiscriminationStrategy,
examples: httpEndpoint.userSpecifiedExamples.map((ex) => ex.example).filter(isNonNullish) ?? []
}),
summary: httpEndpoint.displayName ?? undefined
};

Expand Down Expand Up @@ -182,7 +198,7 @@ function convertHttpEndpoint({
operationObject.requestBody = convertRequestBody({
httpRequest: httpEndpoint.requestBody,
typesByName,
examples: httpEndpoint.examples
examples: httpEndpoint.userSpecifiedExamples.map((ex) => ex.example).filter(isNonNullish) ?? []
});
}
return {
Expand Down Expand Up @@ -248,7 +264,7 @@ function convertRequestBody({
let exampleProperty: ExampleInlinedRequestBodyProperty | undefined = undefined;
if (examples.length > 0 && examples[0]?.request?.type === "inlinedRequestBody") {
exampleProperty = examples[0]?.request.properties.find((example) => {
return example.wireKey === property.name.wireValue;
return example.name.wireValue === property.name.wireValue;
});
}
return {
Expand Down Expand Up @@ -347,25 +363,25 @@ function convertResponse({
examples: ExampleEndpointCall[];
}): Record<string, OpenAPIV3.ResponseObject> {
const responseByStatusCode: Record<string, OpenAPIV3.ResponseObject> = {};
if (httpResponse == null) {
responseByStatusCode["204"] = {
description: ""
};
} else if (httpResponse.type === "json") {
if (httpResponse?.body?.type === "json") {
const convertedResponse: OpenAPIV3.MediaTypeObject = {
schema: convertTypeReference(httpResponse.responseBodyType)
schema: convertTypeReference(httpResponse.body.value.responseBodyType)
};

const openapiExamples: OpenAPIV3.MediaTypeObject["examples"] = {};
for (const example of examples) {
if (example.response.type === "ok" && example.response.body != null) {
if (
example.response.type === "ok" &&
example.response.value.type === "body" &&
example.response.value.value != null
) {
if (example.name && example.name.originalName !== "") {
openapiExamples[example.name.originalName] = {
value: example.response.body.jsonExample
value: example.response.value.value.jsonExample
};
} else {
openapiExamples[`Example${size(openapiExamples) + 1}`] = {
value: example.response.body.jsonExample
value: example.response.value.value.jsonExample
};
}
}
Expand All @@ -375,11 +391,15 @@ function convertResponse({
}

responseByStatusCode["200"] = {
description: httpResponse.docs ?? "",
description: httpResponse.body.value.docs ?? "",
content: {
"application/json": convertedResponse
}
};
} else {
responseByStatusCode["204"] = {
description: ""
};
}

ErrorDiscriminationStrategy._visit(errorDiscriminationStrategy, {
Expand Down Expand Up @@ -582,7 +602,7 @@ function convertPathParameter({
const openapiExamples: OpenAPIV3.ParameterObject["examples"] = {};
for (const example of examples) {
const pathParameterExample = [...example.servicePathParameters, ...example.endpointPathParameters].find(
(param) => param.key === pathParameter.name.originalName
(param) => param.name.originalName === pathParameter.name.originalName
);
if (pathParameterExample != null) {
if (example.name && example.name.originalName !== "") {
Expand All @@ -594,6 +614,10 @@ function convertPathParameter({
value: pathParameterExample.value.jsonExample
};
}

if (convertedParameter.example == null) {
convertedParameter.example = pathParameterExample.value.jsonExample;
}
}
}
if (size(openapiExamples) > 0) {
Expand Down Expand Up @@ -625,7 +649,7 @@ function convertQueryParameter({
const openapiExamples: OpenAPIV3.ParameterObject["examples"] = {};
for (const example of examples) {
const queryParameterExample = example.queryParameters.find(
(param) => param.wireKey === queryParameter.name.wireValue
(param) => param.name.wireValue === queryParameter.name.wireValue
);
if (queryParameterExample != null) {
if (example.name && example.name.originalName !== "") {
Expand All @@ -637,6 +661,10 @@ function convertQueryParameter({
value: queryParameterExample.value.jsonExample
};
}

if (convertedParameter.example == null) {
convertedParameter.example = queryParameterExample.value.jsonExample;
}
}
}
if (size(openapiExamples) > 0) {
Expand Down Expand Up @@ -666,7 +694,7 @@ function convertHeader({
const openapiExamples: OpenAPIV3.ParameterObject["examples"] = {};
for (const example of examples) {
const headerExample = [...example.serviceHeaders, ...example.endpointHeaders].find(
(headerFromExample) => headerFromExample.wireKey === httpHeader.name.wireValue
(headerFromExample) => headerFromExample.name.wireValue === httpHeader.name.wireValue
);
if (headerExample != null) {
if (example.name && example.name.originalName !== "") {
Expand All @@ -678,6 +706,10 @@ function convertHeader({
value: headerExample.value.jsonExample
};
}

if (convertedParameter.example == null) {
convertedParameter.example = headerExample.value.jsonExample;
}
}
}
if (size(openapiExamples) > 0) {
Expand Down
Loading

0 comments on commit 0d03d2e

Please sign in to comment.