-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
608a912
commit ae7b17d
Showing
27 changed files
with
388 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { VersionMigrations } from "../../types/VersionMigrations"; | ||
import UnionSinglePropertyKeyMigration from "./union-single-property-key"; | ||
|
||
const versionMigrations: VersionMigrations = { | ||
version: "0.0.203", | ||
migrations: [UnionSinglePropertyKeyMigration], | ||
}; | ||
|
||
export default versionMigrations; |
34 changes: 34 additions & 0 deletions
34
...3/union-single-property-key/__test__/__snapshots__/union-single-property-key.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`union-single-property-key simple 1`] = ` | ||
"types: | ||
a: string | ||
b: boolean | ||
c: | ||
properties: | ||
a: integer | ||
d: | ||
union: | ||
a: | ||
type: string | ||
key: a | ||
b: | ||
type: boolean | ||
key: b | ||
d: | ||
type: d | ||
key: d | ||
e: | ||
union: | ||
a: | ||
type: string | ||
key: a | ||
b: | ||
type: boolean | ||
key: b | ||
d: | ||
type: d | ||
docs: hello | ||
key: d | ||
" | ||
`; |
1 change: 1 addition & 0 deletions
1
...ns/0.0.203/union-single-property-key/__test__/fixtures/simple/fern/api/definition/api.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
name: my-api |
19 changes: 19 additions & 0 deletions
19
....203/union-single-property-key/__test__/fixtures/simple/fern/api/definition/blog/blog.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
types: | ||
a: string | ||
b: boolean | ||
c: | ||
properties: | ||
a: integer | ||
d: | ||
union: | ||
a: string | ||
b: boolean | ||
d: d | ||
e: | ||
union: | ||
a: | ||
type: string | ||
b: boolean | ||
d: | ||
type: d | ||
docs: hello |
15 changes: 15 additions & 0 deletions
15
...ations/0.0.203/union-single-property-key/__test__/fixtures/simple/fern/api/generators.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
generators: | ||
- name: fernapi/fern-typescript | ||
version: 0.0.144 | ||
config: | ||
mode: model | ||
- name: fernapi/fern-java | ||
version: 0.0.84 | ||
generate: true | ||
config: | ||
packagePrefix: "com.fern" | ||
mode: MODEL | ||
- name: fernapi/fern-postman | ||
version: 0.0.18 | ||
- name: fernapi/fern-openapi | ||
version: 0.0.5 |
4 changes: 4 additions & 0 deletions
4
...grations/0.0.203/union-single-property-key/__test__/fixtures/simple/fern/fern.config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"organization": "fern", | ||
"version": "0.0.0" | ||
} |
29 changes: 29 additions & 0 deletions
29
...c/migrations/0.0.203/union-single-property-key/__test__/union-single-property-key.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/core-utils"; | ||
import { createMockTaskContext } from "@fern-api/task-context"; | ||
import { cp, readFile } from "fs/promises"; | ||
import tmp from "tmp-promise"; | ||
import { migration } from "../migration"; | ||
|
||
const FIXTURES_PATH = join(AbsoluteFilePath.of(__dirname), RelativeFilePath.of("fixtures")); | ||
|
||
describe("union-single-property-key", () => { | ||
it("simple", async () => { | ||
const fixturePath = join(FIXTURES_PATH, RelativeFilePath.of("simple")); | ||
const tmpDir = await tmp.dir(); | ||
|
||
await cp(fixturePath, tmpDir.path, { recursive: true }); | ||
process.chdir(tmpDir.path); | ||
|
||
await migration.run({ | ||
context: createMockTaskContext(), | ||
}); | ||
|
||
const newBlogYaml = ( | ||
await readFile( | ||
join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("fern/api/definition/blog/blog.yml")) | ||
) | ||
).toString(); | ||
|
||
expect(newBlogYaml).toMatchSnapshot(); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/cli/migrations/src/migrations/0.0.203/union-single-property-key/getAllYamlFiles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { AbsoluteFilePath } from "@fern-api/core-utils"; | ||
import { TaskContext, TASK_FAILURE } from "@fern-api/task-context"; | ||
import { findUp } from "find-up"; | ||
import glob from "glob-promise"; | ||
|
||
const FERN_DIRECTORY = "fern"; | ||
|
||
export async function getAllYamlFiles(context: TaskContext): Promise<AbsoluteFilePath[] | TASK_FAILURE> { | ||
const fernDirectory = await getFernDirectory(); | ||
if (fernDirectory == null) { | ||
return context.fail(`Directory "${FERN_DIRECTORY}" not found.`); | ||
} | ||
const filepaths = await glob("*/definition/**/*.yml", { | ||
cwd: fernDirectory, | ||
absolute: true, | ||
}); | ||
return filepaths.map(AbsoluteFilePath.of); | ||
} | ||
|
||
async function getFernDirectory(): Promise<AbsoluteFilePath | undefined> { | ||
const fernDirectoryStr = await findUp(FERN_DIRECTORY, { type: "directory" }); | ||
if (fernDirectoryStr == null) { | ||
return undefined; | ||
} | ||
return AbsoluteFilePath.of(fernDirectoryStr); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/cli/migrations/src/migrations/0.0.203/union-single-property-key/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { migration as default } from "./migration"; |
63 changes: 63 additions & 0 deletions
63
packages/cli/migrations/src/migrations/0.0.203/union-single-property-key/migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { AbsoluteFilePath } from "@fern-api/core-utils"; | ||
import { TaskContext, TASK_FAILURE } from "@fern-api/task-context"; | ||
import { readFile, writeFile } from "fs/promises"; | ||
import YAML from "yaml"; | ||
import { Migration } from "../../../types/Migration"; | ||
import { getAllYamlFiles } from "./getAllYamlFiles"; | ||
|
||
export const migration: Migration = { | ||
name: "union-single-property-migration", | ||
summary: "migrates union types to set the `key` property on non-object subtypes to the discriminant value.", | ||
run: async ({ context }) => { | ||
const yamlFiles = await getAllYamlFiles(context); | ||
if (yamlFiles === TASK_FAILURE) { | ||
return; | ||
} | ||
for (const filepath of yamlFiles) { | ||
try { | ||
await migrateFile(filepath, context); | ||
} catch (error) { | ||
context.fail(`Failed to add 'key' property to union in ${filepath}`, error); | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
async function migrateFile(filepath: AbsoluteFilePath, context: TaskContext): Promise<void> { | ||
const contents = await readFile(filepath); | ||
const parsedDocument = YAML.parseDocument(contents.toString()); | ||
const types = parsedDocument.get("types"); | ||
if (types == null) { | ||
return; | ||
} | ||
if (!YAML.isMap(types)) { | ||
context.fail(`"types" is not a map in ${filepath}`); | ||
return; | ||
} | ||
|
||
for (const typeDeclaration of types.items) { | ||
if (YAML.isMap(typeDeclaration.value)) { | ||
const union = typeDeclaration.value.get("union"); | ||
console.log(typeDeclaration, union); | ||
if (union == null) { | ||
continue; | ||
} | ||
if (!YAML.isMap(union)) { | ||
context.fail(`"union" is not a map in ${filepath}`); | ||
continue; | ||
} | ||
for (const singleUnionType of union.items) { | ||
if (YAML.isScalar(singleUnionType.value)) { | ||
singleUnionType.value = { | ||
type: singleUnionType.value, | ||
key: singleUnionType.key, | ||
}; | ||
} else if (YAML.isMap(singleUnionType.value)) { | ||
singleUnionType.value.add(new YAML.Pair("key", singleUnionType.key)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
await writeFile(filepath, parsedDocument.toString()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { VersionMigrations } from "../types/VersionMigrations"; | ||
import migrations_0_0_188 from "./0.0.188"; | ||
import migrations_0_0_191 from "./0.0.191"; | ||
import migrations_0_0_203 from "./0.0.203"; | ||
|
||
export const ALL_MIGRATIONS: VersionMigrations[] = [migrations_0_0_188, migrations_0_0_191]; | ||
export const ALL_MIGRATIONS: VersionMigrations[] = [migrations_0_0_188, migrations_0_0_191, migrations_0_0_203]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...dator/src/rules/no-object-single-property-key/__test__/fixtures/simple/definition/api.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
name: simple-api |
22 changes: 22 additions & 0 deletions
22
...tor/src/rules/no-object-single-property-key/__test__/fixtures/simple/definition/posts.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
types: | ||
MyPrimitiveAlias: string | ||
MyObjectAlias: MyObject | ||
MyObject: | ||
properties: | ||
a: string | ||
b: number | ||
MyUnion: | ||
union: | ||
a: MyPrimitiveAlias | ||
b: | ||
type: string | ||
key: hello | ||
c: MyObjectAlias | ||
d: | ||
type: MyObjectAlias | ||
key: hello | ||
e: | ||
key: yoyo | ||
f: | ||
key: yoyo | ||
type: void |
1 change: 1 addition & 0 deletions
1
...validator/src/rules/no-object-single-property-key/__test__/fixtures/simple/generators.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Oops, something went wrong.