Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions lib/src/open_api/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ bool _checkReferenceTypes(name, ref, self) {
return true;
} else if (self is SchemaObject) {
// Reference types can be different if the reference is a SchemaObject
// should this return true?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@walsha2 are you able to weigh in here at all?

I think the _checkReferenceTypes needs either a rework or a fix.

The comment on this line seems to suggest it should return true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have attempted to implement a fix, which works for my test cases.

return false;
} else {
throw Exception(
Expand Down
1 change: 1 addition & 0 deletions lib/src/open_api/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class Schema with _$Schema {
);
}

// schema alias issue is caused by _checkReferenceTypes returning false
final isMatch = _checkReferenceTypes(ref, sRef, this);

if (!isMatch) {
Expand Down
25 changes: 25 additions & 0 deletions test/generation/alias.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"openapi": "3.0.1",
"info": {
"title": "schema",
"description": "Schema",
"version": "1.0.0"
},
"paths": {
},
"components": {
"schemas": {
"MyAlias": {
"type": "string"
},
"MyObject": {
"type": "object",
"properties": {
"myField": {
"$ref": "#/components/schemas/MyAlias"
}
}
}
}
}
}
103 changes: 103 additions & 0 deletions test/generation/ref_alias_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import 'dart:io';

import 'package:openapi_spec/openapi_spec.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

void main() {
final tmp = Directory(
p.join('test', 'tmp', 'generation'),
);

final truthJson = p.join('test', 'generation', 'alias.json');

late OpenApi spec;

void createTmpDir() {
if (tmp.existsSync()) {
tmp.deleteSync(recursive: true);
}
tmp.createSync(recursive: true);
}

group('Alias', () {
setUp(() {
spec = OpenApi.fromFile(source: truthJson);
createTmpDir();
});

/// Test schema generation (single file)
test('Can dereference ref alias', () async {
final model = spec.components!.schemas!["MyObject"]! as SchemaObject;
final field = model.properties!["myField"];
expect(field!.ref, equals("MyAlias"));

final dereferenced = field.dereference(components: spec.components!.schemas);

// the deferenced schema should now be a string
expect(dereferenced.type, equals(SchemaType.string));
expect(dereferenced.ref, isNull);
});

test('Generate Schema Code', () async {
final dir = tmp.createTempSync();
await spec.generate(
package: 'alias',
quiet: true,
replace: true,
destination: dir.path,
schemaOptions: SchemaGeneratorOptions(
singleFile: true,
),
);
final genDir = Directory(p.join(dir.path, 'schema'));
final genFiles = genDir.listSync();
expect(genFiles.length, 1);
expect(File(p.joinAll([genDir.path, "schema.dart"])).readAsStringSync(), equals(r'''
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: invalid_annotation_target
import 'package:freezed_annotation/freezed_annotation.dart';

part 'schema.g.dart';
part 'schema.freezed.dart';

// ==========================================
// CLASS: MyObject
// ==========================================

/// No Description
@freezed
class MyObject with _$MyObject {
const MyObject._();

/// Factory constructor for MyObject
const factory MyObject({
/// No Description
@JsonKey(includeIfNull: false) String? myField,
}) = _MyObject;

/// Object construction from a JSON representation
factory MyObject.fromJson(Map<String, dynamic> json) =>
_$MyObjectFromJson(json);

/// List of all property names of schema
static const List<String> propertyNames = ['myField'];

/// Perform validations on the schema property values
String? validateSchema() {
return null;
}

/// Map representation of object (not serialized)
Map<String, dynamic> toMap() {
return {
'myField': myField,
};
}
}
'''));
});
});
}
Loading