这是indexloc提供的服务,不要输入任何密码
Skip to content

Decimal Parsing Improvements #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/data_types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Data Types

This document provides information about how Tonik is mapping data types in OpenAPI into Dart.


## Primitive Types

| OAS Type | OAS Format | Dart Type | Dart Package | Comment |
|----------|------------|-----------|--------------|---------|
| `string` | `date-time` | `DateTime` | `dart:core` | ISO 8601 datetime format |
| `string` | `date` | `Date` | `tonik_util` | RFC3339 date format (YYYY-MM-DD) |
| `string` | `decimal`, `currency`, `money`, `number` | `BigDecimal` | `big_decimal` | High-precision decimal numbers |
| `string` | `enum` | `enum` | Generated | Custom enum type |
| `string` | (default) | `String` | `dart:core` | Standard string type |
| `number` | `float`, `double` | `double` | `dart:core` | 64-bit floating point |
| `number` | (default) | `num` | `dart:core` | Generic number type |
| `integer` | `enum` | `enum` | Generated | Custom enum type |
| `integer` | (default) | `int` | `dart:core` | 64-bit integer |
| `boolean` | (any) | `bool` | `dart:core` | Boolean type |
| `array` | (any) | `List<T>` | `dart:core` | List of specified type |

8 changes: 7 additions & 1 deletion packages/tonik_parse/lib/src/model_importer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ class ModelImporter {
context: context,
),
'string' when schema.format == 'date' => DateModel(context: context),
'string' when schema.format == 'decimal' || schema.format == 'currency' =>
'string'
when [
'decimal',
'currency',
'money',
'number',
].contains(schema.format) =>
DecimalModel(context: context),
'string' when schema.enumerated != null => _parseEnum<String>(
name,
Expand Down
33 changes: 27 additions & 6 deletions packages/tonik_parse/test/model/model_property_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ void main() {
'float': {'type': 'number', 'format': 'float'},
'double': {'type': 'number', 'format': 'double'},
'decimal': {'type': 'string', 'format': 'decimal'},
'decimal-alt': {'type': 'string', 'format': 'currency'},
'currency': {'type': 'string', 'format': 'currency'},
'money': {'type': 'string', 'format': 'money'},
'numberString': {'type': 'string', 'format': 'number'},
'boolean': {'type': 'boolean'},
'date': {'type': 'string', 'format': 'date'},
'dateTime': {'type': 'string', 'format': 'date-time'},
Expand Down Expand Up @@ -88,17 +90,36 @@ void main() {
expect(double.model, isA<DoubleModel>());
});

test('imports decimal', () {
test('imports decimal format', () {
final api = Importer().import(fileContent);

final model = api.models.first as ClassModel;
final decimal = model.properties.firstWhere((p) => p.name == 'decimal');
expect(decimal.model, isA<DecimalModel>());
});

test('imports currency format', () {
final api = Importer().import(fileContent);

final model = api.models.first as ClassModel;
final currency = model.properties.firstWhere((p) => p.name == 'currency');
expect(currency.model, isA<DecimalModel>());
});

test('imports money format', () {
final api = Importer().import(fileContent);

final model = api.models.first as ClassModel;
final money = model.properties.firstWhere((p) => p.name == 'money');
expect(money.model, isA<DecimalModel>());
});

final decimalAlt = model.properties.firstWhere(
(p) => p.name == 'decimal-alt',
);
expect(decimalAlt.model, isA<DecimalModel>());
test('imports number string format', () {
final api = Importer().import(fileContent);

final model = api.models.first as ClassModel;
final numberString = model.properties.firstWhere((p) => p.name == 'numberString');
expect(numberString.model, isA<DecimalModel>());
});

test('imports boolean', () {
Expand Down