Skip to content

Commit

Permalink
(fix, openapi): fall back to default status code if none provided (#3834
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dsinghvi authored Jun 11, 2024
1 parent c2ff9c7 commit 521af41
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`open api ir to fern default-content docs 1`] = `
{
"definitionFiles": {
"generativelanguage.yml": {
"imports": {
"root": "__package__.yml",
},
"service": {
"auth": false,
"base-path": "",
"endpoints": {
"GenerateContent": {
"auth": false,
"docs": undefined,
"examples": [
{
"path-parameters": {
"model": "model",
},
"response": {
"body": {
"bar": "bar",
},
},
},
],
"method": "POST",
"pagination": undefined,
"path": "/generateContent",
"path-parameters": {
"model": "string",
},
"response": {
"docs": "Successful operation",
"type": "root.GenerateContentResponse",
},
},
},
},
},
},
"packageMarkerFile": {
"types": {
"GenerateContentResponse": {
"docs": undefined,
"properties": {
"bar": "optional<string>",
},
},
},
},
"rootApiFile": {
"default-environment": "Default",
"display-name": "Default Content Type",
"environments": {
"Default": "https://ai.com",
},
"error-discrimination": {
"strategy": "status-code",
},
"name": "api",
},
}
`;

exports[`open api ir to fern default-content simple 1`] = `
{
"definitionFiles": {
"generativelanguage.yml": {
"imports": {
"root": "__package__.yml",
},
"service": {
"auth": false,
"base-path": "",
"endpoints": {
"GenerateContent": {
"auth": false,
"docs": undefined,
"examples": [
{
"path-parameters": {
"model": "model",
},
"response": {
"body": {
"bar": "bar",
},
},
},
],
"method": "POST",
"pagination": undefined,
"path": "/generateContent",
"path-parameters": {
"model": "string",
},
"response": {
"docs": "Successful operation",
"type": "root.GenerateContentResponse",
},
},
},
},
},
},
"packageMarkerFile": {
"types": {
"GenerateContentResponse": {
"docs": undefined,
"properties": {
"bar": "optional<string>",
},
},
},
},
"rootApiFile": {
"default-environment": "Default",
"display-name": "Default Content Type",
"environments": {
"Default": "https://ai.com",
},
"error-discrimination": {
"strategy": "status-code",
},
"name": "api",
},
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { testConvertOpenAPI } from "./testConvertOpenApi";

describe("open api ir to fern", () => {
testConvertOpenAPI("default-content", "openapi.yml");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
openapi: 3.0.3
info:
title: Default Content Type
version: v1
servers:
- url: https://ai.com
paths:
"/generateContent":
post:
tags:
- generativelanguage
operationId: GenerateContent
parameters:
- name: model
in: path
required: true
schema:
type: string
responses:
default:
description: Successful operation
content:
application/json:
schema:
"$ref": "#/components/schemas/GenerateContentResponse"
components:
schemas:
GenerateContentResponse:
properties:
bar:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`open api parser default-content parse open api 1`] = `
{
"basePath": null,
"channel": [],
"description": null,
"endpoints": [
{
"audiences": [],
"authed": false,
"availability": null,
"description": null,
"errors": {},
"examples": [],
"generatedRequestName": "GenerateContentRequest",
"headers": [],
"idempotent": null,
"internal": null,
"method": "POST",
"operationId": "GenerateContent",
"pagination": null,
"path": "/generateContent",
"pathParameters": [
{
"description": null,
"name": "model",
"schema": {
"description": null,
"generatedName": "GenerateContentRequestModel",
"groupName": null,
"nameOverride": null,
"schema": {
"default": null,
"format": null,
"maxLength": null,
"minLength": null,
"pattern": null,
"type": "string",
},
"type": "primitive",
},
"variableReference": null,
},
],
"queryParameters": [],
"request": null,
"requestNameOverride": null,
"response": {
"description": "Successful operation",
"responseProperty": null,
"schema": {
"description": null,
"generatedName": "GenerateContentResponse",
"groupName": null,
"nameOverride": null,
"schema": "GenerateContentResponse",
"type": "reference",
},
"type": "json",
},
"sdkName": null,
"server": [],
"summary": null,
"tags": [
"generativelanguage",
],
},
],
"globalHeaders": [],
"groups": {},
"hasEndpointsMarkedInternal": false,
"idempotencyHeaders": [],
"nonRequestReferencedSchemas": [
"GenerateContentResponse",
],
"schemas": {},
"securitySchemes": {},
"servers": [
{
"description": null,
"name": null,
"url": "https://ai.com",
},
],
"tags": {
"orderedTagIds": null,
"tagsById": {},
},
"title": "Default Content Type",
"variables": {},
"webhooks": [],
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { testParseOpenAPI } from "./testParseOpenApi";

describe("open api parser", () => {
testParseOpenAPI("default-content", "openapi.yml");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
openapi: 3.0.3
info:
title: Default Content Type
version: v1
servers:
- url: https://ai.com
paths:
"/generateContent":
post:
tags:
- generativelanguage
operationId: GenerateContent
parameters:
- name: model
in: path
required: true
schema:
type: string
responses:
default:
description: Successful operation
content:
application/json:
schema:
"$ref": "#/components/schemas/GenerateContentResponse"
types:
schemas:
GenerateContentResponse:
properties:
bar:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,59 @@ export function convertResponse({
return { value: undefined, errors: {} };
}
const errors = markErrorSchemas({ responses, context });

let successStatusCodePresent = false;
let convertedResponse: FernOpenapiIr.ResponseWithExample | undefined = undefined;
for (const statusCode of responseStatusCode != null ? [responseStatusCode] : SUCCESSFUL_STATUS_CODES) {
const response = responses[statusCode];
if (response == null) {
continue;
}
successStatusCodePresent = true;
if (convertedResponse == null) {
convertedResponse = convertResolvedResponse({
operationContext,
response,
context,
responseBreadcrumbs,
streamFormat
});
}
}

const convertedResponse = convertResolvedResponse({
// If no success status codes have been visited, then try to fallback to the `default` status code
if (convertedResponse == null && !successStatusCodePresent && responses.default != null) {
convertedResponse = convertResolvedResponse({
operationContext,
response,
response: responses.default,
context,
responseBreadcrumbs,
streamFormat
});
if (convertedResponse != null) {
switch (convertedResponse.type) {
case "json":
return {
value: convertedResponse,
errors
};
case "streamingJson":
case "streamingSse":
return {
value: convertedResponse,
errors
};
case "file":
case "text":
case "streamingText":
return {
value: convertedResponse,
errors
};
default:
assertNever(convertedResponse);
}
}

if (convertedResponse != null) {
switch (convertedResponse.type) {
case "json":
return {
value: convertedResponse,
errors
};
case "streamingJson":
case "streamingSse":
return {
value: convertedResponse,
errors
};
case "file":
case "text":
case "streamingText":
return {
value: convertedResponse,
errors
};
default:
assertNever(convertedResponse);
}
}

Expand Down

0 comments on commit 521af41

Please sign in to comment.