Skip to content

Commit

Permalink
Fix bug where we cant resolve custom types (#6016)
Browse files Browse the repository at this point in the history
* chore(schema): optimize(readability) document dependency check

* fix(schema): map tyhe schemaType type, and not the schematype

fixes a bug where we cant map custom defined string fields etc
  • Loading branch information
sgulseth authored Mar 18, 2024
1 parent dcd5537 commit 217a47d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/@sanity/schema/src/sanity/extractSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ export function extractSchema(
}

// map some known types
if (typesMap.has(schemaType.name)) {
return typesMap.get(schemaType.name)
if (schemaType.type && typesMap.has(schemaType.type.name)) {
return typesMap.get(schemaType.type.name)
}

// Cross dataset references are not supported
Expand Down Expand Up @@ -457,18 +457,20 @@ function sortByDependencies(compiledSchema: SchemaDef): string[] {

if ('fields' in schemaType) {
for (const field of gatherFields(schemaType)) {
const last = lastType(field.type)
if (last.name === 'document') {
dependencies.add(last)
continue
}

let schemaTypeName: string | undefined
if (schemaType.type.type) {
schemaTypeName = field.type.type.name
} else if ('jsonType' in schemaType.type) {
schemaTypeName = field.type.jsonType
}

if (
schemaTypeName === 'document' ||
schemaTypeName === 'object' ||
schemaTypeName === 'block'
) {
if (schemaTypeName === 'object' || schemaTypeName === 'block') {
if (isReferenceType(field.type)) {
field.type.to.forEach((ref) => dependencies.add(ref.type))
} else {
Expand All @@ -493,6 +495,7 @@ function sortByDependencies(compiledSchema: SchemaDef): string[] {

walkDependencies(schemaType, dependencies)
dependencyMap.set(schemaType, dependencies)
seen.clear() // Clear the seen set for the next type
})

// Sorts the types by their dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ Array [
"type": "object",
},
},
Object {
"name": "someTextType",
"type": "type",
"value": Object {
"type": "string",
},
},
Object {
"attributes": Object {
"_createdAt": Object {
Expand Down Expand Up @@ -4268,6 +4275,14 @@ Array [
"type": "inline",
},
},
"someTextType": Object {
"optional": true,
"type": "objectAttribute",
"value": Object {
"name": "someTextType",
"type": "inline",
},
},
"title": Object {
"optional": true,
"type": "objectAttribute",
Expand Down
11 changes: 11 additions & 0 deletions packages/@sanity/schema/test/extractSchema/extractSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ describe('Extract schema test', () => {
name: 'manuscript',
type: 'manuscript',
},
{
title: 'Some text',
name: 'someTextType',
type: 'someTextType',
},
{
title: 'customStringType',
name: 'customStringType',
Expand Down Expand Up @@ -225,6 +230,10 @@ describe('Extract schema test', () => {
},
],
},
defineType({
name: 'someTextType',
type: 'text',
}),
],
})

Expand All @@ -235,6 +244,7 @@ describe('Extract schema test', () => {
'sanity.imageDimensions',
'geopoint',
'slug',
'someTextType',
'sanity.fileAsset',
'code',
'customStringType',
Expand Down Expand Up @@ -269,6 +279,7 @@ describe('Extract schema test', () => {
'number',
'someInlinedObject',
'manuscript',
'someTextType',
'customStringType',
'blocks',
'other',
Expand Down

0 comments on commit 217a47d

Please sign in to comment.