Skip to content

Commit

Permalink
fix(cli): parse examples from primitive OpenAPI schemas (#5061)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Nov 1, 2024
1 parent 6da5bf4 commit 00dd9f4
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ export function convertParameters({
const parameterBreadcrumbs = [...requestBreadcrumbs, resolvedParameter.name];
const generatedName = getGeneratedTypeName(parameterBreadcrumbs);

if (getExamplesString({ schema: resolvedParameter, logger: context.logger })?.includes(" ")) {
context.logger.warn(
"Parameter example contains a space, which is ambiguous. Consider using enums for multiple examples, or use an encoding if a space is part of the parameter."
);
}

let schema =
resolvedParameter.schema != null
? convertSchema(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { OpenAPIV3 } from "openapi-types";
import { getExtension } from "../../../getExtension";

export function getExamples(schema: OpenAPIV3.SchemaObject | OpenAPIV3.ParameterObject): unknown[] {
const examples = getExtension<unknown[]>(schema, "examples");
if (examples != null && Array.isArray(examples)) {
return examples;
}
return [];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Logger } from "@fern-api/logger";
import { OpenAPIV3 } from "openapi-types";
import { getExtension } from "../../getExtension";
import { getExamples } from "../../openapi/v3/extensions/getExamples";

export function getExampleAsNumber({
schema,
Expand All @@ -13,12 +15,15 @@ export function getExampleAsNumber({
if (schema.example != null && typeof schema.example === "number") {
return schema.example;
}
const examples = getExamples(schema);
for (const example of examples ?? []) {
if (typeof example === "number") {
return example;
}
}
if (fallback && typeof fallback === "number") {
return fallback;
} else if (fallback) {
logger.warn(`Expected fallback to be a number, but got ${typeof fallback}`);
}

return undefined;
}

Expand All @@ -34,10 +39,14 @@ export function getExampleAsBoolean({
if (schema.example != null && typeof schema.example === "boolean") {
return schema.example;
}
const examples = getExamples(schema);
for (const example of examples ?? []) {
if (typeof example === "boolean") {
return example;
}
}
if (fallback && typeof fallback === "boolean") {
return fallback;
} else if (fallback) {
logger.warn(`Expected fallback to be a boolean, but got ${typeof fallback}`);
}
return undefined;
}
Expand All @@ -54,10 +63,14 @@ export function getExamplesString({
if (schema.example != null && typeof schema.example === "string") {
return schema.example;
}
const examples = getExamples(schema);
for (const example of examples ?? []) {
if (typeof example === "string") {
return example;
}
}
if (fallback && typeof fallback === "string") {
return fallback;
} else if (fallback) {
logger.warn(`Expected fallback to be a string, but got ${typeof fallback}`);
}
return undefined;
}
Expand All @@ -74,10 +87,14 @@ export function getExampleAsArray({
if (schema.example != null && Array.isArray(schema.example)) {
return schema.example;
}
const examples = getExamples(schema);
for (const example of examples ?? []) {
if (Array.isArray(example)) {
return example;
}
}
if (fallback && Array.isArray(fallback)) {
return fallback;
} else if (fallback) {
logger.warn(`Expected fallback to be a array, but got ${typeof fallback}`);
}
return undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ types:
],
"examples": [
{
"query-parameters": {
"cursor": "65a17e3f43bec88e2792d0eb",
"limit": 8,
},
"response": {
"body": {
"key": "value",
Expand Down Expand Up @@ -677,7 +681,10 @@ service:
- root.GetEndUsersRequestTooManyRequestsError
- root.GetEndUsersRequestInternalServerError
examples:
- response:
- query-parameters:
cursor: 65a17e3f43bec88e2792d0eb
limit: 8
response:
body:
key: value
source:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13859,6 +13859,9 @@ Cutoff times are very slowly changing data. This is a _conditional operation_ wh
"path-parameters": {
"institutionId": "TIBURON",
},
"query-parameters": {
"timeZoneId": "America/New_York",
},
"response": {
"body": {
"items": [
Expand Down Expand Up @@ -14182,7 +14185,7 @@ This information provides hints to clients, allowing bank customers to select tr
"examples": [
{
"query-parameters": {
"countryCode": "countryCode",
"countryCode": "US",
"locator": "locator",
"locatorType": "abaRoutingNumber",
},
Expand All @@ -14197,7 +14200,7 @@ This information provides hints to clients, allowing bank customers to select tr
"locality": "Andalasia",
"postalCode": "28407",
"regionCode": "NC",
"regionName": "regionName",
"regionName": "NC",
},
"locator": "503000196",
"locatorType": "abaRoutingNumber",
Expand All @@ -14212,6 +14215,7 @@ This information provides hints to clients, allowing bank customers to select tr
"locality": "Andalasia",
"postalCode": "28407",
"regionCode": "NC",
"regionName": "NC",
},
"locator": "503000196",
"locatorType": "abaRoutingNumber",
Expand Down Expand Up @@ -14314,7 +14318,7 @@ service:
- query-parameters:
locator: locator
locatorType: abaRoutingNumber
countryCode: countryCode
countryCode: US
response:
body:
found: true
Expand All @@ -14325,7 +14329,7 @@ service:
address2: Building 14, Suite 1500
locality: Andalasia
countryCode: US
regionName: regionName
regionName: NC
regionCode: NC
postalCode: '28407'
locator: '503000196'
Expand All @@ -14337,6 +14341,7 @@ service:
address2: Building 14, Suite 1500
locality: Andalasia
countryCode: US
regionName: NC
regionCode: NC
postalCode: '28407'
locator: '503000196'
Expand Down Expand Up @@ -14469,6 +14474,8 @@ service:
examples:
- path-parameters:
institutionId: TIBURON
query-parameters:
timeZoneId: America/New_York
response:
body:
timeZoneId: America/New_York
Expand Down Expand Up @@ -15271,6 +15278,13 @@ The default response lists only recent transactions. Normally, this is transacti
"path-parameters": {
"accountId": "accountId",
},
"query-parameters": {
"amount": "[1000.00,1200.00)",
"checkNumber": "[1000,1200)",
"createdOn": "2022-05-19",
"occurredOn": "2023-05-19",
"postedOn": "2022-05-19",
},
"response": {
"body": {
"count": 2381,
Expand Down Expand Up @@ -15574,6 +15588,12 @@ service:
examples:
- path-parameters:
accountId: accountId
query-parameters:
occurredOn: '2023-05-19'
createdOn: '2022-05-19'
postedOn: '2022-05-19'
amount: '[1000.00,1200.00)'
checkNumber: '[1000,1200)'
response:
body:
limit: 10
Expand Down Expand Up @@ -15842,6 +15862,11 @@ Note: This operation requires an identity challenge if the financial institution
],
"examples": [
{
"query-parameters": {
"creditsOn": "2022-05-19",
"debitsOn": "2022-05-19",
"scheduledOn": "2022-05-19",
},
"response": {
"body": {
"count": 1,
Expand Down Expand Up @@ -16137,7 +16162,11 @@ service:
- root.TransfersListTransfersRequestForbiddenError
- root.TransfersListTransfersRequestUnprocessableEntityError
examples:
- response:
- query-parameters:
scheduledOn: '2022-05-19'
debitsOn: '2022-05-19'
creditsOn: '2022-05-19'
response:
body:
limit: 10
nextPage_url: >-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@
"generatedName": "GetEndUsersRequestCursor",
"value": {
"schema": {
"example": "65a17e3f43bec88e2792d0eb",
"type": "string"
},
"generatedName": "GetEndUsersRequestCursor",
Expand All @@ -926,6 +927,7 @@
"schema": {
"minimum": 1,
"maximum": 100,
"example": 8,
"type": "int"
},
"generatedName": "GetEndUsersRequestLimit",
Expand Down Expand Up @@ -1247,7 +1249,28 @@
"examples": [
{
"pathParameters": [],
"queryParameters": [],
"queryParameters": [
{
"name": "cursor",
"value": {
"value": {
"value": "65a17e3f43bec88e2792d0eb",
"type": "string"
},
"type": "primitive"
}
},
{
"name": "limit",
"value": {
"value": {
"value": 8,
"type": "int"
},
"type": "primitive"
}
}
],
"headers": [],
"response": {
"value": {
Expand Down
Loading

0 comments on commit 00dd9f4

Please sign in to comment.