Skip to content

Commit 7a25514

Browse files
authored
Merge pull request #74 from atoka93/master
Fix enum value retrieval error
2 parents 88a0693 + 3a0e223 commit 7a25514

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

openapi-generator/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.3.2
2+
3+
- Fix the enum value retrieval error
4+
15
## 3.3.1
26

37
- Update the _analyzer_ constraint

openapi-generator/lib/src/extensions/type_methods.dart

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:mirrors';
22

33
import 'package:analyzer/dart/element/type.dart';
4-
import 'package:source_gen/source_gen.dart' show ConstantReader;
4+
import 'package:source_gen/source_gen.dart' show ConstantReader, TypeChecker;
55

66
/// Extension adding the type methods to `ConstantReader`.
77
extension TypeMethods on ConstantReader {
@@ -54,26 +54,20 @@ extension TypeMethods on ConstantReader {
5454
}
5555

5656
final classMirror = reflectClass(T);
57-
final typeMirror = reflectType(T);
5857
if (!classMirror.isEnum) {
5958
throw Exception(
6059
'Could not read constant via enumValue<$T>(). $T is not a Dart enum.');
6160
}
62-
final varMirrors = <VariableMirror>[];
63-
for (final item in classMirror.declarations.values) {
64-
if (item is VariableMirror && item.type == typeMirror) {
65-
varMirrors.add(item);
66-
}
61+
62+
if (!instanceOf(TypeChecker.fromRuntime(T))) {
63+
throw Exception('Not an instance of $T.');
6764
}
65+
6866
// Access enum field 'values'.
6967
final values = classMirror.getField(const Symbol('values')).reflectee;
70-
for (final varMirror in varMirrors) {
71-
final name = MirrorSystem.getName(varMirror.simpleName);
72-
final index = peek(name)?.intValue;
73-
if (index != null) {
74-
return values[index];
75-
}
76-
}
77-
throw Exception('Could not read enum instance of type $T.');
68+
// Get enum field 'index'.
69+
final enumIndex = objectValue.getField('index')!.toIntValue();
70+
71+
return values[enumIndex];
7872
}
7973
}

openapi-generator/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: openapi_generator
22
description: Generator for openapi client sdk inspired by the npm implementation of openapi-generator-cli.
3-
version: 3.3.1
3+
version: 3.3.2
44
homepage: https://github.com/gibahjoe/openapi-generator-dart
55

66
environment:

0 commit comments

Comments
 (0)