From 75a2c76cc7bb662160c2c7dd92792201b63f5722 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Fri, 14 Apr 2023 17:29:45 +0200 Subject: [PATCH] dynamite: Sort required parameters and properties first --- .../dynamite/lib/src/openapi_builder.dart | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 83c7636d..362f85ba 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -642,7 +642,12 @@ class OpenAPIBuilder implements Builder { final parameters = [ ...pathParameters, if (operation.parameters != null) ...operation.parameters!, - ]; + ]..sort( + (final a, final b) => sortRequiredElements( + a.required ?? false, + b.required ?? false, + ), + ); b ..name = _toDartName(_filterMethodName(operationId, tag ?? '')) ..modifier = MethodModifier.async @@ -1234,6 +1239,13 @@ TypeResult resolveObject( state.output.add( Class( (final b) { + final sortedParameterKeys = schema.properties!.keys.toList() + ..sort( + (final a, final b) => sortRequiredElements( + (schema.required ?? []).contains(a), + (schema.required ?? []).contains(b), + ), + ); b ..name = '${state.prefix}$identifier' ..docs.addAll(_descriptionToDocs(schema.description)) @@ -1257,7 +1269,7 @@ TypeResult resolveObject( Constructor( (final b) => b ..optionalParameters.addAll( - schema.properties!.keys.map( + sortedParameterKeys.map( (final propertyName) => Parameter( (final b) { final propertySchema = schema.properties![propertyName]!; @@ -1865,3 +1877,16 @@ TypeResult resolveType( throw Exception('Can not convert OpenAPI type "${schema.toJson()}" to a Dart type'); } + +// ignore: avoid_positional_boolean_parameters +int sortRequiredElements(final bool a, final bool b) { + if (a != b) { + if (a && !b) { + return -1; + } else { + return 1; + } + } + + return 0; +}