diff --git a/docs/data_types.md b/docs/data_types.md new file mode 100644 index 0000000..39cd8fd --- /dev/null +++ b/docs/data_types.md @@ -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` | `dart:core` | List of specified type | + diff --git a/packages/tonik_parse/lib/src/model_importer.dart b/packages/tonik_parse/lib/src/model_importer.dart index 12bb191..5ec0bd0 100644 --- a/packages/tonik_parse/lib/src/model_importer.dart +++ b/packages/tonik_parse/lib/src/model_importer.dart @@ -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( name, diff --git a/packages/tonik_parse/test/model/model_property_test.dart b/packages/tonik_parse/test/model/model_property_test.dart index ae141b0..a1bc71d 100644 --- a/packages/tonik_parse/test/model/model_property_test.dart +++ b/packages/tonik_parse/test/model/model_property_test.dart @@ -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'}, @@ -88,17 +90,36 @@ void main() { expect(double.model, isA()); }); - 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()); + }); + + 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()); + }); + + 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()); + }); - final decimalAlt = model.properties.firstWhere( - (p) => p.name == 'decimal-alt', - ); - expect(decimalAlt.model, isA()); + 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()); }); test('imports boolean', () {