Skip to content

Commit

Permalink
MS Graph type provider warns on property mismatch (#15824)
Browse files Browse the repository at this point in the history
Updating Ms Graph type loader to warn on property mismatch instead of
error

microsoftgraph/msgraph-bicep-types#191
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/15824)
  • Loading branch information
jason-dou authored Dec 10, 2024
1 parent c5137bd commit c730403
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Bicep.Core.UnitTests/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.TypeSystem.Providers;
using Bicep.Core.TypeSystem.Providers.Az;
using Bicep.Core.TypeSystem.Providers.MicrosoftGraph;
using Bicep.Core.TypeSystem.Types;
using Bicep.Core.UnitTests.Configuration;
using Bicep.Core.UnitTests.Features;
Expand Down Expand Up @@ -129,6 +130,12 @@ public static IServiceCollection WithAzResourceTypeLoaderFactory(this IServiceCo
public static IServiceCollection WithAzResourceProvider(this IServiceCollection services, IAzResourceProvider azResourceProvider)
=> Register(services, azResourceProvider);

public static IServiceCollection WithMsGraphResourceTypeLoaderFactory(this IServiceCollection services, MicrosoftGraphResourceTypeLoader loader)
{
var provider = new MicrosoftGraphResourceTypeProvider(loader);
return Register(services, TestTypeHelper.CreateResourceTypeLoaderFactory(provider));
}

public static IServiceCollection WithArmClientProvider(this IServiceCollection services, IArmClientProvider armClientProvider)
=> Register(services, armClientProvider);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.Core.Diagnostics;
using Bicep.Core.Extensions;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.TypeSystem.Providers.MicrosoftGraph;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
Expand All @@ -13,6 +16,7 @@ namespace Bicep.Core.UnitTests.TypeSystem.MicrosoftGraph
[TestClass]
public class MicrosoftGraphResourceTypeProviderTests
{
private static ServiceBuilder Services => new();

[TestMethod]
public void MicrosoftGraphResourceTypeProvider_can_list_all_types_without_throwing()
Expand All @@ -26,5 +30,37 @@ public void MicrosoftGraphResourceTypeProvider_can_list_all_types_without_throwi
// verify there aren't any duplicates
availableTypes.Select(x => x.FormatName().ToLowerInvariant()).Should().OnlyHaveUniqueItems();
}

[TestMethod]
public void MsGraphResourceTypeProvider_should_warn_for_property_mismatch()
{
Compilation createCompilation(string program) => Services
.WithMsGraphResourceTypeLoader(new MicrosoftGraphResourceTypeLoader())
.BuildCompilation(program);

var compilation = createCompilation(@"
resource app 'Microsoft.Graph/applications@beta' = {
uniqueName: 'test'
displayName: 'test'
extraProp: 'extra'
}
");
compilation.Should().HaveDiagnostics(new[] {
("BCP037", DiagnosticLevel.Warning, "The property \"extraProp\" is not allowed on objects of type \"Microsoft.Graph/applications\". Permissible properties include \"api\", \"appRoles\", \"authenticationBehaviors\", \"defaultRedirectUri\", \"dependsOn\", \"description\", \"disabledByMicrosoftStatus\", \"groupMembershipClaims\", \"identifierUris\", \"info\", \"isDeviceOnlyAuthSupported\", \"isFallbackPublicClient\", \"keyCredentials\", \"logo\", \"notes\", \"optionalClaims\", \"parentalControlSettings\", \"passwordCredentials\", \"publicClient\", \"requestSignatureVerification\", \"requiredResourceAccess\", \"samlMetadataUrl\", \"serviceManagementReference\", \"servicePrincipalLockConfiguration\", \"signInAudience\", \"spa\", \"tags\", \"tokenEncryptionKeyId\", \"verifiedPublisher\", \"web\", \"windows\". If this is a resource type definition inaccuracy, report it using https://aka.ms/bicep-type-issues.")
});

compilation = createCompilation(@"
resource app 'Microsoft.Graph/applications@beta' = {
uniqueName: 'test'
displayName: 'test'
spa: {
extraNestedProp: 'extra'
}
}
");
compilation.Should().HaveDiagnostics(new[] {
("BCP037", DiagnosticLevel.Warning, "The property \"extraNestedProp\" is not allowed on objects of type \"MicrosoftGraphSpaApplication\". Permissible properties include \"redirectUris\". If this is a resource type definition inaccuracy, report it using https://aka.ms/bicep-type-issues.")
});
}
}
}
4 changes: 4 additions & 0 deletions src/Bicep.Core.UnitTests/Utils/ServiceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.TypeSystem.Providers;
using Bicep.Core.TypeSystem.Providers.MicrosoftGraph;
using Bicep.Core.TypeSystem.Types;
using Bicep.Core.UnitTests.Features;
using Bicep.Core.Workspaces;
Expand Down Expand Up @@ -61,6 +62,9 @@ public static ServiceBuilder WithAzResourceTypeLoader(this ServiceBuilder servic
public static ServiceBuilder WithEmptyAzResources(this ServiceBuilder serviceBuilder)
=> serviceBuilder.WithRegistration(x => x.WithEmptyAzResources());

public static ServiceBuilder WithMsGraphResourceTypeLoader(this ServiceBuilder serviceBuilder, MicrosoftGraphResourceTypeLoader typeLoader)
=> serviceBuilder.WithRegistration(x => x.WithMsGraphResourceTypeLoaderFactory(typeLoader));

public static ServiceBuilder WithEnvironmentVariables(this ServiceBuilder serviceBuilder, params (string key, string? value)[] variables)
=> serviceBuilder.WithRegistration(x => x.WithEnvironmentVariables(variables));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ private ObjectType ToCombinedType(IEnumerable<KeyValuePair<string, Azure.Bicep.T

private static TypeSymbolValidationFlags GetValidationFlags(bool isResourceBodyType)
{
var flags = TypeSymbolValidationFlags.WarnOnPropertyTypeMismatch;
if (isResourceBodyType)
{
// strict validation on top-level resource properties, as 'custom' top-level properties are not supported by the platform
return TypeSymbolValidationFlags.Default;
return flags | TypeSymbolValidationFlags.Default;
}

// in all other places, we should allow some wiggle room so that we don't block compilation if there are any swagger inaccuracies
return TypeSymbolValidationFlags.WarnOnTypeMismatch;
return flags | TypeSymbolValidationFlags.WarnOnTypeMismatch;
}

private static ResourceFlags ToResourceFlags(Azure.Bicep.Types.Concrete.ResourceFlags input)
Expand Down

0 comments on commit c730403

Please sign in to comment.