Skip to content

Commit

Permalink
Fixed cases with application/json (#237)
Browse files Browse the repository at this point in the history
* Fixed cases with application/json

* Fixed some things

* Small fix

* Fixed summary field

* Fixed some things

* Removed not needed thing

Co-authored-by: uladzimir_paliukhovich <>
  • Loading branch information
Vovanella95 authored Sep 3, 2021
1 parent f56bb7a commit 63cdc32
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.1.1+2

* Fixed issues when content has no application/json child

# 2.1.1+1

* Fixed Issue ([#235](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/235))
Expand Down
3 changes: 2 additions & 1 deletion lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ abstract class SwaggerModelsGenerator {
paths.forEach((path, pathValue) {
final requests = pathValue as Map<String, dynamic>;

requests.removeWhere((key, value) => key == 'parameters');
requests
.removeWhere((key, value) => key == 'parameters' || key == 'summary');

requests.forEach((request, requestValue) {
if (options.excludePaths.isNotEmpty &&
Expand Down
56 changes: 50 additions & 6 deletions lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,18 @@ class SwaggerRequestsGenerator {
return result.distinctParameters();
}

bool _isBasicTypeRef(String ref, SwaggerRoot root) {
final schemas = _getAllReusableObjects(root);

final neededSchema = schemas[ref.getUnformattedRef()];

if (neededSchema == null) {
return false;
}

return kBasicTypes.contains(neededSchema.type);
}

bool _isEnumRef(String ref, SwaggerRoot root) {
final schemas = root.components?.schemas ?? <String, SwaggerSchema>{};
schemas.addAll(root.definitions);
Expand Down Expand Up @@ -507,6 +519,10 @@ class SwaggerRequestsGenerator {
return ref.asEnum().asList();
}

if (_isBasicTypeRef(ref, root)) {
return kObject.pascalCase;
}

if (ref.isNotEmpty) {
return SwaggerModelsGenerator.getValidatedClassName(
ref.withPostfix(modelPostfix))
Expand All @@ -524,6 +540,10 @@ class SwaggerRequestsGenerator {
return schema.ref.getRef().asEnum();
}

if (_isBasicTypeRef(schema.ref, root)) {
return kObject.pascalCase;
}

return SwaggerModelsGenerator.getValidatedClassName(
schema.ref.getRef().withPostfix(modelPostfix));
}
Expand Down Expand Up @@ -610,10 +630,14 @@ class SwaggerRequestsGenerator {
final ref = swaggerResponse.schema?.ref ?? swaggerResponse.ref;

if (ref.isNotEmpty) {
final responses = root.components?.responses ?? {};
final neededResponse = responses[ref.getRef()];
final allReusableObjects = _getAllReusableObjects(root);
final neededResponse = allReusableObjects[ref.getUnformattedRef()];

if (neededResponse?.ref.isNotEmpty == true) {
if (neededResponse == null) {
return kObject.pascalCase;
}

if (neededResponse.ref.isNotEmpty) {
return kObject.pascalCase;
}

Expand All @@ -623,6 +647,16 @@ class SwaggerRequestsGenerator {
return null;
}

Map<String, SwaggerSchema> _getAllReusableObjects(SwaggerRoot root) {
final results = <String, SwaggerSchema>{};
results.addAll(root.definitions);
results.addAll(root.components?.schemas ?? {});
results.addAll(root.components?.responses ?? {});
results.addAll(root.components?.requestBodies ?? {});

return results;
}

String? _getReturnTypeFromOriginalRef(
SwaggerResponse swaggerResponse, String modelPostfix) {
if (swaggerResponse.schema?.originalRef.isNotEmpty == true) {
Expand Down Expand Up @@ -651,13 +685,23 @@ class SwaggerRequestsGenerator {

final schemaRef = content.schema?.ref ?? '';
if (schemaRef.isNotEmpty) {
final neededSchema = swaggerRoot.components?.schemas[schemaRef.getRef()];
final allRefs = _getAllReusableObjects(swaggerRoot);
final neededSchema = allRefs[schemaRef.getUnformattedRef()];

if (neededSchema == null) {
return kObject.pascalCase;
}

if (kBasicTypes.contains(neededSchema.type)) {
return kObject.pascalCase;
}

final typeName =
SwaggerModelsGenerator.getValidatedClassName(schemaRef.getRef())
.withPostfix(modelPostfix);

if (neededSchema?.type == kArray) {
return neededSchema?.items?.ref
if (neededSchema.type == kArray) {
return neededSchema.items?.ref
.getRef()
.withPostfix(modelPostfix)
.asList();
Expand Down
2 changes: 2 additions & 0 deletions lib/src/extensions/string_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extension TypeExtension on String {

String getRef() => split('/').last.pascalCase;

String getUnformattedRef() => split('/').last;

String withPostfix(String postfix) => '${this}$postfix';

String asList() => 'List<$this>';
Expand Down
23 changes: 11 additions & 12 deletions lib/src/swagger_models/swagger_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,17 @@ Map<String, SwaggerSchema> _mapResponses(Map<String, dynamic>? json) {
final content =
(value as Map<String, dynamic>)['content'] as Map<String, dynamic>?;

final appJsonKey = content?.keys.firstWhere(
(element) => element.startsWith('application/json'),
orElse: () => '') ??
'';

if (appJsonKey.isNotEmpty) {
final appJson = content?[appJsonKey] as Map<String, dynamic>?;

if (appJson != null && appJson['schema'] != null) {
results[key] =
SwaggerSchema.fromJson(appJson['schema'] as Map<String, dynamic>);
}
Map<String, dynamic>? appJson;

if (content?.length == 1) {
appJson = content?.values.first as Map<String, dynamic>?;
} else {
appJson = content?['application/json'] as Map<String, dynamic>?;
}

if (appJson != null && appJson['schema'] != null) {
results[key] =
SwaggerSchema.fromJson(appJson['schema'] as Map<String, dynamic>);
}
});

Expand Down
2 changes: 1 addition & 1 deletion lib/src/swagger_models/swagger_root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Map<String, SwaggerPath> _mapPaths(Map<String, dynamic>? paths) {
return paths.map((path, pathValue) {
final value = pathValue as Map<String, dynamic>;
final parameters = value['parameters'] as List<dynamic>?;
value.removeWhere((key, value) => key == 'parameters');
value.removeWhere((key, value) => key == 'parameters' || key == 'summary');

return MapEntry(
path,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: swagger_dart_code_generator

version: 2.1.1+1
version: 2.1.1+2

homepage: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator
repository: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator
Expand Down

0 comments on commit 63cdc32

Please sign in to comment.