Skip to content

Commit

Permalink
fix: anyOf format
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
navalex authored Jun 22, 2024
1 parent 4640733 commit a2e215e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/generator/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function getDescription(field: DMMF.Field) {
function convertUnionType(
forceAnyOf: 'true' | 'false' | undefined,
type: JSONSchema7['type'],
format: string | undefined,
): JSONSchema7 {
if (forceAnyOf !== 'true') {
return { type }
Expand All @@ -170,7 +171,12 @@ function convertUnionType(
if (!isUnionType) {
return { type }
}
return { anyOf: type.map((t) => ({ type: t })) }
return {
anyOf: type.map((t) => ({
type: t,
...(isDefined(format) && t !== 'null' && { format }),
})),
}
}

function getPropertyDefinition(
Expand All @@ -184,14 +190,19 @@ function getPropertyDefinition(
const enumList = getEnumListByDMMFType(modelMetaData)(field)
const defaultValue = getDefaultValue(field)
const description = getDescription(field)
const convertedUnion = convertUnionType(
transformOptions.forceAnyOf,
type,
format,
)

const definition: JSONSchema7Definition = {
...convertUnionType(transformOptions.forceAnyOf, type),
...convertedUnion,
...(transformOptions.persistOriginalType && {
originalType: field.type,
}),
...(isDefined(defaultValue) && { default: defaultValue }),
...(isDefined(format) && { format }),
...(isDefined(format) && !convertedUnion.anyOf && { format }),
...(isDefined(items) && { items }),
...(isDefined(enumList) && { enum: enumList }),
...(isDefined(description) && { description }),
Expand Down
42 changes: 42 additions & 0 deletions src/tests/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ const datamodelMongoDB = /* Prisma */ `
}
`

const datamodelPostGresQL_anyOfCheck = /* Prisma */ `
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Article {
id Int @id @default(autoincrement())
expiredAt DateTime? @default(now())
}
`

describe('JSON Schema Generator', () => {
describe('db postgresql', () => {
it('returns JSON Schema for given models', async () => {
Expand Down Expand Up @@ -1092,6 +1104,36 @@ describe('JSON Schema Generator', () => {
throw new Error(ajv.errorsText(validate.errors))
}
})

it('nullable anyOf field', async () => {
const dmmf = await getDMMF({
datamodel: datamodelPostGresQL_anyOfCheck,
})
const jsonSchema = transformDMMF(dmmf, {
forceAnyOf: 'true',
})
expect(jsonSchema).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
definitions: {
Article: {
properties: {
id: { type: 'integer' },
expiredAt: {
anyOf: [
{ type: 'string', format: 'date-time' },
{ type: 'null' },
],
},
},
type: 'object',
},
},
properties: {
article: { $ref: '#/definitions/Article' },
},
type: 'object',
})
})
})

describe('db mongodb', () => {
Expand Down

0 comments on commit a2e215e

Please sign in to comment.