Skip to content

Commit 865214e

Browse files
authored
fix: serverOnly + one-to-one id relation regression on main (serverpod#3243)
1 parent cb4831b commit 865214e

File tree

5 files changed

+441
-31
lines changed

5 files changed

+441
-31
lines changed

tools/serverpod_cli/lib/src/analyzer/models/checker/analyze_checker.dart

-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ import 'package:serverpod_cli/src/analyzer/models/validation/keywords.dart';
33
import 'package:yaml/yaml.dart';
44

55
class AnalyzeChecker {
6-
static bool isIdType(dynamic type) {
7-
if (type is! String) return false;
8-
9-
return type == 'int' || type == 'int?';
10-
}
11-
126
static bool isParentDefined(dynamic node) {
137
if (node is! YamlMap) return false;
148
return node.containsKey(Keyword.parent);

tools/serverpod_cli/lib/src/analyzer/models/definitions.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class ResolvedInheritanceDefinition extends InheritanceDefinition {
359359
ResolvedInheritanceDefinition(this.classDefinition);
360360
}
361361

362-
abstract class RelationDefinition {
362+
sealed class RelationDefinition {
363363
String? name;
364364

365365
bool isForeignKeyOrigin;

tools/serverpod_cli/lib/src/analyzer/models/validation/restrictions.dart

+24-21
Original file line numberDiff line numberDiff line change
@@ -731,35 +731,38 @@ class Restrictions {
731731
dynamic content,
732732
SourceSpan? span,
733733
) {
734-
var errors = <SourceSpanSeverityException>[];
735-
var definition = documentDefinition;
736-
737-
if (definition is! ClassDefinition) return errors;
734+
var classDefinition = documentDefinition;
735+
if (classDefinition is! ClassDefinition) return const [];
738736

739-
var field = definition.findField(parentNodeName);
740-
if (field == null) return errors;
741-
var type = field.type.className;
737+
var field = classDefinition.findField(parentNodeName);
738+
if (field == null) return const [];
742739

743-
if (AnalyzeChecker.isIdType(type) &&
744-
!AnalyzeChecker.isParentDefined(content)) {
745-
errors.add(SourceSpanSeverityException(
746-
'The "parent" property must be defined on id fields.',
747-
span,
748-
));
740+
var type = field.type;
741+
if (type.isIdType && !AnalyzeChecker.isParentDefined(content)) {
742+
return [
743+
SourceSpanSeverityException(
744+
'The "parent" property must be defined on id fields.',
745+
span,
746+
)
747+
];
749748
}
750749

751-
if (!AnalyzeChecker.isFieldDefined(content)) {
752-
var isOptional = AnalyzeChecker.isOptionalDefined(content);
753-
var isServerOnly = field.scope == ModelFieldScopeDefinition.serverOnly;
754-
if (isServerOnly && !isOptional) {
755-
errors.add(SourceSpanSeverityException(
750+
var relation = field.relation;
751+
if (relation is! ObjectRelationDefinition) return const [];
752+
753+
if (!AnalyzeChecker.isFieldDefined(content) &&
754+
!classDefinition.serverOnly &&
755+
field.scope == ModelFieldScopeDefinition.serverOnly &&
756+
!relation.nullableRelation) {
757+
return [
758+
SourceSpanSeverityException(
756759
'The relation with scope "${field.scope.name}" requires the relation to be optional.',
757760
span,
758-
));
759-
}
761+
)
762+
];
760763
}
761764

762-
return errors;
765+
return const [];
763766
}
764767

765768
List<SourceSpanSeverityException> validateParentName(

tools/serverpod_cli/test/analyzer/models/stateful_analyzer/model_validation/relation/relation_manual_field_test.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ fields:
328328
test(
329329
'then the error message reports that the "optional" property '
330330
'is mutually exclusive with the "field" property.', () {
331-
var error = errors.first;
332331
expect(
333-
error.message,
334-
'The "optional" property is mutually exclusive with the "field" property.',
332+
errors.map((e) => e.message),
333+
contains(
334+
'The "optional" property is mutually exclusive with the "field" property.'),
335335
);
336336
});
337337
},

0 commit comments

Comments
 (0)