Skip to content

Commit 4dca30c

Browse files
authored
Use AutoTitleAnnotationId to add automatic titles to avoid interfering with user-defined titles (#3918)
1 parent 320557a commit 4dca30c

File tree

5 files changed

+30
-40
lines changed

5 files changed

+30
-40
lines changed

.changeset/clean-turkeys-fry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Use a specific annotation (`AutoTitleAnnotationId`) to add automatic titles (added by `Struct` and `Class` APIs), instead of `TitleAnnotationId`, to avoid interfering with user-defined titles.

packages/effect/src/Schema.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,8 +2605,8 @@ const getDefaultTypeLiteralAST = <
26052605
})
26062606
}
26072607
return new AST.Transformation(
2608-
new AST.TypeLiteral(from, issFrom, { [AST.TitleAnnotationId]: "Struct (Encoded side)" }),
2609-
new AST.TypeLiteral(to, issTo, { [AST.TitleAnnotationId]: "Struct (Type side)" }),
2608+
new AST.TypeLiteral(from, issFrom, { [AST.AutoTitleAnnotationId]: "Struct (Encoded side)" }),
2609+
new AST.TypeLiteral(to, issTo, { [AST.AutoTitleAnnotationId]: "Struct (Type side)" }),
26102610
new AST.TypeLiteralTransformation(transformations)
26112611
)
26122612
}
@@ -8100,15 +8100,6 @@ const extendFields = (a: Struct.Fields, b: Struct.Fields): Struct.Fields => {
81008100
return out
81018101
}
81028102
8103-
// does not overwrite existing title annotation
8104-
const orElseTitleAnnotation = <A, I, R>(schema: Schema<A, I, R>, title: string): Schema<A, I, R> => {
8105-
const annotation = AST.getTitleAnnotation(schema.ast)
8106-
if (option_.isNone(annotation)) {
8107-
return schema.annotations({ title })
8108-
}
8109-
return schema
8110-
}
8111-
81128103
type MakeOptions = boolean | {
81138104
readonly disableValidation?: boolean
81148105
}
@@ -8128,9 +8119,9 @@ const makeClass = ({ Base, annotations, disableToString, fields, identifier, kin
81288119
disableToString?: boolean | undefined
81298120
}): any => {
81308121
const classSymbol = Symbol.for(`effect/Schema/${kind}/${identifier}`)
8131-
const validateSchema = orElseTitleAnnotation(schema, `${identifier} (Constructor)`)
8132-
const encodedSide: Schema.Any = orElseTitleAnnotation(schema, `${identifier} (Encoded side)`)
8133-
const typeSide = orElseTitleAnnotation(typeSchema(schema), `${identifier} (Type side)`)
8122+
const validateSchema = schema.annotations({ [AST.AutoTitleAnnotationId]: `${identifier} (Constructor)` })
8123+
const encodedSide: Schema.Any = schema.annotations({ [AST.AutoTitleAnnotationId]: `${identifier} (Encoded side)` })
8124+
const typeSide = typeSchema(schema).annotations({ [AST.AutoTitleAnnotationId]: `${identifier} (Type side)` })
81348125
const fallbackInstanceOf = (u: unknown) => Predicate.hasProperty(u, classSymbol) && ParseResult.is(typeSide)(u)
81358126
const klass = class extends Base {
81368127
constructor(

packages/effect/src/SchemaAST.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ export type TitleAnnotation = string
126126
*/
127127
export const TitleAnnotationId: unique symbol = Symbol.for("effect/annotation/Title")
128128

129+
/** @internal */
130+
export const AutoTitleAnnotationId: unique symbol = Symbol.for("effect/annotation/AutoTitle")
131+
129132
/**
130133
* @category annotations
131134
* @since 3.10.0
@@ -346,6 +349,9 @@ export const getMissingMessageAnnotation = getAnnotation<MissingMessageAnnotatio
346349
*/
347350
export const getTitleAnnotation = getAnnotation<TitleAnnotation>(TitleAnnotationId)
348351

352+
/** @internal */
353+
export const getAutoTitleAnnotation = getAnnotation<TitleAnnotation>(AutoTitleAnnotationId)
354+
349355
/**
350356
* @category annotations
351357
* @since 3.10.0
@@ -2814,6 +2820,7 @@ const formatKeyword = (ast: AST): string => Option.getOrElse(getExpected(ast), (
28142820
const getExpected = (ast: Annotated): Option.Option<string> => {
28152821
return getIdentifierAnnotation(ast).pipe(
28162822
Option.orElse(() => getTitleAnnotation(ast)),
2817-
Option.orElse(() => getDescriptionAnnotation(ast))
2823+
Option.orElse(() => getDescriptionAnnotation(ast)),
2824+
Option.orElse(() => getAutoTitleAnnotation(ast))
28182825
)
28192826
}

packages/effect/test/Schema/JSONSchema.test.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,6 @@ schema (Suspend): <suspended schema>`
15661566
expectJSONSchema(Schema.typeSchema(A), {
15671567
"$schema": "http://json-schema.org/draft-07/schema#",
15681568
"type": "object",
1569-
"title": "A (Type side)",
15701569
"required": [
15711570
"a"
15721571
],
@@ -1602,8 +1601,7 @@ schema (Suspend): <suspended schema>`
16021601
"type": "string"
16031602
}
16041603
},
1605-
"additionalProperties": false,
1606-
"title": "A (Encoded side)"
1604+
"additionalProperties": false
16071605
})
16081606
})
16091607
})
@@ -2057,8 +2055,7 @@ schema (Suspend): <suspended schema>`
20572055
"minLength": 1
20582056
}
20592057
},
2060-
"additionalProperties": false,
2061-
"title": "Struct (Encoded side)"
2058+
"additionalProperties": false
20622059
})
20632060
expectJSONSchema(Schema.typeSchema(schema), {
20642061
"$schema": "http://json-schema.org/draft-07/schema#",
@@ -2074,8 +2071,7 @@ schema (Suspend): <suspended schema>`
20742071
"minLength": 1
20752072
}
20762073
},
2077-
"additionalProperties": false,
2078-
"title": "Struct (Type side)"
2074+
"additionalProperties": false
20792075
})
20802076
expectJSONSchema(Schema.encodedSchema(schema), {
20812077
"$schema": "http://json-schema.org/draft-07/schema#",
@@ -2107,8 +2103,7 @@ schema (Suspend): <suspended schema>`
21072103
"minLength": 1
21082104
}
21092105
},
2110-
"additionalProperties": false,
2111-
"title": "Struct (Encoded side)"
2106+
"additionalProperties": false
21122107
}
21132108
)
21142109
})
@@ -2130,8 +2125,7 @@ schema (Suspend): <suspended schema>`
21302125
"minLength": 1
21312126
}
21322127
},
2133-
"additionalProperties": false,
2134-
"title": "Struct (Encoded side)"
2128+
"additionalProperties": false
21352129
}
21362130
)
21372131
})
@@ -2155,8 +2149,7 @@ schema (Suspend): <suspended schema>`
21552149
"minLength": 1
21562150
}
21572151
},
2158-
"additionalProperties": false,
2159-
"title": "Struct (Encoded side)"
2152+
"additionalProperties": false
21602153
}
21612154
)
21622155
})

packages/platform/test/OpenApiJsonSchema.test.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,8 +1528,7 @@ schema (Suspend): <suspended schema>`
15281528
"type": "string"
15291529
}
15301530
},
1531-
"additionalProperties": false,
1532-
"title": "A (Encoded side)"
1531+
"additionalProperties": false
15331532
})
15341533
})
15351534
})
@@ -1949,8 +1948,7 @@ schema (Suspend): <suspended schema>`
19491948
"minLength": 1
19501949
}
19511950
},
1952-
"additionalProperties": false,
1953-
"title": "Struct (Encoded side)"
1951+
"additionalProperties": false
19541952
})
19551953
expectJSONSchema(Schema.typeSchema(schema), {
19561954
"type": "object",
@@ -1965,8 +1963,7 @@ schema (Suspend): <suspended schema>`
19651963
"minLength": 1
19661964
}
19671965
},
1968-
"additionalProperties": false,
1969-
"title": "Struct (Type side)"
1966+
"additionalProperties": false
19701967
})
19711968
expectJSONSchema(Schema.encodedSchema(schema), {
19721969
"type": "object",
@@ -1996,8 +1993,7 @@ schema (Suspend): <suspended schema>`
19961993
"minLength": 1
19971994
}
19981995
},
1999-
"additionalProperties": false,
2000-
"title": "Struct (Encoded side)"
1996+
"additionalProperties": false
20011997
}
20021998
)
20031999
})
@@ -2018,8 +2014,7 @@ schema (Suspend): <suspended schema>`
20182014
"minLength": 1
20192015
}
20202016
},
2021-
"additionalProperties": false,
2022-
"title": "Struct (Encoded side)"
2017+
"additionalProperties": false
20232018
}
20242019
)
20252020
})
@@ -2042,8 +2037,7 @@ schema (Suspend): <suspended schema>`
20422037
"minLength": 1
20432038
}
20442039
},
2045-
"additionalProperties": false,
2046-
"title": "Struct (Encoded side)"
2040+
"additionalProperties": false
20472041
}
20482042
)
20492043
})

0 commit comments

Comments
 (0)