# How to automatically generate the REST client code
Rest client code in this folder are not manually written, but generated by autorest.

## Setup
+ install [nodejs](https://nodejs.org/en)
+ install autorest
  + run `npm install -g autorest`

## Download swagger.json
Download swagger.json from [here](https://int.api.azureml-test.ms/flow/swagger/v1.0/swagger.json) to 
[promptflow/azure/_restclient](../promptflow/azure/_restclient)

## Update code
+ cd to [promptflow/azure/_restclient](../promptflow/azure/_restclient)
+ run `autorest --v3 --python --track2 --version=3.8.0 --use=@autorest/python@5.12.2 --input-file=swagger.json --output-folder=. --namespace=flow --modelerfour.lenient-model-deduplication`
  + don't change `--use`. latest version of `autorest/python` will generate code following different pattern, which is not compatible with our code.

## Update the generation history

- 2023.11.13 - [Update SDK restclient](https://github.com/microsoft/promptflow/pull/1101).
- 2023.12.18 - [Remove data portal url from the result of pfazure run show](https://github.com/microsoft/promptflow/pull/1497)

## Troubleshooting

### Duplicate object schemas with "xxx" name detected.

This may be caused by the duplicate generated class names.

```json
"FlowFeature": {
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "nullable": true
    },
    "description": {
      "type": "string",
      "nullable": true
    },
    "state": {
      "type": "object",
      "properties": {
        "Runtime": {
          "$ref": "#/components/schemas/FlowFeatureState"
        },
        "Executor": {
          "$ref": "#/components/schemas/FlowFeatureState"
        },
        "PFS": {
          "$ref": "#/components/schemas/FlowFeatureState"
        }
      },
      "additionalProperties": false,
      "nullable": true
    }
  },
  "additionalProperties": false
},
"FlowFeatureState": {
  "enum": [
    "Ready",
    "E2ETest"
  ],
  "type": "string"
},
```

`FlowFeature` has a nested object field `state`, which will be generated to a new class named `FlowFeatureState`, and it duplicates with the enum `FlowFeatureState`.

To fix this, server side needs to change the class name in the schema, in this case, server side changed the object `state` to `states` and the problem is resolved.
