Skip to content

Way to skip or specify to urlencode a parameter #851

Closed

Description

Currently autorest has this extension x-ms-skip-url-encoding which lets tells that the parameter shouldn't be url encoded. We are lacking this functionality

Activity

  1. added
    design:needed
    on Aug 11, 2022
  2. markcowl commented on Aug 16, 2022

    @markcowl
    Contributor

    pri: 0

  3. tjprescott commented on Aug 17, 2022

    @tjprescott
    Member

    I'd be happy to work on this but I can't assign myself.

  4. mikekistler commented on Aug 17, 2022

    @mikekistler
    Contributor

    I recently got some clarification on URL encoding from Darrel Miller:

    URLs should be encoded at the point of construction. That is the only time when you know which parts are data elements in the URL. If a URL has been fully constructed, it should be considered sufficiently encoded [ref RFC 3986]

    So I'm wondering when we would ever want to specify "x-ms-skip-url-encoding".

  5. timotheeguerin commented on Aug 17, 2022

    @timotheeguerin
    MemberAuthor

    /file/{filePath} I think a path parameter like that you don't want / to be encoded no?

    Or if the type is uri for the server variables. but for that one we can maybe auto guess

  6. mikekistler commented on Aug 17, 2022

    @mikekistler
    Contributor

    I would have expected "/" to be encoded in filePath, but maybe this is a special case. Do we have anything like this in any of the services we are planning for this semester?

    As for a parameter/property that is a URL, these should always be encoded, so we don't need x-ms-skip-url-encoding for these.

  7. timotheeguerin commented on Aug 17, 2022

    @timotheeguerin
    MemberAuthor

    I mean in this case

    @server("{host}/foo", {host: uri})
    

    I do feel like this is something we need to support. For example github api to get files:
    https://docs.github.com/en/rest/repos/contents

    which is at /repos/{owner}/{repo}/contents/{path} and would be that https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt for notes/hello.txt as the path.

    This I guess is the OpenAPI issue to support that built in OAI/OpenAPI-Specification#892, maybe that's how we need to think about it for that use case instead.

    For usage needed Travis Prescott (@tjprescott) had a service who needed and can probably tell us more.

  8. lmazuel commented on Aug 17, 2022

    @lmazuel
    Member

    Mike Kistler (@mikekistler) example of case, scope in ARM is a url, like /subscription/123-456-789/resourceGroup/rgname and when you set a policy you do something like POST /subscription/123-456-789/resourceGroup/rgname/policy. The template will be defined as {scope}/policy. with scope tagged to NOT be encoded.

    Example:
    https://github.com/Azure/azure-rest-api-specs/blob/2614c5038cb384e864c1608e76be61a5ee84cb02/specification/resources/resource-manager/Microsoft.Authorization/stable/2021-06-01/policyAssignments.json#L37

  9. timotheeguerin commented on Aug 17, 2022

    @timotheeguerin
    MemberAuthor

    So wondering if the only case is really that we don't want to escape / but wouldn't we maybe still want to escape in between.
    For example if the scope was /subscription/123-456/rg with spaces/bar (just supposing that was allowed)

    Wondering if maybe what we want is a way to say that a path parameter is actually multiple segment:

    
    @route("{*scope}/policy") // I think the way dotnet does it with a * prefix.
    @route("{scope*}/policy") // With a * suffix
    @route("{scope+}/policy") // With a + suffix
    op test(@path scope: string);
    
  10. mikekistler commented on Aug 18, 2022

    @mikekistler
    Contributor

    Laurent Mazuel (@lmazuel) My point is that if we know something is a URL then we never want to encode it. It would be wrong to encode it. So there is no need to specify "skip-url-encoding" on URLs.

  11. 8 remaining items

  12. timotheeguerin commented on Sep 13, 2022

    @timotheeguerin
    MemberAuthor

    Proposal

    Path parameters are always url encoded except:

    • the path parameter type is uri and is used in @server value.
      • if path parameter is uri in an operation it gets encoded
    • A path parameter is marked as having multiple segments(multiple /)
      • In this case it wouldn't actually skip encoding completely, it would only need to skip encoding of /

    How to mark a path parameter as having multiple segments(multiple /)

    Option 1: Have a convention in the @route path

    @route("{*scope}/policy") // I think the way dotnet does it with a * prefix.
    @route("{scope*}/policy") // With a * suffix
    @route("{scope+}/policy") // With a + suffix
    op test(@path scope: string): void;
    

    Pros:

    • Similar to a lot of web api framework

    Cons:

    • Cannot reuse the parameter with this config. (But maybe that's better that way)

    Option 2 Add a flag on @path decorator

    @route("{scope}/policy") // With a + suffix
    op test(@path({multi: true}) scope: string): void;
    

    Pros:

    • Easier to reuse the parameter. When using this param wherever it will always skip encoding of /

    Cons:

    • ?
  13. mikekistler commented on Sep 13, 2022

    @mikekistler
    Contributor

    Path parameters that may contain unencoded "/" are strongly discouraged as they make the REST API definition ambiguous. If we support this then we also need a linter message that flags these.

  14. markcowl commented on Sep 14, 2022

    @markcowl
    Contributor

    Notes

    • Scenarios we should explore
      • Cases in which the path parameter is a uri path (already appropriately url encoded)
      • Cases in which the path parameter consists of multiple segments that are not url encoded
      • Consider each of these in route and autoRoute cases. autoRoute will be much more prevalent in Azure
      • Is there a general need for specifying 'urlEncode' / 'skipUrlEncode' other than in path parameters?
      • Include expected representations in cadl-autorest and OpenAPI3
    // using route template
    route("{scope+}/policy")
    op myOp(@path scope: string) : void;
    
    // using array value to indicate multiple unencoded parts
    @autoRoute
    op myOp(@path scope: string[], @path name: string) : void;
    
    // using multi to encode segment by segment
    scope: "/subscription/mysub/rg/my+rg/providers/Microsoft.Provider/type/name/"
    @autoRoute
    op myOp(@path({multi: true}) scope: string, @path name: string): void;
    
    // using skip encoding to skip encoding altogether
    scope: "/subscription/mysub/rg/my+rg/providers/Microsoft.Provider/type/name/"
    @autoRoute
    op myOp(@path({skipEncoding: true}) scope: string, @path name: string): void;
    
    //using type to skip encoding
    @format("uri")
    model uriPath is string;
    scope: "/subscription/mysub/rg/my+rg/providers/Microsoft.Provider/type/name/"
    op myOp(@path scope: uriPath, @path name: string): void;
    
    
  15. markcowl commented on Sep 14, 2022

    @markcowl
    Contributor

    Mike Kistler (@mikekistler) Yes, in OpenAPI path segments must not contain unencoded path separators.

    However, in Azure APIs, there is a common pattern, originating in ARM but also used in data planes of entities that 'extend' other entities. This results in paths of variable length for the same operation, like:

    /{extended-entity-identifier}/{extending-entity-type}/{extending-entity-name}

    Since {extended-entity-identifier} is unknown, could contain any number of segments, and does not actually change the operations over the extending resource, it makes sense to describe these as operations under a single path, rather than enumerating all of the possible paths and repeating the operations.

    Personally I am favoring something like a UriPath type. This would allow clients to decide whether to accept an 'already encoded' path variable, or to accept an array of path segments or the like. Languages with Uri types might decide to use those Uri types. This also means we can maintain the current mechanisms we use to represent these operations in OpenAPI.We should propose how to represent them in OpenAPI3 as well.

  16. markcowl commented on Oct 20, 2022

    @markcowl
    Contributor

    Revised Proposal

    • Add an optional settings parameter to @path decorator that marks a path as multi-segment
    @path({multi: true}) myMultiSegmentPath: string
    
    • This would be used for parameters that represent path segments that may contain a '/' and should not be url encoded
    • Sample usage: extension resources
    // extensionUri is a multi-segment path to the resource the extension applies to
    // 'name' is the name of the FooExtension resource
    @route("{extensionUri}/fooextension/{name}")
    op getFooExtension(@path({multi: true}) extensionUri: string, @path name: string): Foo | Error;
    

    Representation in cadl-autorest

    While it is technically invalid, this pattern is common in autorest-inflected swagger. A urlPath parameter should be marked with the 'x-ms-skip-url-encoding' extension:

    "paths": {
      "{extensionUri}/fooextension/{name}": {
        "get": {
          "parameters":[
            {
              "x-ms-skip-url-encoding": true,
              "name": "extensionUri",
              "in": "path",
              "required": true
               
            },
          ]
        }
      }
    }

    Representation in OpenAPI3

    • It seems that we should add this extension to OpenAPI3, as in cases like this, there are no real alternatives.

    Scenario Coverage

    Case Code Behavior
    1. Simple string param op simple(@path foo: string) Encode foo
    2. Uri param op simple(@path foo: uri) Encode foo
    3. Param is a string but I don't want to encode / op simple(@path({multi: true}) foo: string) Don't encode the /
    4. Param is an array of string op simple(@path foos: string[]) Encode each foo and join with /. Note that @autoRoute would have to be expanded to allow this

    Alternative implementation - explicitly skip URL encoding

    • We could differentiate between the multi-segment path scenario and other scenarios in which a parameter should not be url encoded, by adding a skipUrlEncoding property to @path and @query decorators
    @path({skipUrlEncoding: true}) myEncodedSegment: string
    @query({skipUrlEncoding: true}) myQueryParameter: string
    
    • It feels like any explicit directive to skip url encoding a parameter is specifically a client concern, and likely belongs in the DPG library, rather than in a more core library

    Alternative implementation - Only use array types to indicate multiple segments

    @path myMultiSegmentPath: string[]
    
    • This might cause some problems with how autoroute currently functions
  17. markcowl commented on Oct 20, 2022

    @markcowl
    Contributor

    Notes from Design Meeting

    • Consensus was that a type was preferrable to decorator parameters
    • Consider augmenting Uri type to include different kinds of Uris, or whether types for Uri parts are necessary
    • In the context of a type-based solution, consider necessary mechanisms for overriding default encoding behavior
  18. markcowl commented on Nov 2, 2022

    @markcowl
    Contributor

    Revised Proposal

    • Per RFC 3986 model a type similar to relative-reference that represents fully-encoded url paths in cadl/http.
    @format("uri")
    model urlPath is string;
    

    Relative and absolute paths

    • The spec describes 3 types of relative-references:
      • Absolute paths, starting with '/'
      • Relative paths, not starting with '/'
      • Network paths (disused), starting with '//'

    The proposal is to use a single type to represent the common absolute and relative paths, as this provides all necessary information about encoding.

    An alternate idea is to provide individual types for relative and absolute paths, but this would largely be for documentary purposes, as we would expect any processor to correctly handle leading and trailing slashes.

    Emitters would represent relative and absolute url paths, consistent with the relative-reference concept, producing the following default behavior

    Case Code Behavior
    1. string in path op simple(@path foo: string) Encode foo
    2. urlPath in path op simple(@path foo: urlPath) Don't encode
    3. uri in path op simple(@path foo: uri) encode reserved characters
    4. string in query op simple(@query foo: string) Encode foo
    5. urlPath in query op simple(@query foo: urlPath) Don't encode
    6. uri in query op simple(@query foo: uri) Don't encode*
    7. Path Param is an array of string op simple(@path foos: string[]) Encode each foo and join with /.
    • for backward compatibility with older processors, it may be desirable to encode '/' and '?' in query parameters

    Note that here, we are discounting use of the 'network-path' option for relative-path (paths starting with //), as the spec indicates that this is a disused pattern, and we have never seen it. However, we could add a networkPath type in the future, if needed.

  19. markcowl commented on Nov 3, 2022

    @markcowl
    Contributor

    proposal:

    verify no discrepancies with the new spec: https://url.spec.whatwg.org/#relative-url-string

    • uri -> url
    • pathUrl -> relativeUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

design:needed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions