Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Cannot convert from nullable to string for x-ms-parameterized-host enum backing struct when used in RawRequestUriBuilder in generated RestClient #1376

Closed
ambientlight opened this issue Jun 30, 2021 · 0 comments · Fixed by #1421
Assignees
Labels
v3 Version 3 of AutoRest C# generator.

Comments

@ambientlight
Copy link
Member

ambientlight commented Jun 30, 2021

Describe the issue or request
Usecase:
x-ms-parameterized-host is used with enum, declaration:

{
   "x-ms-parameterized-host": {
      "hostTemplate": "{geography}.atlas.microsoft.com",
      "parameters": [
        {
          "$ref": "#/parameters/GeographicResourceLocation"
        }
      ]
    },
   "parameters":  {
    "GeographicResourceLocation": {
      "name": "geography",
      "description": "This parameter specifies where the Azure Maps Creator resource is located.  Valid values are us and eu.",
      "in": "path",
      "required": true,
      "type": "string",
      "default": "us",
      "enum": [
        "us",
        "eu"
      ],
      "x-ms-enum": {
        "name": "GeographicResourceLocation",
        "modelAsString": true,
        "values": [
          {
            "value": "us",
            "description": "Used to access an Azure Maps Creator resource in the United States"
          },
          {
            "value": "eu",
            "description": "Used to access an Azure Maps Creator resource in Europe"
          }
        ]
      },
      "x-ms-parameter-location": "client"
    }
  }
}

Source:
https://github.com/propheel/azure-rest-api-specs/blob/dev-maps-Microsoft.Maps-2.0-versioning-test/specification/maps/data-plane/Common/preview/1.0/common.json
https://github.com/propheel/azure-rest-api-specs/blob/dev-maps-Microsoft.Maps-2.0-versioning-test/specification/maps/data-plane/Render/preview/2.0/render.json

This generates a perfectly valid backing struct:

Geography.cs

The issue appears though with regards to generated RestClient:

namespace Azure.Maps.Render
{
    internal partial class RenderRestClient
    {
        private Geography? geography;

        // reducted ....
        
        internal HttpMessage CreateGetMapStaticImageRequest(RasterTileFormat format, StaticMapLayer? layer, MapImageStyle? style, int? zoom, string center, string bbox, int? height, int? width, string language, string view, IEnumerable<string> pins, IEnumerable<string> path)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;
            request.Method = RequestMethod.Get;
            var uri = new RawRequestUriBuilder();
            uri.AppendRaw("https://", false);
            
            // because geography is nullable, geography.Value is compile time error
            // error CS1503: Argument 1: cannot convert from 'Azure.Maps.Creator.Models.Geography' to 'string'
            uri.AppendRaw(geography.Value, true);

            // reducted ...
         }
}

dotnet --version: 5.0.301
platform: WSL2/Ubuntu 20.04

Autorest command:

npx autorest@3.2.1 --skip-csproj --skip-upgrade-check --version=3.4.5 specification/maps/data-plane/Render/readme.md --use=/home/ambientlight/.nuget/packages/microsoft.azure.autorest.csharp/3.0.0-beta.20210615.2/buildMultiTargeting/../tools/netcoreapp3.1/any/ --output-folder=/home/ambientlight/repos/azure-sdk-for-net/sdk/maps/Azure.Maps.Render/src/Generated --namespace=Azure.Maps.Render --clear-output-folder=true --shared-source-folders="/home/ambientlight/repos/azure-sdk-for-net/eng//../sdk/core/Azure.Core/src/Shared/;/home/ambientlight/.nuget/packages/microsoft.azure.autorest.csharp/3.0.0-beta.20210615.2/buildMultiTargeting/../content/Generator.Shared/" --public-clients

AutoRest code generation utility [cli version: 3.2.1; node: v14.17.0, max-memory: 2048 MB]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
   Loading AutoRest core      '/home/ambientlight/.autorest/@autorest_core@3.4.5/node_modules/@autorest/core/dist' (3.4.5)
INFORMATION: > Loading local AutoRest extension '@autorest/csharp' (/home/ambientlight/.nuget/packages/microsoft.azure.autorest.csharp/3.0.0-beta.20210615.2/buildMultiTargeting/../tools/netcoreapp3.1/any/)
INFORMATION: > Loading AutoRest extension '@autorest/modelerfour' (4.19.3->4.19.3)
WARNING: Semantic violation: Sibling values alongside $ref will be ignored. See https://github.com/Azure/autorest/blob/main/docs/openapi/howto/$ref-siblings.md for allowed values (components > schemas > SearchCommonResponse > properties > summary)
  keys: [ 'readOnly' ]
    - file:///home/ambientlight/repos/azure-rest-api-specs/specification/maps/data-plane/Search/preview/1.0/search.json:478:5
WARNING: Semantic violation: Sibling values alongside $ref will be ignored. See https://github.com/Azure/autorest/blob/main/docs/openapi/howto/$ref-siblings.md for allowed values (components > schemas > SearchCommonResponse > properties > results > items)
  keys: [ 'readOnly' ]
    - file:///home/ambientlight/repos/azure-rest-api-specs/specification/maps/data-plane/Search/preview/1.0/search.json:487:6
WARNING: Semantic violation: Sibling values alongside $ref will be ignored. See https://github.com/Azure/autorest/blob/main/docs/openapi/howto/$ref-siblings.md for allowed values (components > schemas > SearchAddressReverseResponse > properties > summary)
  keys: [ 'readOnly' ]
    - file:///home/ambientlight/repos/azure-rest-api-specs/specification/maps/data-plane/Search/preview/1.0/search.json:671:5
WARNING: Semantic violation: Sibling values alongside $ref will be ignored. See https://github.com/Azure/autorest/blob/main/docs/openapi/howto/$ref-siblings.md for allowed values (components > schemas > SearchAddressReverseCrossStreetResponse > properties > summary)
  keys: [ 'readOnly' ]
    - file:///home/ambientlight/repos/azure-rest-api-specs/specification/maps/data-plane/Search/preview/1.0/search.json:709:5

WARNING (PreCheck/CheckDuplicateSchemas): Checking for duplicate schemas, this could take a (long) while.  Run with --verbose for more detail.
[8.86 s] Generation Complete

Describe your ideas for solutions
I am using a rather sample hotfix to alleviate the above to replace geography.Value with geography.ToString():

find ../azure-sdk-for-net/sdk/maps/$SERVICE_NAME/src/ -name '*.cs' -exec sed -i -e 's/AppendRaw(geography.Value, true)/AppendRaw(geography.ToString(), true)/g' {} \;

Which will result in the following URI produced: https://.atlas.microsoft.com/ in the case when geography is null, which might not be overall the best approach, though this should not happen as the generated constructor has the elvis assignment present: geography ??= Geography.Us;

To reproduce:

  1. Checkout from [Maps] Add separate Swagger definitions for Maps client libraries azure-rest-api-specs#14871
  2. Comment out line48 in gen_csharp.sh and execute the script in the root of azure-rest-api-spec
@ambientlight ambientlight added the v3 Version 3 of AutoRest C# generator. label Jun 30, 2021
@ambientlight ambientlight changed the title [BUG] [BUG] Cannot convert from nullable to string for x-ms-parameterized-host enum backing struct when used in RawRequestUriBuilder in generated RestClient Jun 30, 2021
@AlexanderSher AlexanderSher self-assigned this Jul 16, 2021
AlexanderSher added a commit to AlexanderSher/autorest.csharp that referenced this issue Jul 21, 2021
…-parameterized-host enum backing struct when used in RawRequestUriBuilder in generated RestClient v3

Fix Azure#1375: [BUG] Redundant client constructor copies generated on --public-clients and multiple input files v3
AlexanderSher added a commit to AlexanderSher/autorest.csharp that referenced this issue Jul 21, 2021
…-parameterized-host enum backing struct when used in RawRequestUriBuilder in generated RestClient v3

Fix Azure#1375: [BUG] Redundant client constructor copies generated on --public-clients and multiple input files v3
This was referenced Jul 21, 2021
AlexanderSher added a commit to AlexanderSher/autorest.csharp that referenced this issue Jul 23, 2021
…-parameterized-host enum backing struct when used in RawRequestUriBuilder in generated RestClient v3

Fix Azure#1375: [BUG] Redundant client constructor copies generated on --public-clients and multiple input files v3
AlexanderSher added a commit to AlexanderSher/autorest.csharp that referenced this issue Jul 24, 2021
…-parameterized-host enum backing struct when used in RawRequestUriBuilder in generated RestClient v3

Fix Azure#1375: [BUG] Redundant client constructor copies generated on --public-clients and multiple input files v3
AlexanderSher added a commit that referenced this issue Jul 24, 2021
- Fix #1376: [BUG] Cannot convert from nullable to string for x-ms-parameterized-host enum backing struct when used in RawRequestUriBuilder in generated RestClient v3
- Fix #1375: [BUG] Redundant client constructor copies generated on --public-clients and multiple input files v3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v3 Version 3 of AutoRest C# generator.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants