Skip to content

Commit

Permalink
feat: oneOf support for typescript openapi 3
Browse files Browse the repository at this point in the history
BREAKING CHANGE: OneOfSchemaObject type was added to SchemaObject
  • Loading branch information
raveclassic committed Nov 11, 2019
1 parent 50dd408 commit f9e2ab5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
36 changes: 25 additions & 11 deletions src/language/typescript/3.0/serializers/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getSerializedPropertyType,
getSerializedRecursiveType,
getSerializedRefType,
getSerializedUnionType,
intercalateSerializedTypes,
SERIALIZED_BOOLEAN_TYPE,
SERIALIZED_NUMERIC_TYPE,
Expand All @@ -22,9 +23,10 @@ import { constFalse } from 'fp-ts/lib/function';
import { includes } from '../../../../utils/array';
import { sequenceEither } from '@devexperts/utils/dist/adt/either.utils';
import { fromString, Ref } from '../../../../utils/ref';
import { AllOfSchemaObjectCodec, SchemaObject } from '../../../../schema/3.0/schema-object';
import { AllOfSchemaObjectCodec, OneOfSchemaObjectCodec, SchemaObject } from '../../../../schema/3.0/schema-object';
import { ReferenceObject, ReferenceObjectCodec } from '../../../../schema/3.0/reference-object';
import { traverseNEAEither } from '../../../../utils/either';
import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray';

type AdditionalProperties = boolean | ReferenceObject | SchemaObject;
type AllowedAdditionalProperties = true | ReferenceObject | SchemaObject;
Expand All @@ -41,18 +43,17 @@ export const serializeSchemaObject = (
const serializeSchemaObjectWithRecursion = (from: Ref, shouldTrackRecursion: boolean, name?: string) => (
schemaObject: SchemaObject,
): Either<Error, SerializedType> => {
if (OneOfSchemaObjectCodec.is(schemaObject)) {
return pipe(
serializeChildren(from, schemaObject.oneOf),
either.map(getSerializedUnionType),
either.map(getSerializedRecursiveType(from, shouldTrackRecursion)),
);
}

if (AllOfSchemaObjectCodec.is(schemaObject)) {
return pipe(
traverseNEAEither(schemaObject.allOf, item => {
if (ReferenceObjectCodec.is(item)) {
return pipe(
fromString(item.$ref),
either.map(getSerializedRefType(from)),
);
} else {
return serializeSchemaObjectWithRecursion(from, false)(item);
}
}),
serializeChildren(from, schemaObject.allOf),
either.map(getSerializedIntersectionType),
either.map(getSerializedRecursiveType(from, shouldTrackRecursion)),
);
Expand Down Expand Up @@ -166,3 +167,16 @@ const serializeSchemaObjectWithRecursion = (from: Ref, shouldTrackRecursion: boo
}
}
};

const serializeChildren = (
from: Ref,
children: NonEmptyArray<ReferenceObject | SchemaObject>,
): Either<Error, NonEmptyArray<SerializedType>> =>
traverseNEAEither(children, item =>
ReferenceObjectCodec.is(item)
? pipe(
fromString(item.$ref),
either.map(getSerializedRefType(from)),
)
: serializeSchemaObjectWithRecursion(from, false)(item),
);
26 changes: 24 additions & 2 deletions src/schema/3.0/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,30 @@ export const AllOfSchemaObjectCodec: Codec<AllOfSchemaObject> = recursion('AllOf
}),
);

export type SchemaObject = PrimitiveSchemaObject | ObjectSchemaObject | ArraySchemaObject | AllOfSchemaObject;
export interface OneOfSchemaObject extends BaseSchemaObject {
readonly oneOf: NonEmptyArray<ReferenceObject | SchemaObject>;
}

export const OneOfSchemaObjectCodec: Codec<OneOfSchemaObject> = recursion('OneOfSchemaObject', () =>
type({
...BaseSchemaObjectProps,
oneOf: nonEmptyArray(union([ReferenceObjectCodec, SchemaObjectCodec])),
}),
);

export type SchemaObject =
| PrimitiveSchemaObject
| ObjectSchemaObject
| ArraySchemaObject
| AllOfSchemaObject
| OneOfSchemaObject;

export const SchemaObjectCodec: Codec<SchemaObject> = recursion('SchemaObject', () =>
union([PrimitiveSchemaObjectCodec, ObjectSchemaObjectCodec, ArraySchemaObjectCodec, AllOfSchemaObjectCodec]),
union([
PrimitiveSchemaObjectCodec,
ObjectSchemaObjectCodec,
ArraySchemaObjectCodec,
AllOfSchemaObjectCodec,
OneOfSchemaObjectCodec,
]),
);

0 comments on commit f9e2ab5

Please sign in to comment.