diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 966face9..c0fbcde1 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -1472,6 +1472,53 @@ TypeResult resolveObject( ..type = MethodType.getter, ), ]); + + final defaults = []; + for (final propertyName in schema.properties!.keys) { + final propertySchema = schema.properties![propertyName]!; + if (propertySchema.default_ != null) { + final value = propertySchema.default_!.toString(); + final result = resolveType( + spec, + state, + propertySchema.type!, + propertySchema, + ); + defaults.add('..${_toDartName(propertyName)} = ${_valueToEscapedValue(result, value)}'); + } + } + if (defaults.isNotEmpty) { + b.methods.add( + Method( + (final b) => b + ..name = '_defaults' + ..returns = refer('void') + ..static = true + ..lambda = true + ..annotations.add( + refer('BuiltValueHook').call( + [], + { + 'initializeBuilder': refer('true'), + }, + ), + ) + ..requiredParameters.add( + Parameter( + (final b) => b + ..name = 'b' + ..type = refer('${state.prefix}${identifier}Builder'), + ), + ) + ..body = Code( + [ + 'b', + ...defaults, + ].join(), + ), + ), + ); + } }, ), );