Skip to content

Commit

Permalink
Support DateTime format for parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
hpoul committed Jan 25, 2024
1 parent e26327b commit 25b531c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/openapi_code_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.0

* Support `DateTime` format for parameters.

## 1.1.4+4

* Add option to ignore security schemes.
Expand Down
14 changes: 12 additions & 2 deletions packages/openapi_code_builder/lib/src/openapi_code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class OpenApiLibraryGenerator {
final _void = refer('void');
final _uint8List = refer('Uint8List', 'dart:typed_data');
final _typeString = refer('String');
final _typeDateTime = refer('DateTime');
final _literalNullCode = literalNull.code;

static const mediaTypeJson = OpenApiContentType.json;
Expand Down Expand Up @@ -606,6 +607,8 @@ class OpenApiLibraryGenerator {
final paramEnumType = paramType;
return refer(paramEnumType.symbol! + 'Ext')
.property('fromName')([asString]);
} else if (paramType == _typeDateTime) {
return _typeDateTime.property('parse')([asString]);
} else if (paramType != _typeString) {
throw StateError(
'Unsupported paramType for string $paramType');
Expand Down Expand Up @@ -680,11 +683,18 @@ class OpenApiLibraryGenerator {
} else {
expression = expression.nullSafeProperty('name');
}
} else if (paramType == _typeDateTime) {
if (param.isRequired) {
expression = expression.property('toIso8601String')([]);
} else {
expression =
expression.nullSafeProperty('toIso8601String')([]);
}
} else if (paramType != _typeString) {
// TODO not sure if this makes sense, maybe we should just
// use `toString`?
throw StateError(
'Unsupported paramType for string $paramType');
'encodeParameter: Unsupported paramType for string $paramType');
}
return refer('encodeString')([expression]);
case APIType.number:
Expand Down Expand Up @@ -1304,7 +1314,7 @@ class OpenApiLibraryGenerator {
return _schemaReference(parent, schema);
}
if (schema.format == 'date-time') {
return refer('DateTime');
return _typeDateTime;
}
if (schema.format == 'uuid') {
return _apiUuid;
Expand Down

0 comments on commit 25b531c

Please sign in to comment.