Skip to content

Commit 057dab2

Browse files
liuwuliuyunvcolin7
andauthored
Fix azureterraform_azapi_get to return 400 BadRequest for invalid resource type or API version (#2657)
* Enhance error handling in AzApiDocsService and update status code in AzApiDocsGetCommandTests * Add changelog entry for azapi_get 400 BadRequest fix * Change `GetResourceApiVersions.ResourceVisitor` in BicepSchema tools to throw `InvalidDataException` for unknown resource types (#2840) Change `GetResourceApiVersions.ResourceVisitor` in the BicepSchema tools to throw `InvalidDataException` for unknown resource types `GetResourceApiVersions` threw a bare `Exception` (and an unhandled `KeyNotFoundException` when the provider existed but the resource type did not), which `AzApiDocsService` normalized via a broad catch-all that also misclassified real server-side faults as `400 BadRequest`. Throw `InvalidDataException` at the source instead, consistent with `LoadSingleResource`, and drop the catch-all so unexpected exceptions surface as `500`. --------- Co-authored-by: vcolin7 <victor.y.asi@gmail.com>
1 parent a7d3655 commit 057dab2

5 files changed

Lines changed: 16 additions & 5 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changes:
2+
- section: "Bugs Fixed"
3+
description: "Fixed `azureterraform_azapi_get` returning `500 InternalServerError` for invalid resource type or API version inputs. The tool now returns `400 BadRequest` with an actionable message, enabling agents to auto-correct and retry with valid inputs."

tools/Azure.Mcp.Tools.AzureTerraform/src/Commands/AzApiDocsGetCommand.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System.IO;
45
using System.Net;
56
using Azure.Mcp.Tools.AzureTerraform.Models;
67
using Azure.Mcp.Tools.AzureTerraform.Options;
@@ -55,6 +56,12 @@ protected override AzApiDocsOptions BindOptions(ParseResult parseResult)
5556
};
5657
}
5758

59+
protected override HttpStatusCode GetStatusCode(Exception ex) => ex switch
60+
{
61+
InvalidDataException => HttpStatusCode.BadRequest,
62+
_ => base.GetStatusCode(ex)
63+
};
64+
5865
public override async Task<CommandResponse> ExecuteAsync(
5966
CommandContext context,
6067
ParseResult parseResult,

tools/Azure.Mcp.Tools.AzureTerraform/tests/Azure.Mcp.Tools.AzureTerraform.Tests/AzApiDocsGetCommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public async Task ExecuteAsync_ServiceThrows_HandlesException()
136136

137137
var response = await ExecuteCommandAsync("--resource-type", "Microsoft.Fake/nonexistent");
138138

139-
Assert.NotEqual(HttpStatusCode.OK, response.Status);
139+
Assert.Equal(HttpStatusCode.BadRequest, response.Status);
140140
}
141141

142142
[Theory]

tools/Azure.Mcp.Tools.BicepSchema/src/Services/ResourceProperties/ResourceVisitor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ public string[] GetResourceApiVersions(string resourceTypeName)
8686
{
8787
(string providerName, _, _) = ResourceParser.ParseResourceType(resourceTypeName);
8888

89-
if (!GetAllResourceTypesAndVersionsByProvider().TryGetValue(providerName, out ProviderResourceTypes? provider))
89+
if (!GetAllResourceTypesAndVersionsByProvider().TryGetValue(providerName, out ProviderResourceTypes? provider)
90+
|| !provider.ResourceTypes.TryGetValue(resourceTypeName.ToLowerInvariant(), out UniqueResourceType? uniqueResourceType))
9091
{
91-
throw new Exception($"Resource type {resourceTypeName} not found.");
92+
throw new InvalidDataException($"Resource type {resourceTypeName} not found.");
9293
}
9394

94-
return [.. provider.ResourceTypes[resourceTypeName.ToLowerInvariant()].ApiVersions];
95+
return [.. uniqueResourceType.ApiVersions];
9596
}
9697

9798
public TypesDefinitionResult LoadSingleResource(string resourceTypeName, string apiVersion)

tools/Azure.Mcp.Tools.BicepSchema/tests/Azure.Mcp.Tools.BicepSchema.Tests/GetSchemaTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void GetResourcePropertySchema_ShouldThrowOnNotFound()
3333
SchemaGenerator.ConfigureServices(serviceCollection);
3434
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
3535

36-
var exception = Assert.Throws<Exception>(() =>
36+
var exception = Assert.Throws<InvalidDataException>(() =>
3737
{
3838
TypesDefinitionResult result = SchemaGenerator.GetResourceTypeDefinitions(serviceProvider, "Microsoft.Unknown/virtualRandom");
3939
_ = SchemaGenerator.GetResponse(result);

0 commit comments

Comments
 (0)