Skip to content
This repository was archived by the owner on Feb 10, 2023. It is now read-only.

Commit 9cf0492

Browse files
committed
better allOf resolution with adhoc
1 parent bdc070e commit 9cf0492

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/internal/OpenApiTools/Guard.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ export const isAnyOfSchema = (schema: OpenApi.Schema): schema is Types.AnyOfSche
6666
return !!schema.anyOf && typeof schema.anyOf !== "boolean" && Array.isArray(schema.anyOf);
6767
};
6868

69+
export const isAdhocAllOfSchema = (
70+
schema: OpenApi.JSONSchema,
71+
): schema is Types.AllOfSchema & {
72+
allOf: [OpenApi.Reference, OpenApi.JSONSchema];
73+
} => {
74+
if (!isAllOfSchema(schema)) return false;
75+
const [maybeReference, maybeAdhoc, ...rest] = schema.allOf;
76+
return isReference(maybeReference) && !("type" in maybeAdhoc) && !isReference(maybeAdhoc) && rest.length === 0;
77+
};
78+
6979
export const isComponentName = (name: string): name is Def.ComponentName => {
7080
return ["schemas", "headers", "responses", "parameters", "requestBodies", "securitySchemes", "pathItems"].includes(name);
7181
};

src/internal/OpenApiTools/toTypeNode.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as Reference from "./components/Reference";
99
import * as ConverterContext from "./ConverterContext";
1010
import * as Guard from "./Guard";
1111
import * as InferredType from "./InferredType";
12-
import { ObjectSchemaWithAdditionalProperties } from "./types";
12+
import { AllOfSchema, ObjectSchemaWithAdditionalProperties } from "./types";
1313

1414
export interface ResolveReferencePath {
1515
name: string;
@@ -44,6 +44,19 @@ export interface Option {
4444
useClientSchema?: boolean;
4545
}
4646

47+
export const mergeAdhoc = (
48+
currentPoint: string,
49+
schema: AllOfSchema & {
50+
allOf: [OpenApi.Reference, OpenApi.JSONSchema];
51+
},
52+
context: Context,
53+
): OpenApi.JSONSchema => {
54+
const [reference, adhoc] = schema.allOf;
55+
const { pathArray } = context.resolveReferencePath(currentPoint, reference.$ref);
56+
const resolvedSchema = DotProp.get<OpenApi.JSONSchema>(context.rootSchema, pathArray.join("."));
57+
return { ...resolvedSchema, ...adhoc };
58+
};
59+
4760
export const generateMultiTypeNode = (
4861
entryPoint: string,
4962
currentPoint: string,
@@ -137,6 +150,10 @@ export const convert: Convert = (
137150
return convert(entryPoint, reference.referencePoint, factory, reference.data, context, converterContext, { ...option, parent: schema });
138151
}
139152

153+
if (Guard.isAdhocAllOfSchema(schema)) {
154+
return convert(entryPoint, currentPoint, factory, mergeAdhoc(currentPoint, schema, context), context, converterContext, option);
155+
}
156+
140157
if (Guard.isOneOfSchema(schema)) {
141158
return nullable(
142159
factory,

0 commit comments

Comments
 (0)