From a9a2504d6f29a925ec4dd40e3d8ee96a4cc5a270 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Wed, 13 Sep 2023 17:37:22 +0200 Subject: [PATCH] feat(dynamite): support yaml specs Signed-off-by: Nikolas Rimikis --- .../dynamite/lib/src/openapi_builder.dart | 17 +++++++++++++---- packages/dynamite/dynamite/pubspec.yaml | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index e32e9c9e..a3af8666 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/dynamite/lib/src/openapi_builder.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:build/build.dart'; import 'package:built_collection/built_collection.dart'; +import 'package:checked_yaml/checked_yaml.dart'; import 'package:code_builder/code_builder.dart'; import 'package:dart_style/dart_style.dart'; import 'package:dynamite/src/builder/client.dart'; @@ -18,6 +19,7 @@ class OpenAPIBuilder implements Builder { @override final buildExtensions = const { '.openapi.json': ['.openapi.dart'], + '.openapi.yaml': ['.openapi.dart'], }; @override @@ -31,10 +33,17 @@ class OpenAPIBuilder implements Builder { useNullSafetySyntax: true, ); - final spec = serializers.deserializeWith( - OpenAPI.serializer, - json.decode(await buildStep.readAsString(inputId)), - )!; + final spec = switch (inputId.extension) { + 'json' => serializers.deserializeWith( + OpenAPI.serializer, + json.decode(await buildStep.readAsString(inputId)), + )!, + 'yaml' => checkedYamlDecode( + await buildStep.readAsString(inputId), + (final m) => serializers.deserializeWith(OpenAPI.serializer, m)!, + ), + _ => throw StateError('Openapi specs can only be yaml or json.'), + }; final supportedVersions = ['3.0.3', '3.1.0']; if (!supportedVersions.contains(spec.version)) { diff --git a/packages/dynamite/dynamite/pubspec.yaml b/packages/dynamite/dynamite/pubspec.yaml index 86ca9a27..aab6937e 100644 --- a/packages/dynamite/dynamite/pubspec.yaml +++ b/packages/dynamite/dynamite/pubspec.yaml @@ -8,6 +8,7 @@ dependencies: build: ^2.4.1 built_collection: ^5.1.1 built_value: ^8.6.2 + checked_yaml: ^2.0.3 code_builder: ^4.6.0 collection: ^1.17.2 dart_style: ^2.3.2