Azure / azure-cli

Azure Command-Line Interface
MIT License
3.91k stars 2.87k forks source link

`az apim api import`: add Parameter `translateRequiredQueryParameters` #29081

Open dornfeder opened 3 weeks ago

dornfeder commented 3 weeks ago

Related command az apim api import

Is your feature request related to a problem? Please describe. In Azure Api Management, during the import of an OpenApi spec, by default, required query parameters get translated to template parameters. (see https://learn.microsoft.com/en-us/azure/api-management/api-management-api-import-restrictions#api-management-during-openapi-import)

Which leads an API imported that way to return unexpected status codes when the required parameters are missing (the issue is documented here: https://learn.microsoft.com/en-us/answers/questions/829259/apim-returning-404-when-mandatory-query-parameter)

To prevent this behaviour and import the query parameters as query parameters, the Rest API provides the property translateRequiredQueryParameters, which can be set to query. (see https://learn.microsoft.com/en-us/rest/api/apimanagement/apis/create-or-update?view=rest-apimanagement-2022-08-01&tabs=HTTP#translaterequiredqueryparametersconduct).

Unfortunately, the corresponding azure cli command az apim api import does not seem to provide any way to set the above mentioned property, so you are currently stuck with the default behaviour, when using azure cli.

Describe the solution you'd like I would like an additional parameter translateRequiredQueryParameters for the az apim api import command to be able to control the behaviour during the import. The parameter should map to the already existing property in the Rest Api (see above).

examples:

# Tell the API Management to keep the required query params as query params
az apim api import --translateRequiredQueryParameters=query ...

# Tell the API Management to translate the required query params to template parameters
az apim api import --translateRequiredQueryParameters=template ...

# If the parameter is not explicitly set, the default behaviour should still be template to be backwards compatible
az apim api import ...

Describe alternatives you've considered

Additional context Since this is a behaviour very specific to Azure API Management which is not really to be expected, I think its vital to provide a way to change the default behaviour, regardless if the API was imported through UI, REST Api or Azure cli.

yonzhan commented 3 weeks ago

Thank you for opening this issue, we will look into it.

rsirac commented 2 weeks ago

@yonzhan Do you have any idea when this patch will be released? Indeed, I have exactly the same issue and it is quite critical, I would really appreciate to keep our use of azure cli and not have to switch to REST API implementation

dornfeder commented 2 weeks ago

In the meantime, as a workaround, I ended up using https://github.com/mikefarah/yq to switch all query parameters inside the openapi spec from required: true to required: false in our pipeline before uploading the spec to api management.

That way the spec in the repo remains correct but we avoid the issues with api management:

# We are setting all required query parameters to false to avoid any issues with the import
# see https://learn.microsoft.com/en-us/answers/questions/829259/apim-returning-404-when-mandatory-query-parameter
echo "Preparing Api Schema for import..."
yq "(
  .paths.*.*
    |= select(has(\"parameters\"))
    |= .parameters[]
    |= select(.in == \"query\")
    |= .required = false
) | (
  .components
    |= select(has(\"parameters\"))
    |= .parameters.*
    |= select(.in == \"query\")
    |= .required = false
)" -i  ${file}
echo "done"