Skip to content
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

Fix/dynamite/numbers #999

Merged
merged 3 commits into from
Oct 23, 2023
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
2 changes: 1 addition & 1 deletion external/nextcloud-server
Submodule nextcloud-server updated 1181 files
3 changes: 2 additions & 1 deletion packages/dynamite/dynamite/lib/src/builder/imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ List<Spec> generateImports(final AssetId outputId) => [
Directive.import('package:built_value/serializer.dart'),
Directive.import('package:built_value/standard_json_plugin.dart'),
Directive.import('package:collection/collection.dart'),
Directive.import('package:dynamite_runtime/content_string.dart'),
Directive.import('package:dynamite_runtime/built_value.dart'),
Directive.import('package:dynamite_runtime/http_client.dart'),
Directive.import('package:dynamite_runtime/models.dart'),
Directive.import('package:dynamite_runtime/utils.dart'),
Directive.import('package:meta/meta.dart'),
Directive.import('package:universal_io/io.dart'),
Expand Down
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
2 changes: 1 addition & 1 deletion packages/dynamite/dynamite/lib/src/builder/serializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ List<Spec> buildSerializer(final State state) {
const Code(').build();'),
const Code(''),
const Code(
'final Serializers _jsonSerializers = (_serializers.toBuilder()..addPlugin(StandardJsonPlugin())..addPlugin(const ContentStringPlugin())).build();',
'final Serializers _jsonSerializers = (_serializers.toBuilder()..add(DynamiteDoubleSerializer())..addPlugin(StandardJsonPlugin())..addPlugin(const ContentStringPlugin())).build();',
),
const Code('// coverage:ignore-end'),
];
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.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:built_value/built_value.dart';
import 'package:built_value/json_object.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:dynamite_runtime/content_string.dart';
import 'package:dynamite_runtime/built_value.dart';
import 'package:dynamite_runtime/http_client.dart';

part 'nested_ofs.openapi.g.dart';
Expand Down Expand Up @@ -521,6 +521,7 @@ final Serializers _serializers = (Serializers().toBuilder()
.build();

final Serializers _jsonSerializers = (_serializers.toBuilder()
..add(DynamiteDoubleSerializer())
..addPlugin(StandardJsonPlugin())
..addPlugin(const ContentStringPlugin()))
.build();
Expand Down
2 changes: 2 additions & 0 deletions packages/dynamite/dynamite_runtime/lib/built_value.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'src/built_value/content_string_serializer.dart';
export 'src/built_value/double_serializer.dart';
1 change: 0 additions & 1 deletion packages/dynamite/dynamite_runtime/lib/content_string.dart

This file was deleted.

1 change: 1 addition & 0 deletions packages/dynamite/dynamite_runtime/lib/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'src/models/content_string.dart';
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
import 'dart:convert';

import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'content_string.g.dart';

/// Json data encoded in a `String` as defined by [json-schema](https://json-schema.org/understanding-json-schema/reference/non_json_data.html#contentschema).
abstract class ContentString<T> implements Built<ContentString<T>, ContentStringBuilder<T>> {
/// Creates a new content `String`.
factory ContentString([final void Function(ContentStringBuilder<T>)? b]) = _$ContentString<T>;
const ContentString._();

/// The decoded value of the content `String`.
T get content;

/// The serializer for a content `String`.
static Serializer<ContentString<Object?>> get serializer => _$contentStringSerializer;
}
import 'package:dynamite_runtime/src/models/content_string.dart';

/// Serialization plugin for decoding [ContentString]s.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';

/// A built_value serializer for `double` values that does not accept integers.
class DynamiteDoubleSerializer implements PrimitiveSerializer<double> {
// Constant names match those in [double].
static const String _nan = 'NaN';
static const String _infinity = 'INF';
static const String _negativeInfinity = '-INF';

@override
final Iterable<Type> types = BuiltList<Type>([double]);

@override
final String wireName = 'double';

@override
Object serialize(
final Serializers serializers,
final double aDouble, {
final FullType specifiedType = FullType.unspecified,
}) {
if (aDouble.isNaN) {
return _nan;
} else if (aDouble.isInfinite) {
return aDouble.isNegative ? _negativeInfinity : _infinity;
} else {
return aDouble;
}
}

@override
double deserialize(
final Serializers serializers,
final Object serialized, {
final FullType specifiedType = FullType.unspecified,
}) {
if (serialized == _nan) {
return double.nan;
} else if (serialized == _negativeInfinity) {
return double.negativeInfinity;
} else if (serialized == _infinity) {
return double.infinity;
} else {
return serialized as double;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'content_string.g.dart';

/// Json data encoded in a `String` as defined by [json-schema](https://json-schema.org/understanding-json-schema/reference/non_json_data.html#contentschema).
abstract class ContentString<T> implements Built<ContentString<T>, ContentStringBuilder<T>> {
/// Creates a new content `String`.
factory ContentString([final void Function(ContentStringBuilder<T>)? b]) = _$ContentString<T>;
const ContentString._();

/// The decoded value of the content `String`.
T get content;

/// The serializer for a content `String`.
static Serializer<ContentString<Object?>> get serializer => _$contentStringSerializer;
}
Loading