Compare commits
4 Commits
main
...
test/dynam
Author | SHA1 | Date |
---|---|---|
Nikolas Rimikis | 77727e7ab0 | 1 year ago |
Nikolas Rimikis | 0ded61d9ce | 1 year ago |
Nikolas Rimikis | 3f95251476 | 1 year ago |
Nikolas Rimikis | ec6e7cafd0 | 1 year ago |
16 changed files with 3406 additions and 113 deletions
@ -0,0 +1,148 @@
|
||||
import 'package:code_builder/code_builder.dart'; |
||||
import 'package:dynamite/src/builder/resolve_type.dart'; |
||||
import 'package:dynamite/src/builder/state.dart'; |
||||
import 'package:dynamite/src/helpers/dart_helpers.dart'; |
||||
import 'package:dynamite/src/helpers/dynamite.dart'; |
||||
import 'package:dynamite/src/models/open_api.dart'; |
||||
import 'package:dynamite/src/models/operation.dart'; |
||||
import 'package:dynamite/src/models/response.dart'; |
||||
import 'package:dynamite/src/type_result/type_result.dart'; |
||||
|
||||
(String? dataType, String? dataValue, bool? dataNeedsAwait) resolveMimeTypeDecode( |
||||
final Response response, |
||||
final OpenAPI spec, |
||||
final State state, |
||||
final String identifier, |
||||
) { |
||||
if (response.content != null) { |
||||
if (response.content!.length > 1) { |
||||
print('Can not work with multiple mime types right now. Using the first supported.'); |
||||
} |
||||
|
||||
for (final content in response.content!.entries) { |
||||
final mimeType = content.key; |
||||
final mediaType = content.value; |
||||
|
||||
final result = resolveType( |
||||
spec, |
||||
state, |
||||
'identifier-$mimeType', |
||||
mediaType.schema!, |
||||
); |
||||
|
||||
if (mimeType == '*/*' || mimeType == 'application/octet-stream' || mimeType.startsWith('image/')) { |
||||
return ('Uint8List', '_response.bodyBytes', true); |
||||
} else if (mimeType.startsWith('text/') || mimeType == 'application/javascript') { |
||||
return ('String', '_response.body', true); |
||||
} else if (mimeType == 'application/json') { |
||||
String? dataValue; |
||||
bool? dataNeedsAwait; |
||||
if (result.name == 'dynamic') { |
||||
dataValue = ''; |
||||
} else if (result.name == 'String') { |
||||
dataValue = '_response.body'; |
||||
dataNeedsAwait = true; |
||||
} else if (result is TypeResultEnum || result is TypeResultBase) { |
||||
dataValue = result.deserialize(result.decode('await _response.body')); |
||||
dataNeedsAwait = false; |
||||
} else { |
||||
dataValue = result.deserialize('await _response.jsonBody'); |
||||
dataNeedsAwait = false; |
||||
} |
||||
return (result.name, dataValue, dataNeedsAwait); |
||||
} |
||||
} |
||||
throw Exception('Can not parse any mime type of Operation:"$identifier"'); |
||||
} |
||||
return (null, null, null); |
||||
} |
||||
|
||||
void resolveMimeTypeEncode( |
||||
final Operation operation, |
||||
final OpenAPI spec, |
||||
final State state, |
||||
final String identifier, |
||||
final MethodBuilder b, |
||||
final StringBuffer code, |
||||
) { |
||||
if (operation.requestBody != null) { |
||||
if (operation.requestBody!.content!.length > 1) { |
||||
print('Can not work with multiple mime types right now. Using the first supported.'); |
||||
} |
||||
for (final content in operation.requestBody!.content!.entries) { |
||||
final mimeType = content.key; |
||||
final mediaType = content.value; |
||||
|
||||
code.write("_headers['Content-Type'] = '$mimeType';"); |
||||
|
||||
final dartParameterNullable = isDartParameterNullable( |
||||
operation.requestBody!.required, |
||||
mediaType.schema, |
||||
); |
||||
|
||||
final result = resolveType( |
||||
spec, |
||||
state, |
||||
toDartName('$identifier-request-$mimeType', uppercaseFirstCharacter: true), |
||||
mediaType.schema!, |
||||
nullable: dartParameterNullable, |
||||
); |
||||
final parameterName = toDartName(result.name.replaceFirst(state.classPrefix, '')); |
||||
switch (mimeType) { |
||||
case 'application/json': |
||||
case 'application/x-www-form-urlencoded': |
||||
final dartParameterRequired = isRequired( |
||||
operation.requestBody!.required, |
||||
mediaType.schema?.$default, |
||||
); |
||||
b.optionalParameters.add( |
||||
Parameter( |
||||
(final b) => b |
||||
..name = parameterName |
||||
..type = refer(result.nullableName) |
||||
..named = true |
||||
..required = dartParameterRequired, |
||||
), |
||||
); |
||||
|
||||
if (dartParameterNullable) { |
||||
code.write('if ($parameterName != null) {'); |
||||
} |
||||
code.write( |
||||
'_body = utf8.encode(${result.encode(parameterName, mimeType: mimeType)}) as Uint8List;', |
||||
); |
||||
if (dartParameterNullable) { |
||||
code.write('}'); |
||||
} |
||||
return; |
||||
case 'application/octet-stream': |
||||
final dartParameterRequired = isRequired( |
||||
operation.requestBody!.required, |
||||
mediaType.schema?.$default, |
||||
); |
||||
b.optionalParameters.add( |
||||
Parameter( |
||||
(final b) => b |
||||
..name = parameterName |
||||
..type = refer(result.nullableName) |
||||
..named = true |
||||
..required = dartParameterRequired, |
||||
), |
||||
); |
||||
|
||||
if (dartParameterNullable) { |
||||
code.write('if ($parameterName != null) {'); |
||||
} |
||||
code.write( |
||||
'_body = ${result.encode(parameterName, mimeType: mimeType)};', |
||||
); |
||||
if (dartParameterNullable) { |
||||
code.write('}'); |
||||
} |
||||
return; |
||||
} |
||||
} |
||||
|
||||
throw Exception('Can not parse any mime type of Operation:"$identifier"'); |
||||
} |
||||
} |
@ -0,0 +1,7 @@
|
||||
# https://dart.dev/guides/libraries/private-files |
||||
# Created by `dart pub` |
||||
.dart_tool/ |
||||
|
||||
# Avoid committing pubspec.lock for library packages; see |
||||
# https://dart.dev/guides/libraries/private-files#pubspeclock. |
||||
pubspec.lock |
@ -0,0 +1,5 @@
|
||||
include: package:neon_lints/dart.yaml |
||||
|
||||
analyzer: |
||||
exclude: |
||||
- '**.g.dart' |
@ -0,0 +1 @@
|
||||
export 'src/petstore.openapi.dart'; |
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -0,0 +1,30 @@
|
||||
name: dynamite_end_to_end_test |
||||
publish_to: none |
||||
description: Tests for dynamite. Not meant for publishing. |
||||
version: 1.0.0 |
||||
|
||||
environment: |
||||
sdk: '>=3.1.1 <4.0.0' |
||||
|
||||
dependencies: |
||||
built_collection: ^5.1.1 |
||||
built_value: ^8.6.2 |
||||
dynamite_runtime: |
||||
git: |
||||
url: https://github.com/nextcloud/neon |
||||
path: packages/dynamite/dynamite_runtime |
||||
universal_io: ^2.2.2 |
||||
|
||||
dev_dependencies: |
||||
build_runner: ^2.4.6 |
||||
built_value_generator: ^8.6.2 |
||||
dynamite: |
||||
git: |
||||
url: https://github.com/nextcloud/neon |
||||
path: packages/dynamite/dynamite |
||||
lints: ^2.0.0 |
||||
neon_lints: |
||||
git: |
||||
url: https://github.com/nextcloud/neon |
||||
path: packages/neon_lints |
||||
test: ^1.21.0 |
@ -0,0 +1,8 @@
|
||||
# melos_managed_dependency_overrides: dynamite,dynamite_runtime,neon_lints |
||||
dependency_overrides: |
||||
dynamite: |
||||
path: ../dynamite |
||||
dynamite_runtime: |
||||
path: ../dynamite_runtime |
||||
neon_lints: |
||||
path: ../../neon_lints |
@ -0,0 +1,15 @@
|
||||
#!/bin/bash |
||||
set -euxo pipefail |
||||
cd "$(dirname "$0")/.." |
||||
|
||||
( |
||||
cd packages/dynamite/dynamite_end_to_end_test |
||||
wget https://petstore3.swagger.io/api/v3/openapi.json -O lib/src/petstore.openapi.json |
||||
rm -rf .dart_tool/build |
||||
fvm dart pub run build_runner build --delete-conflicting-outputs |
||||
# For some reason we need to fix and format twice, otherwise not everything gets fixed |
||||
fvm dart fix --apply lib/src/ |
||||
melos run format |
||||
fvm dart fix --apply lib/src/ |
||||
melos run format |
||||
) |
Loading…
Reference in new issue