autorest icon indicating copy to clipboard operation
autorest copied to clipboard

If the swagger definiton of an API has a queryParameter of type enum then Autorest fails to generate the correct API signature.

Open updixit opened this issue 9 years ago • 6 comments

If a swagger API has a query parameter of type enum, Autorest is unable to generate the correct API signature. It treats the enum to be of type 'object'.

I used Autorest 1.2.2 and petStore.json file from Samples/1a-code-generation-minimal to repro this behavior. Repro steps using petstore.json file:

  1. Define a simple enum called "TestEnum" in the definitions section.
  2. Update 'addPetUsingByteArray' API to include a queryparameter of 'TestEnum' type.

API generated via Autorest had the following signature: Task AddPetUsingByteArrayWithHttpMessagesAsync(object sampleEnum, string body = default(string), Dictionary<string, List> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

Expected signature is : Task AddPetUsingByteArrayWithHttpMessagesAsync(TestEnum sampleEnum, string body = default(string), Dictionary<string, List> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

updixit avatar Aug 11 '17 23:08 updixit

We have confirmed that this is an issue for C# code generation. We haven't tried it with other languages.

bitsforthought avatar Aug 11 '17 23:08 bitsforthought

Doing exactly what you describe can only lead to invalid Swagger: Defining TestEnum in the definitions section means you gotta $ref to it from your parameter. I'd be curious how you did that since (for a query parameter) I don't see a valid way, refer to https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#fixed-fields-7 My best guess is that you added a schema, which is only valid on body parameters (and ignored on others which would lead to object).

This is a weird limitation of Swagger 2.0 but note that this doesn't mean you have to copy paste your enum definition. Simply use the parameters section (don't forget to set "x-ms-parameter-location": "method"). I tested both inline and common parameters section and got the expected result:

public async Task<HttpOperationResponse> AddPetUsingByteArrayWithHttpMessagesAsync(
    TestEnum? test = default(TestEnum?), // inline
    TestEnumX? test2 = default(TestEnumX?), // parameters section
    string body = default(string), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))

Let me know if I guessed wrong, misunderstand or whether there is any further issue, I'd be glad to help! 🙂

olydis avatar Aug 12 '17 00:08 olydis

Thanks. This works.

updixit avatar Aug 18 '17 21:08 updixit

Hi @olydis, As per your suggestion, I defined the enum in the parameters section. Now, I am seeing errors while validating the Swagger during 'travis-ci' build. I'd appreciate your help here. https://travis-ci.com/Azure/azure-rest-api-specs-pr/jobs/92511334 Following are the changes I did: eg:

Parameters section of the swagger 
"AutocompleteModeParameter": {
	 "name": "autocompleteMode",
      "in": "query",
      "required": true,      
      "type": "string",
      "x-ms-global": true,
	  "enum": [
        "oneTerm",
        "twoTerms",
        "oneTermWithContext"
      ],
      "x-ms-enum": { 
       "name": "AutocompleteMode",
       "modelAsString": false
      },
      "x-ms-parameter-location": "method",
      "description": "The autocomplete mode."
    }

It is referenced in the parameter section of the a GET API as follows:

{
            "$ref": "#/parameters/AutocompleteModeParameter"
          },

Error during travis-ci build.

Error: Schema validation failed: [ { code: 'ONE_OF_MISSING',

params: [],

 message: 'Data does not match any schemas from \'oneOf\'',

path: '#/parameters/AutocompleteModeParameter',

schemaId: 'https://raw.githubusercontent.com/Azure/autorest/master/schema/swagger-extensions.json#',

 inner: 

  [ { code: 'OBJECT_MISSING_REQUIRED_PROPERTY',

  params: [ 'schema' ],

 message: 'Missing required property: schema',

 path: '#/parameters/AutocompleteModeParameter',

schemaId: undefined },

 { code: 'ONE_OF_MISSING',

 params: [],

message: 'Data does not match any schemas from \'oneOf\'',

 path: '#/parameters/AutocompleteModeParameter',

schemaId: undefined,

  inner: 

 [ { code: 'OBJECT_ADDITIONAL_PROPERTIES',

 params: [ [ 'x-ms-global' ] ],

  message: 'Additional properties not allowed: x-ms-global',
   path: '#/',
   schemaId: undefined },
 { code: 'OBJECT_ADDITIONAL_PROPERTIES',
  params: [ [ 'x-ms-global' ] ],
 message: 'Additional properties not allowed: x-ms-global',
 path: '#/',
   schemaId: undefined },
 { code: 'OBJECT_ADDITIONAL_PROPERTIES',

  params: [ [ 'x-ms-global' ] ],

 message: 'Additional properties not allowed: x-ms-global',

  path: '#/',

  schemaId: undefined },

 { code: 'OBJECT_ADDITIONAL_PROPERTIES',

 params: [ [ 'x-ms-global' ] ],

message: 'Additional properties not allowed: x-ms-global',

path: '#/',

   schemaId: undefined } ] } ] } ]


updixit avatar Sep 28 '17 21:09 updixit

As mentioned via IM, for the record: x-ms-global is unexpected. What is its purpose?

olydis avatar Sep 29 '17 16:09 olydis

Thanks, removing x-ms-global resolved all errors related to schema validation.

updixit avatar Sep 29 '17 17:09 updixit