Skip to content

Commit f639d97

Browse files
authored
fix: Don't include hidden fields (#51)
* fix: Don't include hidden fields According to the Payload docs, hidden fields should not appear in any API. * fix: Update test snapshots
1 parent 469fbf4 commit f639d97

File tree

3 files changed

+20
-363
lines changed

3 files changed

+20
-363
lines changed

src/openapi/generators.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
} from 'payload'
1818
import { entityToJSONSchema } from 'payload'
1919
import type { SanitizedPluginOptions } from '../types.js'
20+
import { isHiddenField } from '../utils/fields.js'
2021
import { mapValuesAsync, visitObjectNodes } from '../utils/objects.js'
2122
import { type ComponentType, collectionName, componentName, globalName } from './naming.js'
2223
import { apiKeySecurity, generateSecuritySchemes } from './securitySchemes.js'
@@ -96,6 +97,14 @@ const generateSchemaObject = (config: SanitizedConfig, collection: Collection):
9697
undefined,
9798
)
9899

100+
schema.properties = Object.fromEntries(
101+
Object.entries(schema.properties ?? {}).filter(([property]) => {
102+
const field = collection.config.fields.find(field => (field as FieldBase).name === property)
103+
104+
return !isHiddenField(field)
105+
}),
106+
)
107+
99108
return {
100109
...schema,
101110
title: collectionName(collection).singular,
@@ -133,9 +142,12 @@ const generateRequestBodySchema = (
133142
)
134143

135144
schema.properties = Object.fromEntries(
136-
Object.entries(schema.properties ?? {}).filter(
137-
([property]) => !['id', 'createdAt', 'updatedAt'].includes(property),
138-
),
145+
Object.entries(schema.properties ?? {}).filter(([property]) => {
146+
const field = collection.config.fields.find(field => (field as FieldBase).name === property)
147+
const isRequestBodyProperty = !['id', 'createdAt', 'updatedAt'].includes(property)
148+
149+
return isRequestBodyProperty && !isHiddenField(field)
150+
}),
139151
)
140152
schema.required = ((schema.required ?? []) as string[]).filter(
141153
property => schema.properties?.[property] !== undefined,

src/utils/fields.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Field } from 'payload'
2+
3+
export const isHiddenField = (field: Field | undefined) => {
4+
return field?.type !== 'ui' && Boolean(field?.hidden)
5+
}

0 commit comments

Comments
 (0)