Skip to content

Commit

Permalink
fix(dynamite): allow default values and validators in atomic allOf types
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda committed Apr 27, 2024
1 parent 397c911 commit 39459c9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 52 deletions.
113 changes: 61 additions & 52 deletions packages/dynamite/dynamite/lib/src/builder/resolve_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ Spec buildInterface(

b.implements.add(refer(interfaceName));
} else {
final property = _generateProperty(
object,
_generateProperty(
b,
object.className,
schema.formattedDescription,
object,
schema,
defaults,
validators,
);

b.methods.add(property);
}
}
}
Expand Down Expand Up @@ -153,7 +154,6 @@ void _generateProperties(
for (final property in properties) {
final propertyName = property.key;
final propertySchema = property.value;
final dartName = toDartName(propertyName);

var result = resolveType(
spec,
Expand All @@ -178,60 +178,69 @@ void _generateProperties(
state.resolvedTypes.add(result);
}

final method = _generateProperty(
result,
_generateProperty(
b,
propertyName,
propertySchema.formattedDescription,
result,
propertySchema,
defaults,
validators,
);
b.methods.add(method);

final $default = propertySchema.$default;
if ($default != null) {
final value = $default.toString();
defaults.writeln('b.$dartName = ${valueToEscapedValue(result, value)};');
}

if (result is TypeResultOneOf && !result.isSingleValue) {
final expression = refer('b').property(dartName).nullSafeProperty('validateOneOf').call([]);
validators.addExpression(expression);
} else if (result is TypeResultAnyOf && !result.isSingleValue) {
final expression = refer('b').property(dartName).nullSafeProperty('validateAnyOf').call([]);
validators.addExpression(expression);
}

buildPatternCheck(propertySchema, 'b.$dartName', dartName).forEach(validators.addExpression);
}
}

Method _generateProperty(
TypeResult type,
void _generateProperty(
ClassBuilder b,
String propertyName,
String? description,
TypeResult result,
openapi.Schema schema,
StringSink defaults,
BlockBuilder validators,
) {
return Method(
(b) {
final name = toFieldName(toDartName(propertyName), type.name);
b
..name = name
..type = MethodType.getter
..docs.addAll(escapeDescription(description));

if (type is TypeResultSomeOf && type.isSingleValue) {
b.returns = refer(type.dartType.name);
} else {
b.returns = refer(type.nullableName);
}
final dartName = toDartName(propertyName);
final name = toFieldName(dartName, result.name);

final builtValueFieldAnnotations = <String, Expression>{};
if (name != propertyName) {
builtValueFieldAnnotations['wireName'] = literalString(propertyName);
}
b.methods.add(
Method(
(b) {
b
..name = name
..type = MethodType.getter
..docs.addAll(escapeDescription(schema.formattedDescription));

if (builtValueFieldAnnotations.isNotEmpty) {
b.annotations.add(
refer('BuiltValueField').call([], builtValueFieldAnnotations),
);
}
},
if (result is TypeResultSomeOf && result.isSingleValue) {
b.returns = refer(result.dartType.name);
} else {
b.returns = refer(result.nullableName);
}

final builtValueFieldAnnotations = <String, Expression>{};
if (name != propertyName) {
builtValueFieldAnnotations['wireName'] = literalString(propertyName);
}

if (builtValueFieldAnnotations.isNotEmpty) {
b.annotations.add(
refer('BuiltValueField').call([], builtValueFieldAnnotations),
);
}
},
),
);

final $default = schema.$default;
if ($default != null) {
final value = $default.toString();
defaults.writeln('b.$dartName = ${valueToEscapedValue(result, value)};');
}

if (result is TypeResultOneOf && !result.isSingleValue) {
final expression = refer('b').property(dartName).nullSafeProperty('validateOneOf').call([]);
validators.addExpression(expression);
} else if (result is TypeResultAnyOf && !result.isSingleValue) {
final expression = refer('b').property(dartName).nullSafeProperty('validateAnyOf').call([]);
validators.addExpression(expression);
}

buildPatternCheck(schema, 'b.$dartName', dartName).forEach(validators.addExpression);
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ abstract interface class $BaseNestedAllOfInterface implements $BaseAllOfInterfac
@BuiltValueHook(finalizeBuilder: true)
static void _validate($BaseNestedAllOfInterfaceBuilder b) {
$BaseAllOfInterface._validate(b);
b.baseOneOf?.validateOneOf();
b.baseAnyOf?.validateAnyOf();
}
}

Expand Down

0 comments on commit 39459c9

Please sign in to comment.