-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli):
fern check
now runs simple rules on openapi specs & over…
…rides. (#5559) * feat(cli): `fern check` now runs simple rules on openapi specs & overrides. * chore: update changelog * Add complex test fixture and generate snapshots. * use `toMatchSnapshot` instead. --------- Co-authored-by: eyw520 <[email protected]>
- Loading branch information
Showing
37 changed files
with
779 additions
and
209 deletions.
There are no files selected for viewing
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 @@ | ||
## 0.46.23 | ||
**`(feat):`** The CLI now validates that method and group name overrides in OpenAPI settings are not duplicated. | ||
|
||
|
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
42 changes: 42 additions & 0 deletions
42
packages/cli/cli/src/commands/validate/validateOSSWorkspaceAndLogIssues.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,42 @@ | ||
import { OSSWorkspace } from "@fern-api/lazy-fern-workspace"; | ||
import { validateOSSWorkspace } from "@fern-api/oss-validator"; | ||
import { TaskContext } from "@fern-api/task-context"; | ||
|
||
import { logViolations } from "./logViolations"; | ||
|
||
export async function validateOSSWorkspaceWithoutExiting({ | ||
workspace, | ||
context, | ||
logWarnings, | ||
logSummary = true | ||
}: { | ||
workspace: OSSWorkspace; | ||
context: TaskContext; | ||
logWarnings: boolean; | ||
logSummary?: boolean; | ||
}): Promise<{ hasErrors: boolean }> { | ||
const violations = await validateOSSWorkspace(workspace, context); | ||
const { hasErrors } = logViolations({ violations, context, logWarnings, logSummary }); | ||
|
||
return { hasErrors }; | ||
} | ||
|
||
export async function validateOSSWorkspaceAndLogIssues({ | ||
workspace, | ||
context, | ||
logWarnings | ||
}: { | ||
workspace: OSSWorkspace; | ||
context: TaskContext; | ||
logWarnings: boolean; | ||
}): Promise<void> { | ||
const { hasErrors } = await validateOSSWorkspaceWithoutExiting({ | ||
workspace, | ||
context, | ||
logWarnings | ||
}); | ||
|
||
if (hasErrors) { | ||
context.failAndThrow(); | ||
} | ||
} |
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
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
7 changes: 7 additions & 0 deletions
7
packages/cli/workspace/lazy-fern-workspace/src/utils/convertOpenAPIV2ToV3.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,7 @@ | ||
import { OpenAPIV2, OpenAPIV3 } from "openapi-types"; | ||
import { convertObj } from "swagger2openapi"; | ||
|
||
export async function convertOpenAPIV2ToV3(openAPI: OpenAPIV2.Document): Promise<OpenAPIV3.Document> { | ||
const conversionResult = await convertObj(openAPI, {}); | ||
return conversionResult.openapi; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/cli/workspace/lazy-fern-workspace/src/utils/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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export { getAllOpenAPISpecs } from "./getAllOpenAPISpecs"; | ||
export { loadDependency } from "./loadDependency"; | ||
export { WorkspaceLoaderFailureType, type WorkspaceLoader } from "./Result"; | ||
export { convertOpenAPIV2ToV3 } from "./convertOpenAPIV2ToV3"; | ||
export { loadAsyncAPI } from "./loadAsyncAPI"; | ||
export { loadOpenAPI } from "./loadOpenAPI"; |
29 changes: 29 additions & 0 deletions
29
packages/cli/workspace/lazy-fern-workspace/src/utils/loadAsyncAPI.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 { readFile } from "fs/promises"; | ||
import yaml from "js-yaml"; | ||
|
||
import { AbsoluteFilePath } from "@fern-api/fs-utils"; | ||
import { AsyncAPIV2 } from "@fern-api/openapi-ir-parser"; | ||
import { TaskContext } from "@fern-api/task-context"; | ||
|
||
import { mergeWithOverrides } from "../loaders/mergeWithOverrides"; | ||
|
||
export async function loadAsyncAPI({ | ||
context, | ||
absoluteFilePath, | ||
absoluteFilePathToOverrides | ||
}: { | ||
context: TaskContext; | ||
absoluteFilePath: AbsoluteFilePath; | ||
absoluteFilePathToOverrides: AbsoluteFilePath | undefined; | ||
}): Promise<AsyncAPIV2.Document> { | ||
const contents = (await readFile(absoluteFilePath)).toString(); | ||
const parsed = (await yaml.load(contents)) as AsyncAPIV2.Document; | ||
if (absoluteFilePathToOverrides != null) { | ||
return await mergeWithOverrides<AsyncAPIV2.Document>({ | ||
absoluteFilePathToOverrides, | ||
context, | ||
data: parsed | ||
}); | ||
} | ||
return parsed; | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/cli/workspace/lazy-fern-workspace/src/utils/loadOpenAPI.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,52 @@ | ||
import { OpenAPI } from "openapi-types"; | ||
|
||
import { AbsoluteFilePath, RelativeFilePath, dirname, join } from "@fern-api/fs-utils"; | ||
import { FernOpenAPIExtension } from "@fern-api/openapi-ir-parser"; | ||
import { TaskContext } from "@fern-api/task-context"; | ||
|
||
import { mergeWithOverrides } from "../loaders/mergeWithOverrides"; | ||
import { parseOpenAPI } from "./parseOpenAPI"; | ||
|
||
export async function loadOpenAPI({ | ||
context, | ||
absolutePathToOpenAPI, | ||
absolutePathToOpenAPIOverrides | ||
}: { | ||
context: TaskContext; | ||
absolutePathToOpenAPI: AbsoluteFilePath; | ||
absolutePathToOpenAPIOverrides: AbsoluteFilePath | undefined; | ||
}): Promise<OpenAPI.Document> { | ||
const parsed = await parseOpenAPI({ | ||
absolutePathToOpenAPI | ||
}); | ||
|
||
let overridesFilepath = undefined; | ||
if (absolutePathToOpenAPIOverrides != null) { | ||
overridesFilepath = absolutePathToOpenAPIOverrides; | ||
} else if ( | ||
typeof parsed === "object" && | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(parsed as any)[FernOpenAPIExtension.OPENAPI_OVERIDES_FILEPATH] != null | ||
) { | ||
overridesFilepath = join( | ||
dirname(absolutePathToOpenAPI), | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
RelativeFilePath.of((parsed as any)[FernOpenAPIExtension.OPENAPI_OVERIDES_FILEPATH]) | ||
); | ||
} | ||
|
||
if (overridesFilepath != null) { | ||
const merged = await mergeWithOverrides<OpenAPI.Document>({ | ||
absoluteFilePathToOverrides: overridesFilepath, | ||
context, | ||
data: parsed | ||
}); | ||
// Run the merged document through the parser again to ensure that any override | ||
// references are resolved. | ||
return await parseOpenAPI({ | ||
absolutePathToOpenAPI, | ||
parsed: merged | ||
}); | ||
} | ||
return parsed; | ||
} |
Oops, something went wrong.