From 24b173679d5fb60129c6346c96fa7e0cb32130fe Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Sat, 2 Sep 2023 20:32:45 +0200 Subject: [PATCH] refactor(dynamite): cleanup required checks Signed-off-by: Nikolas Rimikis --- .../dynamite/lib/src/openapi_builder.dart | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index 2630ccdb..87a33f1f 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/dynamite/lib/src/openapi_builder.dart @@ -320,18 +320,7 @@ class OpenAPIBuilder implements Builder { final parameters = [ ...?pathEntry.value.parameters, ...?operation.parameters, - ]..sort( - (final a, final b) => sortRequiredElements( - _isDartParameterRequired( - a.required, - a.schema?.default_, - ), - _isDartParameterRequired( - b.required, - b.schema?.default_, - ), - ), - ); + ]..sort(sortRequiredParameters); b ..name = _toDartName(_filterMethodName(operationId, tag ?? '')) ..modifier = MethodModifier.async @@ -390,10 +379,7 @@ class OpenAPIBuilder implements Builder { parameter.schema?.nullable, parameter.schema?.default_, ); - final dartParameterRequired = _isDartParameterRequired( - parameter.required, - parameter.schema?.default_, - ); + final dartParameterRequired = _isDartParameterRequired(parameter); final result = resolveType( spec, @@ -514,7 +500,7 @@ class OpenAPIBuilder implements Builder { switch (mimeType) { case 'application/json': case 'application/x-www-form-urlencoded': - final dartParameterRequired = _isDartParameterRequired( + final dartParameterRequired = _isRequired( operation.requestBody!.required, mediaType.schema?.default_, ); @@ -848,7 +834,10 @@ bool _isDartParameterNullable( ) => (!(required ?? false) && default_ == null) || (nullable ?? false); -bool _isDartParameterRequired( +bool _isDartParameterRequired(final spec_parameter.Parameter parameter) => + _isRequired(parameter.required, parameter.schema?.default_); + +bool _isRequired( final bool? required, final dynamic default_, ) => @@ -1738,9 +1727,12 @@ TypeResult resolveType( } // ignore: avoid_positional_boolean_parameters -int sortRequiredElements(final bool a, final bool b) { - if (a != b) { - if (a && !b) { +int sortRequiredParameters(final spec_parameter.Parameter a, final spec_parameter.Parameter b) { + final aRequired = _isDartParameterRequired(a); + final bRequired = _isDartParameterRequired(b); + + if (aRequired != bRequired) { + if (aRequired && !bRequired) { return -1; } else { return 1;