Skip to content

Commit

Permalink
Merge pull request #111 from gibahjoe/fix-issue-#110
Browse files Browse the repository at this point in the history
fix: useEnumExtension in additional properties throws invalid type error
  • Loading branch information
gibahjoe authored Sep 4, 2023
2 parents 45af1cf + 53b711a commit 6a1befa
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,7 @@ class DioProperties extends AdditionalProperties {

DioProperties.fromMap(Map<String, dynamic> map)
: dateLibrary = EnumTransformer.dioDateLibrary(map['dateLibrary']),
nullableFields = map['nullableFields'] != null
? map['nullableFields'] == 'true'
: null,
nullableFields = map['nullableFields'],
serializationLibrary = EnumTransformer.dioSerializationLibrary(
map['serializationLibrary']),
super.fromMap(map);
Expand Down Expand Up @@ -621,12 +619,9 @@ class DioAltProperties extends AdditionalProperties {
wrapper: wrapper);

DioAltProperties.fromMap(Map<String, dynamic> map)
: nullSafe = map['nullSafe'] != null ? map['nullSafe'] == 'true' : null,
nullSafeArrayDefault = map['nullSafeArrayDefault'] != null
? map['nullSafeArrayDefault'] == 'true'
: null,
listAnyOf =
map['listAnyOf'] != null ? map['listAnyOf'] == 'true' : null,
: nullSafe = map['nullSafe'],
nullSafeArrayDefault = map['nullSafeArrayDefault'],
listAnyOf = map['listAnyOf'],
pubspecDependencies = map['pubspecDependencies'],
pubspecDevDependencies = map['pubspecDevDependencies'],
super.fromMap(map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void main() {
'sortParamsByRequiredFlag': props.sortParamsByRequiredFlag,
'sourceFolder': props.sourceFolder,
'wrapper': 'none',
'nullableFields': '${props.nullableFields}',
'nullableFields': props.nullableFields,
'dateLibrary': 'core',
'serializationLibrary': 'json_serializable',
};
Expand Down Expand Up @@ -289,9 +289,9 @@ void main() {
'sortParamsByRequiredFlag': props.sortParamsByRequiredFlag,
'sourceFolder': props.sourceFolder,
'wrapper': 'none',
'nullSafe': '${props.nullSafe}',
'nullSafeArrayDefault': '${props.nullSafeArrayDefault}',
'listAnyOf': '${props.listAnyOf}',
'nullSafe': props.nullSafe,
'nullSafeArrayDefault': props.nullSafeArrayDefault,
'listAnyOf': props.listAnyOf,
'pubspecDevDependencies': props.pubspecDevDependencies,
'pubspecDependencies': props.pubspecDependencies,
};
Expand Down
5 changes: 3 additions & 2 deletions openapi-generator/lib/src/extensions/type_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ extension ReadProperty on ConstantReader {
) as T;
}

if (isA(v, Map)) {
if (isA(v, Map<String, String>)) {
return v.mapValue.map((key, value) => MapEntry(
convertToPropertyValue(key!), convertToPropertyValue(value!))) as T;
convertToPropertyValue(key!) as String,
convertToPropertyValue(value!) as String)) as T;
} else if (isA(v, bool)) {
return v.boolValue as T;
} else if (isA(v, double)) {
Expand Down
6 changes: 3 additions & 3 deletions openapi-generator/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ String getMapAsString(Map<dynamic, dynamic> data) {
}

/// Converts a [DartObject] to it's given type.
String convertToPropertyValue(DartObject value) {
dynamic convertToPropertyValue(DartObject value) {
if (value.isNull) {
return '';
}
return value.toStringValue() ??
value.toBoolValue()?.toString() ??
value.toIntValue()?.toString() ??
value.toBoolValue() ??
value.toIntValue() ??
value.getField('_name')?.toStringValue() ??
'';
}
Expand Down
4 changes: 4 additions & 0 deletions openapi-generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ dev_dependencies:
source_gen_test:
pedantic:
coverage: ^1.6.3

dependency_overrides:
openapi_generator_annotations:
path: ../openapi-generator-annotations
193 changes: 149 additions & 44 deletions openapi-generator/test/generator_arguments_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,50 +153,155 @@ void main() {
),
);
});
test('uses config', () async {
final config = File(
'${Directory.current.path}${Platform.pathSeparator}test${Platform.pathSeparator}specs${Platform.pathSeparator}test_config.dart')
.readAsStringSync();
final annotations = (await resolveSource(
config,
(resolver) async =>
(await resolver.findLibraryByName('test_lib'))!))
.getClass('TestClassConfig')!
.metadata
.map((e) => src_gen.ConstantReader(e.computeConstantValue()!))
.first;
final args = GeneratorArguments(annotations: annotations);
expect(args.alwaysRun, isTrue);
expect(args.useNextGen, isTrue);
expect(args.cachePath, './test/specs/output/cache.json');
expect(args.outputDirectory, './test/specs/output');
expect(args.runSourceGen, isTrue);
expect(args.shouldFetchDependencies, isTrue);
expect(args.skipValidation, isFalse);
expect(await args.inputFileOrFetch, './test/specs/openapi.test.yaml');
expect(args.templateDirectory, 'template');
expect(args.generator, Generator.dio);
expect(args.wrapper, Wrapper.fvm);
expect(args.importMappings, {'package': 'test'});
expect(args.typeMappings, {'key': 'value'});
expect(args.reservedWordsMappings, {'const': 'final'});
expect(args.inlineSchemaNameMappings, {'200resp': 'OkResp'});
expect(args.pubspecPath, './test/specs/dart_pubspec.test.yaml');
expect(args.isRemote, isFalse);
expect(args.generatorName, 'dart-dio');
expect(args.shouldGenerateSources, isTrue);
expect(await args.jarArgs, [
'generate',
'-o=${args.outputDirectory}',
'-i=${await args.inputFileOrFetch}',
'-t=${args.templateDirectory}',
'-g=${args.generatorName}',
'--reserved-words-mappings=${args.reservedWordsMappings.entries.fold('', foldStringMap())}',
'--inline-schema-name-mappings=${args.inlineSchemaNameMappings.entries.fold('', foldStringMap())}',
'--import-mappings=${args.importMappings.entries.fold('', foldStringMap())}',
'--type-mappings=${args.typeMappings.entries.fold('', foldStringMap())}',
'--additional-properties=${args.additionalProperties!.toMap().entries.fold('', foldStringMap(keyModifier: convertToPropertyKey))}'
]);
group('annotation specification', () {
// https://github.com/gibahjoe/openapi-generator-dart/issues/110
test('Processes annotations correctly', () async {
final config = File(
'${Directory.current.path}${Platform.pathSeparator}test${Platform.pathSeparator}specs${Platform.pathSeparator}test_config.dart')
.readAsStringSync();
final annotations = (await resolveSource(
config,
(resolver) async =>
(await resolver.findLibraryByName('test_lib'))!))
.getClass('TestClassConfig')!
.metadata
.map((e) => src_gen.ConstantReader(e.computeConstantValue()!))
.first;
final args = GeneratorArguments(annotations: annotations);
expect(args.alwaysRun, isTrue);
expect(args.useNextGen, isTrue);
expect(args.cachePath, './test/specs/output/cache.json');
expect(args.outputDirectory, './test/specs/output');
expect(args.runSourceGen, isTrue);
expect(args.shouldFetchDependencies, isTrue);
expect(args.skipValidation, isFalse);
expect(await args.inputFileOrFetch, './test/specs/openapi.test.yaml');
expect(args.templateDirectory, 'template');
expect(args.generator, Generator.dio);
expect(args.wrapper, Wrapper.fvm);
expect(args.importMappings, {'package': 'test'});
expect(args.typeMappings, {'key': 'value'});
expect(args.reservedWordsMappings, {'const': 'final'});
expect(args.inlineSchemaNameMappings, {'200resp': 'OkResp'});
expect(args.pubspecPath, './test/specs/dart_pubspec.test.yaml');
expect(args.isRemote, isFalse);
expect(args.generatorName, 'dart-dio');
expect(args.shouldGenerateSources, isTrue);
expect(args.additionalProperties?.useEnumExtension, isTrue);
expect(args.additionalProperties?.pubAuthor, 'test author');
expect(await args.jarArgs, [
'generate',
'-o=${args.outputDirectory}',
'-i=${await args.inputFileOrFetch}',
'-t=${args.templateDirectory}',
'-g=${args.generatorName}',
'--reserved-words-mappings=${args.reservedWordsMappings.entries.fold('', foldStringMap())}',
'--inline-schema-name-mappings=${args.inlineSchemaNameMappings.entries.fold('', foldStringMap())}',
'--import-mappings=${args.importMappings.entries.fold('', foldStringMap())}',
'--type-mappings=${args.typeMappings.entries.fold('', foldStringMap())}',
'--additional-properties=${args.additionalProperties!.toMap().entries.fold('', foldStringMap(keyModifier: convertToPropertyKey))}'
]);
});
test('Processes annotation with DioProperties correctly', () async {
final config = File(
'${Directory.current.path}${Platform.pathSeparator}test${Platform.pathSeparator}specs${Platform.pathSeparator}dio_properties_test_config.dart')
.readAsStringSync();
final annotations = (await resolveSource(
config,
(resolver) async =>
(await resolver.findLibraryByName('test_lib'))!))
.getClass('DioPropertiesTestConfig')!
.metadata
.map((e) => src_gen.ConstantReader(e.computeConstantValue()!))
.first;
final args = GeneratorArguments(annotations: annotations);
expect(args.alwaysRun, isTrue);
expect(args.useNextGen, isTrue);
expect(args.cachePath, './test/specs/output/cache.json');
expect(args.outputDirectory, './test/specs/output');
expect(args.runSourceGen, isTrue);
expect(args.shouldFetchDependencies, isTrue);
expect(args.skipValidation, isFalse);
expect(await args.inputFileOrFetch, './test/specs/openapi.test.yaml');
expect(args.templateDirectory, 'template');
expect(args.generator, Generator.dio);
expect(args.wrapper, Wrapper.fvm);
expect(args.importMappings, {'package': 'test'});
expect(args.typeMappings, {'key': 'value'});
expect(args.reservedWordsMappings, {'const': 'final'});
expect(args.inlineSchemaNameMappings, {'200resp': 'OkResp'});
expect(args.pubspecPath, './test/specs/dart_pubspec.test.yaml');
expect(args.isRemote, isFalse);
expect(args.generatorName, 'dart-dio');
expect(args.shouldGenerateSources, isTrue);
expect(args.additionalProperties?.useEnumExtension, isTrue);
expect((args.additionalProperties as DioProperties?)?.nullableFields,
isTrue);
expect(await args.jarArgs, [
'generate',
'-o=${args.outputDirectory}',
'-i=${await args.inputFileOrFetch}',
'-t=${args.templateDirectory}',
'-g=${args.generatorName}',
'--reserved-words-mappings=${args.reservedWordsMappings.entries.fold('', foldStringMap())}',
'--inline-schema-name-mappings=${args.inlineSchemaNameMappings.entries.fold('', foldStringMap())}',
'--import-mappings=${args.importMappings.entries.fold('', foldStringMap())}',
'--type-mappings=${args.typeMappings.entries.fold('', foldStringMap())}',
'--additional-properties=${args.additionalProperties!.toMap().entries.fold('', foldStringMap(keyModifier: convertToPropertyKey))}'
]);
});
test('Processes annotation with DioAltProperties correctly', () async {
final config = File(
'${Directory.current.path}${Platform.pathSeparator}test${Platform.pathSeparator}specs${Platform.pathSeparator}dio_alt_properties_test_config.dart')
.readAsStringSync();
final annotations = (await resolveSource(
config,
(resolver) async =>
(await resolver.findLibraryByName('test_lib'))!))
.getClass('DioAltPropertiesTestConfig')!
.metadata
.map((e) => src_gen.ConstantReader(e.computeConstantValue()!))
.first;
final args = GeneratorArguments(annotations: annotations);
expect(args.alwaysRun, isTrue);
expect(args.useNextGen, isTrue);
expect(args.cachePath, './test/specs/output/cache.json');
expect(args.outputDirectory, './test/specs/output');
expect(args.runSourceGen, isTrue);
expect(args.shouldFetchDependencies, isTrue);
expect(args.skipValidation, isFalse);
expect(await args.inputFileOrFetch, './test/specs/openapi.test.yaml');
expect(args.templateDirectory, 'template');
expect(args.generator, Generator.dio);
expect(args.wrapper, Wrapper.fvm);
expect(args.importMappings, {'package': 'test'});
expect(args.typeMappings, {'key': 'value'});
expect(args.reservedWordsMappings, {'const': 'final'});
expect(args.inlineSchemaNameMappings, {'200resp': 'OkResp'});
expect(args.pubspecPath, './test/specs/dart_pubspec.test.yaml');
expect(args.isRemote, isFalse);
expect(args.generatorName, 'dart-dio');
expect(args.shouldGenerateSources, isTrue);
expect(args.additionalProperties?.useEnumExtension, isTrue);
expect(
(args.additionalProperties as DioAltProperties?)?.nullSafe, isTrue);
expect(
(args.additionalProperties as DioAltProperties?)
?.nullSafeArrayDefault,
isTrue);
expect(await args.jarArgs, [
'generate',
'-o=${args.outputDirectory}',
'-i=${await args.inputFileOrFetch}',
'-t=${args.templateDirectory}',
'-g=${args.generatorName}',
'--reserved-words-mappings=${args.reservedWordsMappings.entries.fold('', foldStringMap())}',
'--inline-schema-name-mappings=${args.inlineSchemaNameMappings.entries.fold('', foldStringMap())}',
'--import-mappings=${args.importMappings.entries.fold('', foldStringMap())}',
'--type-mappings=${args.typeMappings.entries.fold('', foldStringMap())}',
'--additional-properties=${args.additionalProperties!.toMap().entries.fold('', foldStringMap(keyModifier: convertToPropertyKey))}'
]);
});
});
});
}
30 changes: 30 additions & 0 deletions openapi-generator/test/specs/dio_alt_properties_test_config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
library test_lib;

import 'package:openapi_generator_annotations/openapi_generator_annotations.dart';

@Openapi(
inputSpecFile: './openapi.test.yaml',
inputSpec: InputSpec(path: './test/specs/openapi.test.yaml'),
generatorName: Generator.dio,
useNextGen: true,
cachePath: './test/specs/output/cache.json',
typeMappings: {'key': 'value'},
templateDirectory: 'template',
alwaysRun: true,
outputDirectory: './test/specs/output',
runSourceGenOnOutput: true,
apiPackage: 'test',
skipSpecValidation: false,
importMappings: {'package': 'test'},
reservedWordsMappings: {'const': 'final'},
additionalProperties: DioAltProperties(
wrapper: Wrapper.fvm,
useEnumExtension: true,
pubAuthor: 'test author',
nullSafe: true,
nullSafeArrayDefault: true),
inlineSchemaNameMappings: {'200resp': 'OkResp'},
overwriteExistingFiles: true,
projectPubspecPath: './test/specs/dart_pubspec.test.yaml',
)
class DioAltPropertiesTestConfig {}
29 changes: 29 additions & 0 deletions openapi-generator/test/specs/dio_properties_test_config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
library test_lib;

import 'package:openapi_generator_annotations/openapi_generator_annotations.dart';

@Openapi(
inputSpecFile: './openapi.test.yaml',
inputSpec: InputSpec(path: './test/specs/openapi.test.yaml'),
generatorName: Generator.dio,
useNextGen: true,
cachePath: './test/specs/output/cache.json',
typeMappings: {'key': 'value'},
templateDirectory: 'template',
alwaysRun: true,
outputDirectory: './test/specs/output',
runSourceGenOnOutput: true,
apiPackage: 'test',
skipSpecValidation: false,
importMappings: {'package': 'test'},
reservedWordsMappings: {'const': 'final'},
additionalProperties: DioProperties(
wrapper: Wrapper.fvm,
useEnumExtension: true,
pubAuthor: 'test author',
nullableFields: true),
inlineSchemaNameMappings: {'200resp': 'OkResp'},
overwriteExistingFiles: true,
projectPubspecPath: './test/specs/dart_pubspec.test.yaml',
)
class DioPropertiesTestConfig {}
9 changes: 7 additions & 2 deletions openapi-generator/test/specs/test_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart
skipSpecValidation: false,
importMappings: {'package': 'test'},
reservedWordsMappings: {'const': 'final'},
additionalProperties: AdditionalProperties(wrapper: Wrapper.fvm),
additionalProperties: AdditionalProperties(
wrapper: Wrapper.fvm,
useEnumExtension: true,
pubAuthor: 'test author',
sortModelPropertiesByRequiredFlag: true,
legacyDiscriminatorBehavior: true),
inlineSchemaNameMappings: {'200resp': 'OkResp'},
overwriteExistingFiles: true,
projectPubspecPath: './test/specs/dart_pubspec.test.yaml',
)
class TestClassConfig extends OpenapiGeneratorConfig {}
class TestClassConfig {}
4 changes: 1 addition & 3 deletions openapi-generator/test/test_annotations/test_configs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ const useNextGen = false;
@Openapi(
inputSpecFile: '',
generatorName: Generator.dart,
additionalProperties: AdditionalProperties(
wrapper: Wrapper.flutterw,
),
additionalProperties: AdditionalProperties(wrapper: Wrapper.flutterw),
)
class TestClassHasCustomAnnotations extends OpenapiGeneratorConfig {}

Expand Down

0 comments on commit 6a1befa

Please sign in to comment.