Skip to content
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/map additional properties ref #153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions swagger_parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.14.1
- Removed check that would avoid generating a map when additional properties has a `$ref` value

## 1.14.0
- Fixed error with empty content type
- Fixed retrofit template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ final class UniversalType {

@override
String toString() =>
'UniversalType(\ntype: $type,\nname: $name,\ndescription: $description,\nformat: $format,\njsonKey: $jsonKey,\ndefaultValue: $defaultValue,\nisRequired: $isRequired,\nenumType: $enumType,\narrayDepth: $arrayDepth,\nnullable: $nullable\n)';
'UniversalType(\ntype: $type,\nname: $name,\ndescription: $description,\nformat: $format,\njsonKey: $jsonKey,\ndefaultValue: $defaultValue,\nisRequired: $isRequired,\nenumType: $enumType,\narrayDepth: $arrayDepth,\nnullable: $nullable\n, mapType: $mapType\n)';
}

/// Converts [UniversalType] to type from specified language
Expand Down
4 changes: 1 addition & 3 deletions swagger_parser/lib/src/parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1075,9 +1075,7 @@ class OpenApiParser {
// To detect is this entity is map or not
final mapType = map[_typeConst].toString() == _objectConst &&
map.containsKey(_additionalPropertiesConst) &&
(map[_additionalPropertiesConst] is Map<String, dynamic>) &&
!(map[_additionalPropertiesConst] as Map<String, dynamic>)
.containsKey(_refConst)
(map[_additionalPropertiesConst] is Map<String, dynamic>)
? 'string'
: null;
final defaultValue = map[_defaultConst]?.toString();
Expand Down
2 changes: 1 addition & 1 deletion swagger_parser/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: swagger_parser
description: Package that generates REST clients and data classes from OpenApi definition file
version: 1.14.0
version: 1.14.1
repository: https://github.com/Carapacik/swagger_parser/tree/main/swagger_parser
homepage: https://omega-r.com
topics:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ void main() {
expect(item2, expectedItem2);
});

test('additionalProperties entity that should not parse to object test',
test('additionalProperties entity that should not parse to object test 3.0',
() async {
final schemaPath = p.join(
'test',
Expand Down Expand Up @@ -487,6 +487,7 @@ void main() {
description: 'data',
jsonKey: 'data',
isRequired: false,
mapType: 'string',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write a separate test for this if it's not difficult

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added my json sample and wrote a test for swagger v2 additional properties

),
],
),
Expand All @@ -507,6 +508,60 @@ void main() {
expect(item2, expectedItem2);
});

test('additionalProperties entity should parse to object test 2.0',
() async {
final schemaPath = p.join(
'test',
'parser',
'schemas',
'additional_properties_class.2.0.json',
);
final configFile = schemaFile(schemaPath);
final schemaContent = configFile!.readAsStringSync();
final parser = OpenApiParser(schemaContent);
final dataClasses = parser.parseDataClasses().toList();
final expectedDataClasses = <UniversalDataClass>[
const UniversalComponentClass(
name: 'ValueClass',
imports: {},
parameters: [
UniversalType(
type: 'string',
name: 'testProp',
jsonKey: 'testProp',
description: 'A test property',
),
],
),
const UniversalComponentClass(
name: 'WrapperClass',
imports: { 'ValueClass' },
parameters: [
UniversalType(
type: 'ValueClass',
name: 'map',
jsonKey: 'map',
mapType: 'string',
),
],
),
];

expect(dataClasses.length, expectedDataClasses.length);
final item1 = dataClasses[0] as UniversalComponentClass;
final item2 = dataClasses[1] as UniversalComponentClass;
final expectedItem1 = expectedDataClasses[0] as UniversalComponentClass;
final expectedItem2 = expectedDataClasses[1] as UniversalComponentClass;
expect(item1.parameters.length, expectedItem1.parameters.length);
for (var i = 0; i < item1.parameters.length; i++) {
expect(
item1.parameters[i],
expectedItem1.parameters[i],
);
}
expect(item2, expectedItem2);
});

test('Enum name test', () async {
final schemaPath = p.join('test', 'parser', 'schemas', 'enum_class.json');
final configFile = schemaFile(schemaPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"swagger": "2.0",
"info": {
"description": "API",
"version": "1.0",
"title": "REST API"
},
"host": "localhost:8081",
"basePath": "/",
"tags": [
{
"name": "Test",
"description": "Test"
}
],
"paths": {
"/test": {
"get": {
"tags": [
"Test"
],
"operationId": "testMethod",
"produces": [
"application/json"
],
"parameters": [
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/WrapperClass"
}
},
"400": {
"description": "Invalid request parameters"
},
"500": {
"description": "Server error"
}
},
"deprecated": false
}
}
},
"definitions": {
"ValueClass": {
"type": "object",
"required": [
"testProp"
],
"properties": {
"testProp": {
"type": "string",
"description": "A test property"
}
},
"title": "ValueClass"
},
"WrapperClass": {
"type": "object",
"required": [
"map"
],
"properties": {
"map": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/ValueClass"
}
}
},
"title": "WrapperClass"
}
}
}
Loading