Skip to content

[fix-finder] Enable nullable reference types in MamJsonToXml.cs #11452

@github-actions

Description

@github-actions

Problem

src/Xamarin.Android.Build.Tasks/MamJsonToXml.cs does not opt into nullable reference types and uses the null! operator on line 19, which violates repository conventions.

Location

  • File: src/Xamarin.Android.Build.Tasks/MamJsonToXml.cs

Current Code

using System;
using System.IO;

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Android.Build.Tasks;

namespace Xamarin.Android.Tasks
{
	public class MamJsonToXml : AndroidTask
	{
		public  override    string  TaskPrefix  => "A2C";

		[Required]
		public  ITaskItem[] MappingFiles        { get; set; } = [];

		[Required]
		public  ITaskItem   XmlMappingOutput    { get; set; } = null!; // NRT - guarded by [Required]

		public override bool RunTask ()
		{
			var parser = new MamJsonParser (this.CreateTaskLogger ());
			foreach (var file in MappingFiles) {
				parser.Load (file.ItemSpec);
			}
			var tree   = parser.ToXml ();
			using (var o = File.CreateText (XmlMappingOutput.ItemSpec)) {
				o.WriteLine (tree.ToString ());
			}
			return true;
		}
	}
}

Suggested Fix

  1. Add #nullable enable as the very first line (no preceding blank lines).
  2. Replace null! on the XmlMappingOutput property. Since ITaskItem is a reference type with [Required], use a helper or throw pattern. The simplest compliant approach: declare a backing field initialized to a dummy and use ArgumentNullException.ThrowIfNull in RunTask before use, OR use Microsoft.Android.Build.Tasks.TaskItemStub.Default if available. If no stub exists, the recommended pattern for [Required] ITaskItem in this repo is:
[Required]
public  ITaskItem   XmlMappingOutput    { get; set; } = new TaskItem ();

This follows the same pattern as [Required] string properties using = "" — it provides a non-null default that MSBuild will overwrite at runtime.

Guidelines

  • Never use ! (null-forgiving operator) — this is a hard rule in the repo
  • Space before ( and [ (Mono style): File.CreateText (path), array [0]
  • Use tabs for indentation
  • [Required] properties: non-nullable with a sensible default (= "", = [], or = new TaskItem ())
  • Add using Microsoft.Build.Utilities; if TaskItem is not already imported (it is already imported in this file)

Acceptance Criteria

  • #nullable enable is the first line of the file
  • No null! operator anywhere in the file
  • XmlMappingOutput has a non-null default value
  • File compiles without nullable warnings
  • All tests pass
  • No new warnings introduced

Fix-finder metadata

  • Script: 01-nullable-reference-types
  • Score: 28/30 (actionability: 10, safety: 8, scope: 10)

Generated by Nightly Fix Finder for issue #11451 · ● 6.9M ·

  • expires on May 29, 2026, 3:26 PM UTC

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions