Skip to content

Commit

Permalink
fixup!:
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Apr 24, 2024
1 parent db4bee7 commit 6cb2a05
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ TypeResult _resolveType(
spec,
state,
toDartName(name, className: true),
schema.buildRef(state.rootJson),
schema.resolveRef(state.rootJson),
nullable: nullable,
);

Expand Down
4 changes: 2 additions & 2 deletions packages/dynamite/dynamite/lib/src/models/openapi/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ abstract class Schema implements Built<Schema, SchemaBuilder> {
@BuiltValueField(wireName: r'$ref')
Uri? get ref;

Schema buildRef(Map<String, dynamic> json) {
Schema resolveRef(Map<String, dynamic> json) {
if (ref == null) {
throw StateError('Referenced schema can only be built when a ref is present');
throw StateError(r'Referenced schema can only be resolved when a $ref is present');
}

final rootID = json[r'$id'] as String?;
Expand Down
1 change: 1 addition & 0 deletions packages/dynamite/dynamite/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies:
dev_dependencies:
build_runner: ^2.4.9
built_value_generator: ^8.9.2
built_value_test: ^8.9.2
neon_lints:
git:
url: https://github.com/nextcloud/neon
Expand Down
68 changes: 68 additions & 0 deletions packages/dynamite/dynamite/test/openapi_spec_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// ignore_for_file: inference_failure_on_collection_literal

import 'package:built_value_test/matcher.dart';
import 'package:dynamite/src/models/openapi.dart';
import 'package:test/test.dart';

Expand All @@ -12,5 +15,70 @@ void main() {
schema = serializers.deserializeWith(Schema.serializer, value);
expect(schema, Schema());
});

test('Schema reference resolving', () {
const json = {
'openapi': '3.1.0',
'info': {
'title': 'reference resolving test',
'version': '0.0.1',
},
'components': {
'schemas': {
'SuperClass': {
'type': 'object',
'properties': {
'bool': {'type': 'boolean'},
'integer': {'type': 'integer'},
'double': {'type': 'number', 'format': 'float'},
'num': {'type': 'number'},
'string': {'type': 'string'},
'content-string': {
'type': 'string',
'nullable': true,
'contentMediaType': 'application/json',
'contentSchema': {'type': 'integer'},
},
'string-binary': {'type': 'string', 'format': 'binary'},
'list': {'type': 'array'},
'list-never': {'type': 'array', 'maxLength': 0},
'list-string': {
'type': 'array',
'items': {'type': 'string'},
},
},
},
'SubClass': {
r'$ref': '#/components/schemas/SuperClass',
},
},
},
'paths': {},
'tags': [],
};

final spec = serializers.deserializeWith(OpenAPI.serializer, json);
final superSchema = spec!.components!.schemas!['SuperClass']!;
expect(superSchema.type, equals(SchemaType.object));

final subSchema = spec.components!.schemas!['SubClass']!;
expect(
subSchema,
equalsBuilt(
Schema((b) {
b.ref = Uri.parse('#/components/schemas/SuperClass');
}),
),
);

expect(
subSchema.resolveRef(json),
equalsBuilt(
superSchema.rebuild((b) {
b.id = Uri.parse('#/components/schemas/SuperClass');
}),
),
);
});
});
}

0 comments on commit 6cb2a05

Please sign in to comment.