Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.58k stars 2.8k forks source link

"One or more fields contain incorrect values" error while creating new API through apimanagement python sdk #12577

Closed devang93 closed 3 years ago

devang93 commented 4 years ago

Describe the bug We are trying to use apimanagement python sdk to automate api deployment into api management service over azure. Using apimanagement v 0.2.0.

Using following configs and code to create a simple test api using sdk and its throwing "One or more fields contain incorrect values" error. It doesn't tell us which fields are incorrect, making it harder to debug.

apim_client = azure.mgmt.apimanagement.ApiManagementClient(credentials=credentials, subscription_id=subscription_id)

api_create_or_update_parameters = azure.mgmt.apimanagement.models.ApiCreateOrUpdateParameter(
    description="AI reserve deployed sample api",
    authentication_settings = None,
    subscription_key_parameter_names = None,
    api_type = None,
    api_revision= "1",
    api_version= None,
    is_current= True,
    is_online= None,
    api_revision_description= None,
    api_version_description= None,
    api_version_set_id= None,
    subscription_required= True,
    source_api_id= None,
    display_name= "Sample API",
    service_url= "https://conferenceapi.azurewebsites.net",
    path= 'sample',
    protocols= ['https'],
    api_version_set= None,
    value= "sample_test",
    format="openapi"
)

deployed_api_response = apim_client.api.create_or_update( resource_group_name, apim_service_name, "test-api", api_create_or_update_parameters)
print(deployed_api_response.result())

Exception or Stack Trace

Traceback (most recent call last):
  File "/Users/devpatel/Desktop/projects/Ai-Reserve-PlatformProvisioning/aml_service/configureAPIM.py", line 63, in <module>
    deployed_api_response = apim_client.api.create_or_update( resource_group_name, apim_service_name, "test-api", api_create_or_update_parameters)
  File "/Users/devpatel/Desktop/projects/.venv/lib/python3.7/site-packages/azure/mgmt/apimanagement/operations/_api_operations.py", line 378, in create_or_update
    **operation_config
  File "/Users/devpatel/Desktop/projects/.venv/lib/python3.7/site-packages/azure/mgmt/apimanagement/operations/_api_operations.py", line 314, in _create_or_update_initial
    raise models.ErrorResponseException(self._deserialize, response)
azure.mgmt.apimanagement.models._models_py3.ErrorResponseException: One or more fields contain incorrect values:

Note: I am able to trigger api.delete() or api.get() operations successfully. Facing the error while trying to create a new one.

Environment Setup

ghost commented 4 years ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @miaojiang.

changlong-liu commented 4 years ago

Hi service team, would you have a look at this issue? I don't reproduce this in my side, and the message should comes from the service http response. Thank you.

devang93 commented 4 years ago

@changlong-liu Were you able to create an api using the api parameters listed above? Are those parameters looks good to you?

changlong-liu commented 4 years ago

Hi, @devang93 , I get ErrorResponseException with a different error message using the api parameters listed above. The error message is returned by the service side.

devang93 commented 4 years ago

@changlong-liu got it. I am trying to debug it by changing some parameters to None but still unable to figure out for which parameters its giving me this error. Waiting on the service team's response. Thanks

devang93 commented 4 years ago

@changlong-liu Any updates here? I am not sure how to reach out to the service team and we are blocked here.

KedarJoshi commented 4 years ago

@devang93 are you using actual OpenApiSpec in the Value field (instead of sample_test)? you can first try to do this using REST API https://docs.microsoft.com/en-us/rest/api/apimanagement/2019-12-01/apis/createorupdate

value= "sample_test", format="openapi"

devang93 commented 4 years ago

@KedarJoshi I didn't quite understand it. What is the expected value for "value" field?

I just commented the "value" field out and I was able to create the api in apim. However, it doesn't import all the operations automatically.

devang93 commented 4 years ago

@KedarJoshi after going through the rest api documentation I understand that "value" field requires api spec documentation.

I am passing the following values for a sample api but it still fails with the same error. The sample api in this example uses swagger specs.

 value= "https://conferenceapi.azurewebsites.net/",
 format="swagger-link-json"

I also tried to download the swaggerspec file and then pass that entire file string into value but it again failed with same error.

with open ("api_spec.yaml", "r") as specfile:
    api_spec=specfile.readlines()

...
 value= api_spec,
 format="swagger-json"
RupengLiu commented 4 years ago

just verified, please try this one:

value= "https://conferenceapi.azurewebsites.net/", format="openapi-link"

devang93 commented 4 years ago

@RupengLiu yes, it worked. I was also able to figure out another swagger-link-json as well. I figured out that the ConferenceApi I was trying out with was not swagger formatted.

My issue has been resolved but however, it would be nice if the error/exception given described which field is missing or have incorrect value. Following error doesn't explain much or help in debugging:

azure.mgmt.apimanagement.models._models_py3.ErrorResponseException: One or more fields contain incorrect values:
changlong-liu commented 4 years ago

Hi @devang93 , the detail reasons are in the response body of the apimanagement service response. It's too complex to show in one exception message.

Would you try this code:

try:
    deployed_api_response = apim_client.api.create_or_update( resource_group_name, apim_service_name, "test-api", api_create_or_update_parameters)
except Exception as e:
    print(e.response.content)   # all response body returned by service side.

You may find details information for the error.

lilyjma commented 3 years ago

Hi, this issue seems to be resolved, so I'll close it for now. If you still have more questions, please reopen. Thanks!