-
Notifications
You must be signed in to change notification settings - Fork 8
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: schema aliases can now be dereferenced correctly #80
Open
josh-burton
wants to merge
3
commits into
tazatechnology:main
Choose a base branch
from
josh-burton:ref-alias
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} | ||
''')); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.