Skip to content

Commit

Permalink
fix(cli): default booleans are parsed from strings as well (#5504)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Dec 30, 2024
1 parent e2a95f9 commit 59791ff
Show file tree
Hide file tree
Showing 10 changed files with 845 additions and 2 deletions.
5 changes: 5 additions & 0 deletions fern/pages/changelogs/cli/2024-12-30.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 0.46.17
**`(fix):`** Support parsing string values for boolean defaults in OpenAPI schemas.
* String values like "true" and "false" are now correctly parsed as boolean defaults.


Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SdkGroupName,
Source
} from "@fern-api/openapi-ir";
import { size } from "lodash-es";
import { OpenAPIV3 } from "openapi-types";
import { getExtension } from "../getExtension";
import { OpenAPIExtension } from "../openapi/v3/extensions/extensions";
Expand Down Expand Up @@ -40,7 +41,6 @@ import { SchemaParserContext } from "./SchemaParserContext";
import { getBreadcrumbsFromReference } from "./utils/getBreadcrumbsFromReference";
import { getGeneratedTypeName } from "./utils/getSchemaName";
import { isReferenceObject } from "./utils/isReferenceObject";
import { size } from "lodash-es";

export const SCHEMA_REFERENCE_PREFIX = "#/components/schemas/";
export const SCHEMA_INLINE_REFERENCE_PREFIX = "#/components/responses/";
Expand Down Expand Up @@ -351,7 +351,7 @@ export function convertSchemaObject(
generatedName,
title,
primitive: PrimitiveSchemaValueWithExample.boolean({
default: schema.default,
default: getBooleanFromDefault(schema.default),
example: getExampleAsBoolean({ schema, logger: context.logger, fallback })
}),
wrapAsNullable,
Expand Down Expand Up @@ -893,6 +893,25 @@ export function convertSchemaObject(
);
}

function getBooleanFromDefault(defaultValue: unknown): boolean | undefined {
if (defaultValue == null) {
return undefined;
}
if (typeof defaultValue === "boolean") {
return defaultValue;
}
if (typeof defaultValue === "string") {
const lowercased = defaultValue.toLowerCase();
if (lowercased === "true") {
return true;
}
if (lowercased === "false") {
return false;
}
}
return undefined;
}

export function getSchemaIdFromReference(ref: OpenAPIV3.ReferenceObject): string | undefined {
if (!ref.$ref.startsWith(SCHEMA_REFERENCE_PREFIX)) {
return undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"absoluteFilePath": "/DUMMY_PATH",
"importedDefinitions": {},
"namedDefinitionFiles": {
"__package__.yml": {
"absoluteFilepath": "/DUMMY_PATH",
"contents": {
"service": {
"auth": false,
"base-path": "",
"endpoints": {
"getTest": {
"auth": false,
"docs": undefined,
"examples": [
{
"response": {
"body": {
"boolField1": true,
"boolField2": true,
"numberField": 1.1,
"stringField": "stringField",
},
},
},
],
"method": "GET",
"pagination": undefined,
"path": "/test",
"request": {
"name": "GetTestRequest",
"query-parameters": {
"boolParam1": "optional<boolean>",
"boolParam2": "optional<boolean>",
"boolParam3": "optional<boolean>",
"boolParam4": "optional<boolean>",
"integerParam": "optional<integer>",
"numberParam": "optional<double>",
"stringParam": "optional<string>",
},
},
"response": {
"docs": "Successful response",
"type": "GetTestResponse",
},
"source": {
"openapi": "../openapi.yml",
},
},
},
"source": {
"openapi": "../openapi.yml",
},
},
"types": {
"GetTestResponse": {
"docs": undefined,
"inline": undefined,
"properties": {
"boolField1": {
"default": true,
"type": "optional<boolean>",
},
"boolField2": {
"default": true,
"type": "optional<boolean>",
},
"numberField": {
"default": 3.14,
"type": "optional<double>",
},
"stringField": {
"default": "defaultValue",
"type": "optional<string>",
},
},
"source": {
"openapi": "../openapi.yml",
},
},
},
},
"rawContents": "types:
GetTestResponse:
properties:
boolField1:
type: optional<boolean>
default: true
boolField2:
type: optional<boolean>
default: true
stringField:
type: optional<string>
default: defaultValue
numberField:
type: optional<double>
default: 3.14
source:
openapi: ../openapi.yml
service:
auth: false
base-path: ''
endpoints:
getTest:
path: /test
method: GET
auth: false
source:
openapi: ../openapi.yml
request:
name: GetTestRequest
query-parameters:
boolParam1: optional<boolean>
boolParam2: optional<boolean>
boolParam3: optional<boolean>
boolParam4: optional<boolean>
stringParam: optional<string>
numberParam: optional<double>
integerParam: optional<integer>
response:
docs: Successful response
type: GetTestResponse
examples:
- response:
body:
boolField1: true
boolField2: true
stringField: stringField
numberField: 1.1
source:
openapi: ../openapi.yml
",
},
},
"packageMarkers": {},
"rootApiFile": {
"contents": {
"display-name": "API with Default Values",
"error-discrimination": {
"strategy": "status-code",
},
"name": "api",
},
"defaultUrl": undefined,
"rawContents": "name: api
error-discrimination:
strategy: status-code
display-name: API with Default Values
",
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"type": "openapi",
"value": {
"openapi": "3.0.0",
"info": {
"title": "API with Default Values",
"version": "1.0.0"
},
"paths": {
"/test": {
"get": {
"parameters": [
{
"name": "boolParam1",
"in": "query",
"schema": {
"type": "boolean",
"default": true
}
},
{
"name": "boolParam2",
"in": "query",
"schema": {
"type": "boolean",
"default": "true"
}
},
{
"name": "boolParam3",
"in": "query",
"schema": {
"type": "boolean",
"default": false
}
},
{
"name": "boolParam4",
"in": "query",
"schema": {
"type": "boolean",
"default": "false"
}
},
{
"name": "stringParam",
"in": "query",
"schema": {
"type": "string",
"default": "defaultString"
}
},
{
"name": "numberParam",
"in": "query",
"schema": {
"type": "number",
"default": 42
}
},
{
"name": "integerParam",
"in": "query",
"schema": {
"type": "integer",
"default": 100
}
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"boolField1": {
"type": "boolean",
"default": true
},
"boolField2": {
"type": "boolean",
"default": "true"
},
"stringField": {
"type": "string",
"default": "defaultValue"
},
"numberField": {
"type": "number",
"default": 3.14
}
}
}
}
}
}
}
}
}
}
}
}
Loading

0 comments on commit 59791ff

Please sign in to comment.