Skip to content

Commit a2e215e

Browse files
authored
fix: anyOf format
* Add format to anyOf objects, and remove it of the property root * Lint fix * Use t instead of type * Fix misstyped convertedUnion * Adding basic test * Fix lint
1 parent 4640733 commit a2e215e

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/generator/properties.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ function getDescription(field: DMMF.Field) {
162162
function convertUnionType(
163163
forceAnyOf: 'true' | 'false' | undefined,
164164
type: JSONSchema7['type'],
165+
format: string | undefined,
165166
): JSONSchema7 {
166167
if (forceAnyOf !== 'true') {
167168
return { type }
@@ -170,7 +171,12 @@ function convertUnionType(
170171
if (!isUnionType) {
171172
return { type }
172173
}
173-
return { anyOf: type.map((t) => ({ type: t })) }
174+
return {
175+
anyOf: type.map((t) => ({
176+
type: t,
177+
...(isDefined(format) && t !== 'null' && { format }),
178+
})),
179+
}
174180
}
175181

176182
function getPropertyDefinition(
@@ -184,14 +190,19 @@ function getPropertyDefinition(
184190
const enumList = getEnumListByDMMFType(modelMetaData)(field)
185191
const defaultValue = getDefaultValue(field)
186192
const description = getDescription(field)
193+
const convertedUnion = convertUnionType(
194+
transformOptions.forceAnyOf,
195+
type,
196+
format,
197+
)
187198

188199
const definition: JSONSchema7Definition = {
189-
...convertUnionType(transformOptions.forceAnyOf, type),
200+
...convertedUnion,
190201
...(transformOptions.persistOriginalType && {
191202
originalType: field.type,
192203
}),
193204
...(isDefined(defaultValue) && { default: defaultValue }),
194-
...(isDefined(format) && { format }),
205+
...(isDefined(format) && !convertedUnion.anyOf && { format }),
195206
...(isDefined(items) && { items }),
196207
...(isDefined(enumList) && { enum: enumList }),
197208
...(isDefined(description) && { description }),

src/tests/generator.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ const datamodelMongoDB = /* Prisma */ `
6060
}
6161
`
6262

63+
const datamodelPostGresQL_anyOfCheck = /* Prisma */ `
64+
datasource db {
65+
provider = "postgresql"
66+
url = env("DATABASE_URL")
67+
}
68+
69+
model Article {
70+
id Int @id @default(autoincrement())
71+
expiredAt DateTime? @default(now())
72+
}
73+
`
74+
6375
describe('JSON Schema Generator', () => {
6476
describe('db postgresql', () => {
6577
it('returns JSON Schema for given models', async () => {
@@ -1092,6 +1104,36 @@ describe('JSON Schema Generator', () => {
10921104
throw new Error(ajv.errorsText(validate.errors))
10931105
}
10941106
})
1107+
1108+
it('nullable anyOf field', async () => {
1109+
const dmmf = await getDMMF({
1110+
datamodel: datamodelPostGresQL_anyOfCheck,
1111+
})
1112+
const jsonSchema = transformDMMF(dmmf, {
1113+
forceAnyOf: 'true',
1114+
})
1115+
expect(jsonSchema).toEqual({
1116+
$schema: 'http://json-schema.org/draft-07/schema#',
1117+
definitions: {
1118+
Article: {
1119+
properties: {
1120+
id: { type: 'integer' },
1121+
expiredAt: {
1122+
anyOf: [
1123+
{ type: 'string', format: 'date-time' },
1124+
{ type: 'null' },
1125+
],
1126+
},
1127+
},
1128+
type: 'object',
1129+
},
1130+
},
1131+
properties: {
1132+
article: { $ref: '#/definitions/Article' },
1133+
},
1134+
type: 'object',
1135+
})
1136+
})
10951137
})
10961138

10971139
describe('db mongodb', () => {

0 commit comments

Comments
 (0)