From 356319bbb8aac5859939bdf376bf542cf856bfcb Mon Sep 17 00:00:00 2001 From: Alexey Date: Mon, 17 Jun 2024 19:00:12 -0700 Subject: [PATCH] Fix (#139) --- CHANGELOG.md | 5 +++++ example/server.dart | 1 + lib/src/query/fields.dart | 2 +- lib/src/query/include.dart | 2 +- lib/src/server/controller_router.dart | 16 ++++++++++------ pubspec.yaml | 2 +- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 679d663..4dac7f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [7.0.1] - 2024-06-17 +### Fixed +- "Accept" header with multiple values was being mishandled + ## [7.0.0] - 2023-11-12 ### Changed - Migrated to `http_interop` v1. @@ -246,6 +250,7 @@ the Document model. ### Added - Client: fetch resources, collections, related resources and relationships +[7.0.1]: https://github.com/f3ath/json-api-dart/compare/7.0.0...7.0.1 [7.0.0]: https://github.com/f3ath/json-api-dart/compare/6.0.1...7.0.0 [6.0.1]: https://github.com/f3ath/json-api-dart/compare/6.0.0...6.0.1 [6.0.0]: https://github.com/f3ath/json-api-dart/compare/5.4.0...6.0.0 diff --git a/example/server.dart b/example/server.dart index bea19e0..9235e38 100644 --- a/example/server.dart +++ b/example/server.dart @@ -23,6 +23,7 @@ Future main() async { handler = TryCatchHandler(handler, onError: ErrorConverter(onError: (e, stack) async { stderr.writeln(e); + stderr.writeln(stack); return Response(500, document: OutboundErrorDocument( [ErrorObject(title: 'Internal Server Error')])); diff --git a/lib/src/query/fields.dart b/lib/src/query/fields.dart index 0291e86..a09bab7 100644 --- a/lib/src/query/fields.dart +++ b/lib/src/query/fields.dart @@ -19,7 +19,7 @@ class Fields with MapMixin> implements QueryEncodable { static Fields fromUri(Uri uri) => Fields(uri.queryParametersAll.map((k, v) => MapEntry( _regex.firstMatch(k)?.group(1) ?? '', - v.expand((_) => _.split(',')).toList())) + v.expand((it) => it.split(',')).toList())) ..removeWhere((k, v) => k.isEmpty)); static final _regex = RegExp(r'^fields\[(.+)\]$'); diff --git a/lib/src/query/include.dart b/lib/src/query/include.dart index 8d26b23..838cfc3 100644 --- a/lib/src/query/include.dart +++ b/lib/src/query/include.dart @@ -14,7 +14,7 @@ class Include with IterableMixin implements QueryEncodable { } static Include fromUri(Uri uri) => Include( - uri.queryParametersAll['include']?.expand((_) => _.split(',')) ?? []); + uri.queryParametersAll['include']?.expand((it) => it.split(',')) ?? []); final _ = []; diff --git a/lib/src/server/controller_router.dart b/lib/src/server/controller_router.dart index 08f25a6..e10d519 100644 --- a/lib/src/server/controller_router.dart +++ b/lib/src/server/controller_router.dart @@ -2,6 +2,7 @@ import 'package:http_interop/http_interop.dart'; import 'package:http_parser/http_parser.dart'; import 'package:json_api/http.dart'; import 'package:json_api/routing.dart'; +import 'package:json_api/src/media_type.dart'; import 'package:json_api/src/server/controller.dart'; import 'package:json_api/src/server/errors/method_not_allowed.dart'; import 'package:json_api/src/server/errors/unacceptable.dart'; @@ -47,16 +48,19 @@ class ControllerRouter implements Handler { void _validate(Request request) { final contentType = request.headers.last('Content-Type'); - if (contentType != null && !_isValid(MediaType.parse(contentType))) { + if (contentType != null && _isInvalid(MediaType.parse(contentType))) { throw UnsupportedMediaType(); } - final accept = request.headers.last('Accept'); - if (accept != null && !_isValid(MediaType.parse(accept))) { + if ((request.headers['Accept'] ?? []) + .expand((it) => it.split(',')) + .map((it) => it.trim()) + .map(MediaType.parse) + .any(_isInvalid)) { throw Unacceptable(); } } - bool _isValid(MediaType mediaType) { - return mediaType.parameters.isEmpty; // TODO: check for ext and profile - } + bool _isInvalid(MediaType mt) => + mt.mimeType == mediaType && + mt.parameters.isNotEmpty; // TODO: check for ext and profile } diff --git a/pubspec.yaml b/pubspec.yaml index d47cfb3..eab220f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: json_api -version: 7.0.0 +version: 7.0.1 homepage: https://github.com/f3ath/json-api-dart description: A framework-agnostic implementations of JSON:API Client and Server. Supports JSON:API v1.0 (https://jsonapi.org) environment: