Skip to content

Commit

Permalink
Fix web interface for new config option
Browse files Browse the repository at this point in the history
  • Loading branch information
Carapacik committed Dec 6, 2023
1 parent 850739a commit 434ca02
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 63 deletions.
4 changes: 2 additions & 2 deletions swagger_parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 1.13.0
- Added support for (`dart_mappable`)[https://github.com/schultek/dart_mappable] serializer
- Changed `freezed` schema property to `jsonSerializer`, which can be set to `freezed`, `dart_mappable` or `json_serializable` (default).
- Added support for (`dart_mappable`)[https://pub.dev/packages/dart_mappable]
- Changed `freezed` schema property to `json_serializer`, which can be set to `freezed`, `dart_mappable` or `json_serializable` (default).
- Fixed enum generation name that are defined inside an array

## 1.12.2
Expand Down
8 changes: 6 additions & 2 deletions swagger_parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ In your pubspec.yaml, add the following dependencies:

```yaml
dependencies:
# dart_mappable: ^4.0.1 # for dart_mappable
# dio: ^5.3.0
# freezed_annotation: ^2.4.1 # for freezed
# json_annotation: ^4.8.1
# retrofit: ^4.0.3

dev_dependencies:
# build_runner: ^2.4.6
# dart_mappable_builder: ^4.0.1 # for dart_mappable
# freezed: ^2.4.5 # for freezed
# json_serializable: ^6.7.1
# retrofit_generator: ^8.0.4
Expand All @@ -55,6 +57,7 @@ swagger_parser:
# Sets the OpenApi schema path directory for api definition.
schema_path: schemas/openapi.json
# Sets the url of the OpenApi schema.
schema_url: https://petstore.swagger.io/v2/swagger.json
Expand All @@ -69,8 +72,9 @@ swagger_parser:
# Current available options are: path, url.
prefer_schema_source: url
# Optional (dart only). Set 'true' to generate data classes using freezed package.
freezed: false
# Optional (dart only).
# Current available serializers are: 'json_serializable', 'freezed' and 'dart_mappable'.
json_serializer: json_serializable
# Optional (dart only). Set 'true' to generate root client
# with interface and all clients instances.
Expand Down
5 changes: 3 additions & 2 deletions swagger_parser/example/swagger_parser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ swagger_parser:
# Current available options are: path, url.
prefer_schema_source: url

# Optional (dart only). Set 'true' to generate data classes using freezed package.
freezed: false
# Optional (dart only).
# Current available serializers are: 'json_serializable', 'freezed' and 'dart_mappable'.
json_serializer: json_serializable

# Optional (dart only). Set 'true' to generate root client
# with interface and all clients instances.
Expand Down
4 changes: 2 additions & 2 deletions swagger_parser/lib/src/config/yaml_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ final class YamlConfig {
}

JsonSerializer? jsonSerializer;
final rawJsonSerializer = yamlConfig['jsonSerializer']?.toString();
final rawJsonSerializer = yamlConfig['json_serializer']?.toString();
if (rawJsonSerializer != null) {
jsonSerializer = JsonSerializer.fromString(rawJsonSerializer);
if (jsonSerializer == null) {
throw ConfigException(
"'jsonSerializer' field must be contained in ${JsonSerializer.values.map((e) => e.name)}.",
"'json_serializer' field must be contained in ${JsonSerializer.values.map((e) => e.name)}.",
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:collection/collection.dart';

import '../../../swagger_parser.dart';
import '../generator_exception.dart';
import '../templates/dart_dart_mappable_dto_template.dart';
import '../templates/dart_enum_dto_template.dart';
Expand All @@ -14,8 +13,11 @@ import '../templates/kotlin_enum_dto_template.dart';
import '../templates/kotlin_moshi_dto_template.dart';
import '../templates/kotlin_retrofit_client_template.dart';
import '../templates/kotlin_typedef_template.dart';
import 'generated_file.dart';
import 'json_serializer.dart';
import 'open_api_info.dart';
import 'universal_data_class.dart';
import 'universal_rest_client.dart';

/// Enumerates supported programming languages to determine templates
enum ProgrammingLanguage {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:collection/collection.dart';

import '../../../swagger_parser.dart';
import '../../utils/case_utils.dart';
import '../../utils/type_utils.dart';
import '../../utils/utils.dart';
import '../models/json_serializer.dart';
import '../models/programming_language.dart';
import '../models/universal_data_class.dart';
import '../models/universal_type.dart';
import 'dart_import_dto_template.dart';

Expand All @@ -26,7 +27,7 @@ ${indentation(2)}const $className(${getParameters(dataClass)});
${getFields(dataClass)}
${indentation(2)}static $className fromJson(Map<String, dynamic> json) => ${className}Mapper.ensureInitialized().decodeMap<${className}>(json);
${indentation(2)}static $className fromJson(Map<String, dynamic> json) => ${className}Mapper.ensureInitialized().decodeMap<$className>(json);
}
''';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:collection/collection.dart';

import '../../../swagger_parser.dart';
import '../../utils/case_utils.dart';
import '../../utils/type_utils.dart';
import '../../utils/utils.dart';
Expand Down Expand Up @@ -35,7 +34,7 @@ String dartEnumDtoTemplate(
jsonParam: jsonParam,
),
).join(',\n')}${unknownEnumValue ? ',' : ';'}';
final unkownEnumValueStr = unknownEnumValue ? _unkownEnumValue() : '';
final unknownEnumValueStr = unknownEnumValue ? _unkownEnumValue() : '';
final constructorStr = jsonParam ? _constructor(className) : '';
final fromJsonStr = unknownEnumValue ? _fromJson(className, enumClass) : '';
final jsonFieldStr = jsonParam ? _jsonField(enumClass) : '';
Expand All @@ -48,7 +47,7 @@ ${generatedFileComment(
${descriptionComment(enumClass.description)}@JsonEnum()
enum $className {
$values$unkownEnumValueStr$constructorStr$fromJsonStr$jsonFieldStr$toJsonStr
$values$unknownEnumValueStr$constructorStr$fromJsonStr$jsonFieldStr$toJsonStr
}
''';
}
Expand Down
1 change: 1 addition & 0 deletions swagger_parser/lib/swagger_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ library swagger_parser;
export 'src/generator/fill_controller.dart';
export 'src/generator/generator.dart';
export 'src/generator/models/generated_file.dart';
export 'src/generator/models/json_serializer.dart';
export 'src/generator/models/programming_language.dart';
export 'src/generator/models/replacement_rule.dart';
export 'src/generator/models/universal_data_class.dart';
Expand Down
78 changes: 33 additions & 45 deletions swagger_parser_pages/lib/content/generator_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class _GeneratorContentState extends State<GeneratorContent> {
late final _name = TextEditingController();
late final _rootClientName = TextEditingController();
ProgrammingLanguage _language = ProgrammingLanguage.dart;
JsonSerializer _jsonSerializer = JsonSerializer.jsonSerializable;
bool _isYaml = false;
bool _freezed = false;
bool _rootClient = true;
bool _putClientsInFolder = false;
bool _putInFolder = false;
Expand Down Expand Up @@ -44,8 +44,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: ConstrainedBox(
constraints:
const BoxConstraints(maxWidth: 400, maxHeight: 500),
constraints: const BoxConstraints(maxWidth: 400, maxHeight: 500),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
Expand All @@ -72,8 +71,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
children: [
const Text(
'Config parameters',
style:
TextStyle(fontSize: 32, fontWeight: FontWeight.w800),
style: TextStyle(fontSize: 32, fontWeight: FontWeight.w800),
),
const SizedBox(height: 24),
Center(
Expand All @@ -92,21 +90,22 @@ class _GeneratorContentState extends State<GeneratorContent> {
const SizedBox(height: 16),
AnimatedCrossFade(
duration: const Duration(milliseconds: 600),
crossFadeState: _language == ProgrammingLanguage.dart
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
crossFadeState:
_language == ProgrammingLanguage.dart ? CrossFadeState.showSecond : CrossFadeState.showFirst,
sizeCurve: Curves.fastOutSlowIn,
// for correct animation
firstChild: Container(),
secondChild: StatefulBuilder(
builder: (context, setState) => CheckboxListTile(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
title: const Text('Use freezed'),
value: _freezed,
onChanged: (value) =>
setState(() => _freezed = value!),
secondChild: Center(
child: DropdownMenu<JsonSerializer>(
label: const Text('Json serializer'),
width: 300,
initialSelection: JsonSerializer.jsonSerializable,
dropdownMenuEntries: JsonSerializer.values
.map(
(e) => DropdownMenuEntry(value: e, label: e.name),
)
.toList(growable: false),
onSelected: (js) => setState(() => _jsonSerializer = js!),
),
),
),
Expand All @@ -133,9 +132,8 @@ class _GeneratorContentState extends State<GeneratorContent> {
),
AnimatedCrossFade(
duration: const Duration(milliseconds: 600),
crossFadeState: _language == ProgrammingLanguage.dart
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
crossFadeState:
_language == ProgrammingLanguage.dart ? CrossFadeState.showSecond : CrossFadeState.showFirst,
sizeCurve: Curves.fastOutSlowIn,
firstChild: Container(),
secondChild: TextField(
Expand All @@ -152,9 +150,8 @@ class _GeneratorContentState extends State<GeneratorContent> {
),
AnimatedCrossFade(
duration: const Duration(milliseconds: 600),
crossFadeState: _language == ProgrammingLanguage.dart
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
crossFadeState:
_language == ProgrammingLanguage.dart ? CrossFadeState.showSecond : CrossFadeState.showFirst,
sizeCurve: Curves.fastOutSlowIn,
firstChild: Container(),
secondChild: StatefulBuilder(
Expand All @@ -166,8 +163,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
'Generate root client for REST clients',
),
value: _rootClient,
onChanged: (value) =>
setState(() => _rootClient = value!),
onChanged: (value) => setState(() => _rootClient = value!),
),
),
),
Expand All @@ -189,8 +185,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
),
title: const Text('Put the all api in its folder'),
value: _putInFolder,
onChanged: (value) =>
setState(() => _putInFolder = value!),
onChanged: (value) => setState(() => _putInFolder = value!),
),
),
StatefulBuilder(
Expand All @@ -200,8 +195,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
),
title: const Text('Put all clients in clients folder'),
value: _putClientsInFolder,
onChanged: (value) =>
setState(() => _putClientsInFolder = value!),
onChanged: (value) => setState(() => _putClientsInFolder = value!),
),
),
StatefulBuilder(
Expand All @@ -211,8 +205,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
),
title: const Text('Squash all clients in one client'),
value: _squashClients,
onChanged: (value) =>
setState(() => _squashClients = value!),
onChanged: (value) => setState(() => _squashClients = value!),
),
),
StatefulBuilder(
Expand All @@ -222,8 +215,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
),
title: const Text('Generate method name from url path'),
value: _pathMethodName,
onChanged: (value) =>
setState(() => _pathMethodName = value!),
onChanged: (value) => setState(() => _pathMethodName = value!),
),
),
StatefulBuilder(
Expand All @@ -233,15 +225,13 @@ class _GeneratorContentState extends State<GeneratorContent> {
),
title: const Text('Mark files as generated'),
value: _markFilesAsGenerated,
onChanged: (value) =>
setState(() => _markFilesAsGenerated = value!),
onChanged: (value) => setState(() => _markFilesAsGenerated = value!),
),
),
AnimatedCrossFade(
duration: const Duration(milliseconds: 600),
crossFadeState: _language == ProgrammingLanguage.dart
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
crossFadeState:
_language == ProgrammingLanguage.dart ? CrossFadeState.showSecond : CrossFadeState.showFirst,
sizeCurve: Curves.fastOutSlowIn,
// for correct animation
firstChild: Container(),
Expand All @@ -252,8 +242,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
),
title: const Text('Generate to json in Enum'),
value: _enumsToJson,
onChanged: (value) =>
setState(() => _enumsToJson = value!),
onChanged: (value) => setState(() => _enumsToJson = value!),
),
),
),
Expand All @@ -266,8 +255,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
'Generate enum name with prefix from parent component',
),
value: _enumsPrefix,
onChanged: (value) =>
setState(() => _enumsPrefix = value!),
onChanged: (value) => setState(() => _enumsPrefix = value!),
),
),
const SizedBox(height: 32),
Expand All @@ -285,7 +273,7 @@ class _GeneratorContentState extends State<GeneratorContent> {
clientPostfix: _clientPostfix.text,
language: _language,
isYaml: _isYaml,
freezed: _freezed,
jsonSerializer: _jsonSerializer,
rootClient: _rootClient,
rootClientName: _rootClientName.text,
name: _name.text,
Expand Down Expand Up @@ -314,7 +302,7 @@ Future<void> _generateOutputs(
required String schema,
required String clientPostfix,
required ProgrammingLanguage language,
required bool freezed,
required JsonSerializer jsonSerializer,
required String name,
required bool putInFolder,
required bool putClientsInFolder,
Expand All @@ -333,7 +321,7 @@ Future<void> _generateOutputs(
isYaml: isYaml,
language: language,
outputDirectory: '',
freezed: freezed,
jsonSerializer: jsonSerializer,
rootClient: rootClient,
rootClientName: rootClientName.trim().isEmpty ? null : rootClientName,
clientPostfix: clientPostfix.trim().isEmpty ? null : clientPostfix,
Expand Down
8 changes: 4 additions & 4 deletions swagger_parser_pages/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ description: Web interface for swagger_parser
publish_to: none
version: 1.0.0+1
environment:
sdk: ^3.1.0
sdk: ^3.2.0

dependencies:
archive: ^3.4.6
archive: ^3.4.9
flutter:
sdk: flutter
flutter_web_plugins:
sdk: flutter
swagger_parser:
path:
../swagger_parser
url_launcher: ^6.1.14
url_launcher: ^6.2.1

dev_dependencies:
carapacik_lints: ^1.5.1
carapacik_lints: ^1.6.0

flutter:
uses-material-design: true

0 comments on commit 434ca02

Please sign in to comment.