Skip to content

Commit

Permalink
fix(cli): validated example generation (#5275)
Browse files Browse the repository at this point in the history
* Validated example generation.

* Update ete snapshots.

* Update snapshots.

* Update Snapshots

* chore(cli): Add dynamic property to IR (#5276)

* fix(docs): update public sdk guide redirect (#5274)

* (chore): remove `hidden` from publish sdk guide

* Update snapshots.

* Better string example generation & ete test updates.

* Remove non-null assertion; snapshot updates.

* Add additional example strings; default to minLength.

* Update snapshots

* Update snapshots.

* Update snapshots

* Snapshot

* update csharp model snapshots.

---------

Co-authored-by: Alex McKinney <alexmckinney01@gmail.com>
Co-authored-by: Catherine Deskur <46695336+chdeskur@users.noreply.github.com>
Co-authored-by: Deep Singhvi <deep@buildwithfern.com>
  • Loading branch information
4 people authored Nov 26, 2024
1 parent 825b028 commit 7dd123f
Show file tree
Hide file tree
Showing 268 changed files with 12,907 additions and 2,463 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("fern generate --local", () => {
await runFernCli(["generate", "--local", "--keepDocker"], {
cwd: pathOfDirectory
});
}, 180_000);
}, 360_000);
});

async function expectPathDoesNotExist(absoluteFilePath: AbsoluteFilePath): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "f894d7236e53b5a4907afb3242986edc936b7231",
"id": "334da6161121c9cadbf98de7666869a59bd9b834",
"url": "/create",
"name": null,
"endpointHeaders": [],
Expand Down Expand Up @@ -1524,10 +1524,10 @@
"type": "primitive",
"primitive": {
"type": "double",
"double": 1.1
"double": 2.2
}
},
"jsonExample": 1.1
"jsonExample": 2.2
}
},
{
Expand Down Expand Up @@ -1559,10 +1559,10 @@
"type": "primitive",
"primitive": {
"type": "integer",
"integer": 1
"integer": 100
}
},
"jsonExample": 1
"jsonExample": 100
}
},
{
Expand Down Expand Up @@ -1595,11 +1595,11 @@
"primitive": {
"type": "string",
"string": {
"original": "name"
"original": "foo"
}
}
},
"jsonExample": "name"
"jsonExample": "foo"
}
},
{
Expand Down Expand Up @@ -1687,9 +1687,9 @@
}
],
"jsonExample": {
"decimal": 1.1,
"even": 1,
"name": "name",
"decimal": 2.2,
"even": 100,
"name": "foo",
"shape": "SQUARE"
}
},
Expand Down Expand Up @@ -1758,10 +1758,10 @@
"type": "primitive",
"primitive": {
"type": "double",
"double": 1.1
"double": 2.2
}
},
"jsonExample": 1.1
"jsonExample": 2.2
}
},
{
Expand Down Expand Up @@ -1819,10 +1819,10 @@
"type": "primitive",
"primitive": {
"type": "integer",
"integer": 1
"integer": 100
}
},
"jsonExample": 1
"jsonExample": 100
}
},
{
Expand Down Expand Up @@ -1881,11 +1881,11 @@
"primitive": {
"type": "string",
"string": {
"original": "name"
"original": "foo"
}
}
},
"jsonExample": "name"
"jsonExample": "foo"
}
},
{
Expand Down Expand Up @@ -2028,9 +2028,9 @@
}
},
"jsonExample": {
"decimal": 1.1,
"even": 1,
"name": "name",
"decimal": 2.2,
"even": 100,
"name": "foo",
"shape": "SQUARE"
}
}
Expand Down Expand Up @@ -2327,7 +2327,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "5ec5bb625677405238a5bb25cc6d0d168e9f5038",
"id": "e7df79454aff4dcd365862a38fd297679ac3975d",
"url": "",
"name": null,
"endpointHeaders": [],
Expand Down Expand Up @@ -2364,10 +2364,10 @@
"type": "primitive",
"primitive": {
"type": "double",
"double": 1.1
"double": 2.2
}
},
"jsonExample": 1.1
"jsonExample": 2.2
}
},
{
Expand Down Expand Up @@ -2401,10 +2401,10 @@
"type": "primitive",
"primitive": {
"type": "integer",
"integer": 1
"integer": 100
}
},
"jsonExample": 1
"jsonExample": 100
}
},
{
Expand Down Expand Up @@ -2439,11 +2439,11 @@
"primitive": {
"type": "string",
"string": {
"original": "name"
"original": "foo"
}
}
},
"jsonExample": "name"
"jsonExample": "foo"
}
}
],
Expand Down Expand Up @@ -2516,10 +2516,10 @@
"type": "primitive",
"primitive": {
"type": "double",
"double": 1.1
"double": 2.2
}
},
"jsonExample": 1.1
"jsonExample": 2.2
}
},
{
Expand Down Expand Up @@ -2577,10 +2577,10 @@
"type": "primitive",
"primitive": {
"type": "integer",
"integer": 1
"integer": 100
}
},
"jsonExample": 1
"jsonExample": 100
}
},
{
Expand Down Expand Up @@ -2639,11 +2639,11 @@
"primitive": {
"type": "string",
"string": {
"original": "name"
"original": "foo"
}
}
},
"jsonExample": "name"
"jsonExample": "foo"
}
},
{
Expand Down Expand Up @@ -2786,9 +2786,9 @@
}
},
"jsonExample": {
"decimal": 1.1,
"even": 1,
"name": "name",
"decimal": 2.2,
"even": 100,
"name": "foo",
"shape": "SQUARE"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { ExamplePrimitive, PrimitiveType } from "@fern-api/ir-sdk";
import {
ExamplePrimitive,
PrimitiveType,
PrimitiveTypeV2,
StringValidationRules,
DoubleValidationRules,
IntegerValidationRules
} from "@fern-api/ir-sdk";
import { assertNever, Examples } from "@fern-api/core-utils";
import { ExampleGenerationResult, ExampleGenerationSuccess } from "./ExampleGenerationResult";
import { ExampleGenerationSuccess } from "./ExampleGenerationResult";

export declare namespace generatePrimitiveExample {
interface Args {
Expand All @@ -16,8 +23,10 @@ export function generatePrimitiveExample({
}: generatePrimitiveExample.Args): ExampleGenerationSuccess<ExamplePrimitive> {
switch (primitiveType.v1) {
case "STRING": {
const jsonExample = fieldName ?? Examples.STRING;
return { type: "success", example: ExamplePrimitive.string({ original: jsonExample }), jsonExample };
return generatePrimitiveStringExample({
fieldName,
validation: maybeStringValidation(primitiveType.v2)
});
}
case "BASE_64": {
return { type: "success", example: ExamplePrimitive.base64(Examples.BASE64), jsonExample: Examples.BASE64 };
Expand All @@ -43,13 +52,19 @@ export function generatePrimitiveExample({
};
}
case "DOUBLE": {
return { type: "success", example: ExamplePrimitive.double(Examples.DOUBLE), jsonExample: Examples.DOUBLE };
return generatePrimitiveDoubleExample({
fieldName,
validation: maybeDoubleValidation(primitiveType.v2)
});
}
case "FLOAT": {
return { type: "success", example: ExamplePrimitive.float(Examples.FLOAT), jsonExample: Examples.FLOAT };
}
case "INTEGER": {
return { type: "success", example: ExamplePrimitive.integer(Examples.INT), jsonExample: Examples.INT };
return generatePrimitiveIntegerExample({
fieldName,
validation: maybeIntegerValidation(primitiveType.v2)
});
}
case "UINT_64": {
return { type: "success", example: ExamplePrimitive.uint64(Examples.INT64), jsonExample: Examples.INT64 };
Expand All @@ -74,3 +89,110 @@ export function generatePrimitiveExample({
assertNever(primitiveType.v1);
}
}

function maybeStringValidation(v2: PrimitiveTypeV2 | undefined): StringValidationRules | undefined {
if (v2?.type === "string") {
const stringType = v2 as PrimitiveTypeV2.String;
return stringType.validation;
}
return undefined;
}

function maybeDoubleValidation(v2: PrimitiveTypeV2 | undefined): DoubleValidationRules | undefined {
if (v2?.type === "double") {
const doubleType = v2 as PrimitiveTypeV2.Double;
return doubleType.validation;
}
return undefined;
}

function maybeIntegerValidation(v2: PrimitiveTypeV2 | undefined): IntegerValidationRules | undefined {
if (v2?.type === "integer") {
const integerType = v2 as PrimitiveTypeV2.Integer;
return integerType.validation;
}
return undefined;
}

function getStringExampleOfLength(length: number): string {
if (length <= Examples.SAMPLE_STRINGS.length) {
const sampleString = Examples.SAMPLE_STRINGS[length - 1];
if (sampleString) {
return sampleString;
} else {
throw new Error(`Unexpected undefined value in SAMPLE_STRINGS at index ${length - 1}`);
}
}
return (
Examples.SAMPLE_STRINGS[Examples.SAMPLE_STRINGS.length - 1] +
".".repeat(length - Examples.SAMPLE_STRINGS.length)
);
}

function generatePrimitiveStringExample({
fieldName,
validation
}: {
fieldName: string | undefined;
validation: StringValidationRules | undefined;
}): ExampleGenerationSuccess<ExamplePrimitive> {
if (validation) {
const minLength = validation.minLength;
const maxLength = validation.maxLength;
if (minLength) {
const minLengthExample = getStringExampleOfLength(minLength);
return {
type: "success",
example: ExamplePrimitive.string({ original: minLengthExample }),
jsonExample: minLengthExample
};
} else if (maxLength) {
const maxLengthExample = getStringExampleOfLength(maxLength);
return {
type: "success",
example: ExamplePrimitive.string({ original: maxLengthExample }),
jsonExample: maxLengthExample
};
}
}
const jsonExample = fieldName ?? Examples.STRING;
return { type: "success", example: ExamplePrimitive.string({ original: jsonExample }), jsonExample };
}

function generatePrimitiveDoubleExample({
fieldName,
validation
}: {
fieldName: string | undefined;
validation: DoubleValidationRules | undefined;
}): ExampleGenerationSuccess<ExamplePrimitive> {
if (validation) {
const maximum = validation.max;
const minimum = validation.min;
if (maximum) {
return { type: "success", example: ExamplePrimitive.double(maximum), jsonExample: maximum };
} else if (minimum) {
return { type: "success", example: ExamplePrimitive.double(minimum), jsonExample: minimum };
}
}
return { type: "success", example: ExamplePrimitive.double(Examples.DOUBLE), jsonExample: Examples.DOUBLE };
}

function generatePrimitiveIntegerExample({
fieldName,
validation
}: {
fieldName: string | undefined;
validation: IntegerValidationRules | undefined;
}): ExampleGenerationSuccess<ExamplePrimitive> {
if (validation) {
const maximum = validation.max;
const minimum = validation.min;
if (maximum) {
return { type: "success", example: ExamplePrimitive.integer(maximum), jsonExample: maximum };
} else if (minimum) {
return { type: "success", example: ExamplePrimitive.integer(minimum), jsonExample: minimum };
}
}
return { type: "success", example: ExamplePrimitive.integer(Examples.INT), jsonExample: Examples.INT };
}
Loading

0 comments on commit 7dd123f

Please sign in to comment.