Skip to content

Working with AutoRest

Weidong Xu edited this page Nov 4, 2021 · 8 revisions

AutoRest is the name of the tool we use to generate the communication layer that lies beneath the API we expose to our users. AutoRest saves us from having to write a lot of network communication code, and instead we can direct our focus towards building a great user experience on top of this generated code. Our goal should be to expose as little, and ideally none, of the generated code to the user.

Getting started with AutoRest

  1. Fork and clone this GitHub repo.
git clone https://github.com/Azure/autorest.java.git
  1. Build the package using Maven
mvn clean package -Dlocal
  1. Run npm install. You need v10 version of Node.JS installed.
  2. Run npm install -g autorest.
  3. Run AutoRest commands like this:

To generate code from a local README file:

autorest --java --use=C:\work\autorest.java\ C:\swagger\updated\Blob\readme.md --output-folder=C:\work\azure-sdk-for-java\storage\client\ --namespace=com.azure.storage.blob --sync-methods=none --generate-client-interfaces=false --enable-xml --required-parameter-client-methods

To generate code from a json swagger spec accessed from a URL:

autorest --java --use:./ --input-file=https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/search/data-plane/Azure.Search/preview/2019-05-06-preview/searchservice.json --namespace=com.azure.search.service --output-folder=generated-code --sync-methods=all --client-side-validations=true --add-context-parameter=true --context-client-method-parameter=true 

AutoRest Options

  • Include license header in generated code

Option name: license-header

Option values:

Option Value Header
MICROSOFT_MIT
MICROSOFT_APACHE
MICROSOFT_MIT_NO_VERSION
MICROSOFT_APACHE_NO_VERSION
MICROSOFT_MIT_NO_CODEGEN
MICROSOFT_MIT_SMALL
MICROSOFT_MIT_SMALL_NO_CODEGEN
NONE
  • Namespace

    Option name: namespace

    Option value: Java base package name used for generating the code

  • Client side validation

    Option name: client-side-validations

    Default value: false

  • Client type prefix

    Option name: client-type-prefix

  • Generate client interfaces

    Option name: generate-client-interfaces

  • Generate client as implementation type

    Option name: generate-client-as-impl

  • Implementation subpackage name

    Option name: implementation-subpackage

  • Models subpackage name

    Option name: models-subpackage

  • Custom types

    Option name: custom-types

  • Custom types subpackage name

    Option name: custom-types-subpackage

  • Add context parameter to proxy methods

    Option name: add-context-parameter

  • Add context parameter to client methods

    Option name: context-client-method-parameter

  • Generate sync and async clients

    Option name: generate-sync-async-clients

  • Generate models with required fields as constructor arguments

    Option name: required-fields-as-ctor-args

  • Enable xml

    Option name: enable-xml

  • Generate fluent interface

    Option name: fluent

  • Output folder

    Option name: output-folder

  • Input file

    Option name: input-file

Notes:

  1. --use=<path to your autorest.java clone>
  2. If the input is a JSON spec, use --input-file=<local path of URL of JSON file>, if it's a README.md file, remove --input-file=
  3. --enable-xml --required-parameter-client-methods are not needed for services other than Storage
  4. autorest --reset to remove all extensions; or autorest --version=<string> to specify the autorest.core version
Clone this wiki locally