Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include schemas from all documents in dependency graph #227

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function buildDependencyGraph(
* Create an object mapping name -> Set(direct dependencies)
*/
const dependencyGraph = Object.fromEntries(
// TODO: this may miss extracted in-line schemas
Object.entries(input.allSchemas()).map(([name, schema]) => {
return [
getNameForRef({$ref: name}),
Expand Down
13 changes: 12 additions & 1 deletion packages/openapi-code-generator/src/core/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {

export type OperationGroup = {name: string; operations: IROperation[]}
export type OperationGroupStrategy = "none" | "first-tag" | "first-slug"

export class Input {
constructor(
readonly loader: OpenapiLoader,
Expand All @@ -47,7 +48,17 @@ export class Input {
}

allSchemas(): Record<string, IRModel> {
const schemas = this.loader.entryPoint.components?.schemas ?? {}
const allDocuments = this.loader.allDocuments()

const schemas = allDocuments.reduce(
(acc, it) => {
return Object.assign(acc, it.components?.schemas ?? {})
},
{} as {
[schemaName: string]: Schema | Reference
},
)

return Object.fromEntries(
Object.entries(schemas).map(([name, maybeSchema]) => {
return [name, this.schema(normalizeSchemaObject(maybeSchema))]
Expand Down
10 changes: 7 additions & 3 deletions packages/openapi-code-generator/src/core/openapi-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import type {OpenapiValidator} from "./openapi-validator"

export class OpenapiLoader {
private readonly virtualLibrary = new Map<string, VirtualDefinition>()
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
private readonly library = new Map<string, any>()
private readonly library = new Map<string, OpenapiDocument>()

private constructor(
private readonly entryPointKey: string,
Expand Down Expand Up @@ -50,6 +49,10 @@ export class OpenapiLoader {
return this.library.get(this.entryPointKey)!
}

allDocuments(): OpenapiDocument[] {
return Array.from(this.library.values())
}

paths(maybeRef: Reference | Path): Path {
return isRef(maybeRef) ? this.$ref(maybeRef) : maybeRef
}
Expand Down Expand Up @@ -81,7 +84,8 @@ export class OpenapiLoader {
private $ref<T>({$ref}: Reference): T {
const [key, objPath] = $ref.split("#")

const obj = key && this.library.get(key)
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const obj: any = key && this.library.get(key)

if (!obj) {
throw new Error(`could not load $ref, key not loaded. $ref: '${$ref}'`)
Expand Down