Skip to content

Commit

Permalink
feat(dynamite,nextcloud): add number format support
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 Oct 19, 2023
1 parent b20490e commit 221815a
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 401 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TypeResultObject resolveObject(
final result = resolveType(
spec,
state,
propertySchema.type!,
propertySchema.type!.name,
propertySchema,
);
defaults.add('..${toDartName(property.key)} = ${valueToEscapedValue(result, value)}');
Expand Down
45 changes: 26 additions & 19 deletions packages/dynamite/dynamite/lib/src/builder/resolve_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,42 @@ TypeResult resolveType(
);
} else {
switch (schema.type) {
case 'boolean':
case openapi.SchemaType.boolean:
result = TypeResultBase(
'bool',
nullable: nullable,
);
case 'integer':
case openapi.SchemaType.integer:
result = TypeResultBase(
'int',
nullable: nullable,
);
case 'number':
result = TypeResultBase(
'num',
nullable: nullable,
);
case 'string':
switch (schema.format) {
case 'binary':
result = TypeResultBase(

case openapi.SchemaType.number:
result = switch (schema.format) {
'float' || 'double' => TypeResultBase(
'double',
nullable: nullable,
),
_ => TypeResultBase(
'num',
nullable: nullable,
),
};

case openapi.SchemaType.string:
result = switch (schema.format) {
'binary' => TypeResultBase(
'Uint8List',
nullable: nullable,
);
}
),
_ => TypeResultBase(
'String',
nullable: nullable,
),
};

result = TypeResultBase(
'String',
nullable: nullable,
);
case 'array':
case openapi.SchemaType.array:
if (schema.items != null) {
final subResult = resolveType(
spec,
Expand All @@ -108,7 +115,7 @@ TypeResult resolveType(
nullable: nullable,
);
}
case 'object':
case openapi.SchemaType.object:
if (schema.properties == null) {
if (schema.additionalProperties == null) {
result = TypeResultBase(
Expand Down
1 change: 1 addition & 0 deletions packages/dynamite/dynamite/lib/src/models/openapi.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 36 additions & 2 deletions packages/dynamite/dynamite/lib/src/models/openapi/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:built_value/built_value.dart';
import 'package:built_value/json_object.dart';
import 'package:built_value/serializer.dart';
import 'package:dynamite/src/helpers/docs.dart';
import 'package:dynamite/src/models/exceptions.dart';
import 'package:dynamite/src/models/openapi/discriminator.dart';

part 'schema.g.dart';
Expand Down Expand Up @@ -30,7 +31,7 @@ abstract class Schema implements Built<Schema, SchemaBuilder> {

bool get deprecated;

String? get type;
SchemaType? get type;

String? get format;

Expand Down Expand Up @@ -62,7 +63,8 @@ abstract class Schema implements Built<Schema, SchemaBuilder> {

bool get nullable;

bool get isContentString => type == 'string' && (contentMediaType?.isNotEmpty ?? false) && contentSchema != null;
bool get isContentString =>
type == SchemaType.string && (contentMediaType?.isNotEmpty ?? false) && contentSchema != null;

Iterable<String> get formattedDescription => descriptionToDocs(description);

Expand All @@ -71,5 +73,37 @@ abstract class Schema implements Built<Schema, SchemaBuilder> {
b
..deprecated ??= false
..nullable ??= false;

const allowedNumberFormats = [null, 'float', 'double'];
if (b.type == SchemaType.number && !allowedNumberFormats.contains(b.format)) {
throw OpenAPISpecError('Format "${b.format}" is not allowed for ${b.type}. Use one of $allowedNumberFormats.');
}
const allowedIntegerFormats = [null, 'int32', 'int64'];
if (b.type == SchemaType.integer) {
if (!allowedIntegerFormats.contains(b.format)) {
throw OpenAPISpecError('Format "${b.format}" is not allowed for ${b.type}. Use one of $allowedIntegerFormats.');
} else if (b.format != null) {
print(
'All integers are represented as `int` meaning 64bit precision in the VM/wasm and 53bit precision on js.',
);
}
}
}
}

class SchemaType extends EnumClass {
const SchemaType._(super.name);

static const SchemaType boolean = _$schemaTypeBoolean;
static const SchemaType integer = _$schemaTypeInteger;
static const SchemaType number = _$schemaTypeNumber;
static const SchemaType string = _$schemaTypeString;
static const SchemaType array = _$schemaTypeArray;
static const SchemaType object = _$schemaTypeObject;

static BuiltSet<SchemaType> get values => _$schemaTypeValues;

static SchemaType valueOf(final String name) => _$schemaType(name);

static Serializer<SchemaType> get serializer => _$schemaTypeSerializer;
}
63 changes: 57 additions & 6 deletions packages/dynamite/dynamite/lib/src/models/openapi/schema.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/nextcloud/lib/src/api/files.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ abstract interface class TemplateFileCreatorInterface {
String get $extension;
String? get iconClass;
BuiltList<String> get mimetypes;
num? get ratio;
double? get ratio;
String get actionLabel;
}

Expand Down
16 changes: 8 additions & 8 deletions packages/nextcloud/lib/src/api/files.openapi.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions packages/nextcloud/lib/src/api/files_sharing.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2896,7 +2896,7 @@ abstract class RemoteUnshareResponseApplicationJson
abstract interface class ShareInfo_SizeInterface {
JsonObject get data;
int? get $int;
num? get $num;
double? get $double;
}

abstract class ShareInfo_Size implements ShareInfo_SizeInterface, Built<ShareInfo_Size, ShareInfo_SizeBuilder> {
Expand Down Expand Up @@ -2945,9 +2945,9 @@ class _$ShareInfo_SizeSerializer implements PrimitiveSerializer<ShareInfo_Size>
result._$int = _jsonSerializers.deserialize(data, specifiedType: const FullType(int))! as int;
} catch (_) {}
try {
result._$num = _jsonSerializers.deserialize(data, specifiedType: const FullType(num))! as num;
result._$double = _jsonSerializers.deserialize(data, specifiedType: const FullType(double))! as double;
} catch (_) {}
assert([result._$int, result._$num].where((final x) => x != null).isNotEmpty, 'Need oneOf for ${result._data}');
assert([result._$int, result._$double].where((final x) => x != null).isNotEmpty, 'Need oneOf for ${result._data}');
return result.build();
}
}
Expand Down Expand Up @@ -2987,7 +2987,7 @@ abstract class ShareInfo implements ShareInfoInterface, Built<ShareInfo, ShareIn
@BuiltValue(instantiable: false)
abstract interface class Share_ItemSizeInterface {
JsonObject get data;
num? get $num;
double? get $double;
int? get $int;
}

Expand Down Expand Up @@ -3034,12 +3034,12 @@ class _$Share_ItemSizeSerializer implements PrimitiveSerializer<Share_ItemSize>
}) {
final result = Share_ItemSizeBuilder()..data = JsonObject(data);
try {
result._$num = _jsonSerializers.deserialize(data, specifiedType: const FullType(num))! as num;
result._$double = _jsonSerializers.deserialize(data, specifiedType: const FullType(double))! as double;
} catch (_) {}
try {
result._$int = _jsonSerializers.deserialize(data, specifiedType: const FullType(int))! as int;
} catch (_) {}
assert([result._$num, result._$int].where((final x) => x != null).isNotEmpty, 'Need oneOf for ${result._data}');
assert([result._$double, result._$int].where((final x) => x != null).isNotEmpty, 'Need oneOf for ${result._data}');
return result.build();
}
}
Expand Down
Loading

0 comments on commit 221815a

Please sign in to comment.