autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Add an extension for generating randomValues for a model property or a parameter

Open amarzavery opened this issue 10 years ago • 3 comments

Some parameters like resourceGroup, vmName, deploymentName need to be passed a randomName so that they can be referenced later. Most of the times they are path parameters and endup being as required parameters of the method signature. If the Swagger spec has this extension called x-ms-random on them, then we don't need them as method parameters. The generated code can generate a random value for these parameters. If the operation is successful, then these names would be provided to the end user in the response anyway for later use.

I was thinking that the extension can be used to generate a random alphanumeric string or a random number based on the type of the parameter/property. The extension can have an optional property named prefix with a defaultValue as the parameter name that can be used to generate a random unique name for that artifact if it is of type string

To keep it simple, the extension can be made applicable only for string and integer types. For anything else, either throw an exception or ignore and log a warning. Note: The value (length) of a random number will be dependent upon the implementation of random in the programming language of the generated code.

Some examples as follows:

  • random value for a string type parameter with a predefined prefix. Output would be: "testRG34562"
{
  "name": "resourceGroupName",
  "in": "path",
  "required": true,
  "type": "string",
  "description": "The name of the resource group. The name is case insensitive.",
  "x-ms-random": {
    "prefix": "testRG"
  }
 }
  • random value for a string type parameter with a default value of prefix that is (parameter name). Out would be : "deploymentName51232"
{
  "name": "deploymentName",
  "in": "path",
  "required": true,
  "type": "string",
  "description": "The name of the deployment to be deleted.",
  "x-ms-random": {
  }
}
  • random value for a string type parameter which explicilty specifies that it does not want a prefix. Output would be: "56123"
{
  "name": "deploymentName",
  "in": "path",
  "required": true,
  "type": "string",
  "description": "The name of the deployment to be deleted.",
  "x-ms-random": {
    "prefix": null
  }
}
  • random value for an integer type parameter. Prefix would not be applicable for numbers. Out would be : 565448
{
  "name": "capacity",
  "in": "path",
  "required": true,
  "type": "integer",
  "format": "int32",
  "description": "The capacity for a slot.",
  "x-ms-random": {
  }
}

Suggestions are welcome :).

amarzavery avatar Feb 08 '16 00:02 amarzavery

As per #688, this extension can be used for guids, as well.

amarzavery avatar Feb 08 '16 00:02 amarzavery

We should add an attribute for allowed char set.

azuresdkci avatar Mar 02 '16 00:03 azuresdkci

I'm closing this, the assumption is that it's covered by the uuid one...

fearthecowboy avatar Jun 15 '16 19:06 fearthecowboy