Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to show deserialiser returns null #457

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions sdk/Pulumi.Tests/Mocks/Issue456.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2016-2021, Pulumi Corporation

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using OneOf.Types;
using Pulumi.Serialization;
using Pulumi.Testing;

namespace Pulumi.Tests.Mocks
{
/// <summary>
/// Supports testing that null returned for an InputMap doesn't cause a null reference exception.
///
/// See https://github.com/pulumi/pulumi-dotnet/issues/456
/// </summary>
public sealed class Issue456
{
public class ReproStack : Stack
{
[Output("result")]
public Output<string> Result { get; private set; }

public ReproStack()
{
var instance1 = new CustomMap("instance1");
var instance2 = new CustomMap("instance2");
// Assert that both instance1 and instance2 don't have null for Metadata.Labels

this.Result = Output.Tuple(instance1.Metadata, instance2.Metadata).Apply(tuple => {
if (tuple.Item1.Labels == null || tuple.Item2.Labels == null) {
throw new Exception("Labels should not be null");
}
return "success";
});
}
}

public class ReproMocks : IMocks
{
public Task<object> CallAsync(MockCallArgs args)
{
throw new Exception("CallAsync should not be called");
}

public Task<(string? id, object state)> NewResourceAsync(MockResourceArgs args)
{
if (args.Type != "pkg:index:CustomMap")
{
throw new Exception($"Unknown resource {args.Type}");
}

if (args.Name == "instance1") {
return Task.FromResult<(string?, object)>(
("some_id",
new Dictionary<string, object>
{
{
"metadata",
new Dictionary<string, object>
{
{ "labels", null! },
}
},
}
));
} else if (args.Name == "instance2") {
return Task.FromResult<(string?, object)>(
("some_id",
new Dictionary<string, object>
{
{
"metadata",
new Dictionary<string, object>
{
}
},
}
));
} else {
throw new Exception($"Unknown resource {args.Name}");
}
}
}
}
}
7 changes: 7 additions & 0 deletions sdk/Pulumi.Tests/Mocks/MocksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ public async Task TestAliases()
}
}
}

[Fact]
public async Task TestNullMaps()
{
await Deployment.TestAsync<Issue456.ReproStack>(
new Issue456.ReproMocks());
}
}

public static class Testing
Expand Down
26 changes: 26 additions & 0 deletions sdk/Pulumi.Tests/Mocks/TestStack.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2016-2020, Pulumi Corporation

using System.Collections.Immutable;

namespace Pulumi.Tests.Mocks
{
[ResourceType("aws:ec2/instance:Instance", null)]
Expand Down Expand Up @@ -47,4 +49,28 @@ public MyStack()
this.PublicIp = myInstance.PublicIp;
}
}

[OutputType]
public sealed class ObjectMeta
{
public readonly ImmutableDictionary<string, string> Labels;

[OutputConstructor]
private ObjectMeta(ImmutableDictionary<string, string> labels){
Labels = labels;
}
}


public class CustomMap : CustomResource
{
[Output("metadata")]
public Output<ObjectMeta> Metadata { get; private set; } = null!;

public CustomMap(string name, CustomResourceOptions? options = null)
: base("pkg:index:CustomMap", name, null, options)
{
}
}

}
2 changes: 1 addition & 1 deletion sdk/Pulumi/Core/InputMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static implicit operator InputMap<V>(Output<Dictionary<string, V>> values
public static implicit operator InputMap<V>(Output<IDictionary<string, V>> values)
=> values.Apply(ImmutableDictionary.CreateRange);

public static implicit operator InputMap<V>(Output<ImmutableDictionary<string, V>> values)
public static implicit operator InputMap<V>(Output<ImmutableDictionary<string, V>>values)
=> new InputMap<V>(values.Apply(values =>
{
var builder = ImmutableDictionary.CreateBuilder<string, Input<V>>();
Expand Down
Loading