Giter Club home page Giter Club logo

Comments (5)

lrhn avatar lrhn commented on August 15, 2024 1

The other option is to treat a parameter of test1 as having no value, and test2= as having an empty value.
Then a URL of something?test1&test2=&test3=abc would encode the difference and parsing back in could recreate it. (If there was no way to represent the difference in text, it can't be round-tripped, but this may be a way.)

I'll have to check what the WhatWG URL spec does, because I still hope to one day migrate Dart Uri to that behavior.
If they don't distinguish the two cases, then Dart also won't.
If they do, then we may want to see if this is a breaking change worth making early, or is better to include it on a bigger update.
(Probably the latter, since we never noticed before.)

from sdk.

dart-github-bot avatar dart-github-bot commented on August 15, 2024

Labels: area-core-library, type-bug
Summary: The Uri class encodes empty and null query parameters identically, making it impossible to distinguish between them after parsing. This inconsistency with common conventions, like those used by Java Spring, can lead to unexpected behavior when interacting with other systems.

from sdk.

lrhn avatar lrhn commented on August 15, 2024

I think this is working as intended.
URIs are textual, they cannot represent "key with no value". Well, they could, but they don't.

As your example http://google.com?test2&test3=&test4=null shows, that URI has the keys test2, test3 and test4.
Their values are the strings "", "" and "null".
There is no support in a Uri for having a key with no string value, so if the key is there, the value is at least the empty string.

from sdk.

lrhn avatar lrhn commented on August 15, 2024

That said, while this is intentional, it doesn't mean we can't change it. (It's harder, because it may break existing code).
So labeleing as enhancement request.

from sdk.

jakobleck avatar jakobleck commented on August 15, 2024

Thanks for your reply, Lasse. Your explanation made me realize an error in my reasoning (of course the String "null" cannot always be taken to mean null), but the main point is still valid:

To me it would seem reasonable if the following test passed, which currently it does not do.

import 'package:flutter_test/flutter_test.dart';

void main() {
  test(
    "URI encoding should not change value assigned to a parameter",
    () {
      final queryParameters = {"test": null};
      final uri = Uri(queryParameters: queryParameters);
      final uriString = uri.toString();
      final parsedUri = Uri.parse(uriString);
      final parsedParameters = parsedUri.queryParameters;
      expect(queryParameters["test"], parsedParameters["test"]);
    },
  );
}

This could be reached by removing all key-value-pairs with a null value when creating the query string. But yes, this might break existing code.

If you're happy to say the current behaviour is as intended, we can close this.

from sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.