Skip to content

Commit

Permalink
api/schema: Avoid circular references between ai-api schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Sep 13, 2024
1 parent 41e7b11 commit ace389e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 35 deletions.
70 changes: 39 additions & 31 deletions packages/api/src/schema/ai-api-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,37 @@ paths:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"401":
description: Unauthorized
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"422":
description: Validation Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPValidationError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"500":
description: Internal Server Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
default:
description: Error
content:
application/json:
schema:
$ref: "#/components/schemas/error"
$ref: "#/components/schemas/studio-api-error"
security:
- HTTPBearer: []
x-speakeasy-name-override: textToImage
Expand Down Expand Up @@ -90,37 +90,37 @@ paths:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"401":
description: Unauthorized
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"422":
description: Validation Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPValidationError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"500":
description: Internal Server Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
default:
description: Error
content:
application/json:
schema:
$ref: "#/components/schemas/error"
$ref: "#/components/schemas/studio-api-error"
security:
- HTTPBearer: []
x-speakeasy-name-override: imageToImage
Expand Down Expand Up @@ -152,37 +152,37 @@ paths:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"401":
description: Unauthorized
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"422":
description: Validation Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPValidationError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"500":
description: Internal Server Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
default:
description: Error
content:
application/json:
schema:
$ref: "#/components/schemas/error"
$ref: "#/components/schemas/studio-api-error"
security:
- HTTPBearer: []
x-speakeasy-name-override: imageToVideo
Expand Down Expand Up @@ -214,37 +214,37 @@ paths:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"401":
description: Unauthorized
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"422":
description: Validation Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPValidationError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"500":
description: Internal Server Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
default:
description: Error
content:
application/json:
schema:
$ref: "#/components/schemas/error"
$ref: "#/components/schemas/studio-api-error"
security:
- HTTPBearer: []
x-speakeasy-name-override: upscale
Expand Down Expand Up @@ -276,45 +276,45 @@ paths:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"401":
description: Unauthorized
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"413":
description: Request Entity Too Large
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"422":
description: Validation Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPValidationError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"500":
description: Internal Server Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
default:
description: Error
content:
application/json:
schema:
$ref: "#/components/schemas/error"
$ref: "#/components/schemas/studio-api-error"
security:
- HTTPBearer: []
x-speakeasy-name-override: audioToText
Expand Down Expand Up @@ -346,37 +346,37 @@ paths:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"401":
description: Unauthorized
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"422":
description: Validation Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPValidationError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
"500":
description: Internal Server Error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/HTTPError"
- $ref: "#/components/schemas/error"
- $ref: "#/components/schemas/studio-api-error"
default:
description: Error
content:
application/json:
schema:
$ref: "#/components/schemas/error"
$ref: "#/components/schemas/studio-api-error"
security:
- HTTPBearer: []
x-speakeasy-name-override: segmentAnything2
Expand Down Expand Up @@ -860,6 +860,14 @@ components:
- text
title: chunk
description: A chunk of text with a timestamp.
studio-api-error:
type: object
properties:
errors:
type: array
minItems: 1
items:
type: string
securitySchemes:
HTTPBearer:
type: http
Expand Down
29 changes: 25 additions & 4 deletions packages/api/src/schema/pull-ai-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ const schemaDir = path.resolve(__dirname, ".");
const aiSchemaUrl =
"https://raw.githubusercontent.com/livepeer/ai-worker/refs/heads/main/runner/gateway.openapi.yaml";

const studioApiErrorSchema = {
type: "object",
properties: {
errors: {
type: "array",
minItems: 1,
items: {
type: "string",
},
},
},
};

const write = (dir, data) => {
if (fs.existsSync(dir)) {
const existing = fs.readFileSync(dir, "utf8");
Expand Down Expand Up @@ -44,14 +57,19 @@ const downloadAiSchema = async () => {
delete schema.info;
delete schema.servers;

// add studio-api-error schema
schema.components.schemas["studio-api-error"] = studioApiErrorSchema;

// patches to the paths section
schema.paths = mapObject(schema.paths, (path, value) => {
// prefix paths with /api/beta/generate
path = `/api/beta/generate${path}`;
// remove security field
delete value.security;
// add $ref: "#/components/schemas/error" as oneOf to all of the error responses
const apiError = () => ({ $ref: "#/components/schemas/error" });
// add Studio API error as oneOf to all of the error responses
const studioErrorRef = () => ({
$ref: "#/components/schemas/studio-api-error",
});
value.post.responses = mapObject(
value.post.responses,
(statusCode, response) => {
Expand All @@ -62,7 +80,10 @@ const downloadAiSchema = async () => {
return [statusCode, response];
}
response.content["application/json"].schema = {
oneOf: [response.content["application/json"].schema, apiError()],
oneOf: [
response.content["application/json"].schema,
studioErrorRef(),
],
};
return [statusCode, response];
},
Expand All @@ -71,7 +92,7 @@ const downloadAiSchema = async () => {
if (!value.post.responses["default"]) {
value.post.responses["default"] = {
description: "Error",
content: { "application/json": { schema: apiError() } },
content: { "application/json": { schema: studioErrorRef() } },
};
}
return [path, value];
Expand Down

0 comments on commit ace389e

Please sign in to comment.