diff --git a/src/Umbraco.Core/Configuration/Models/ModelsBuilderSettings.cs b/src/Umbraco.Core/Configuration/Models/ModelsBuilderSettings.cs index be86cf1f2b..127b7d9330 100644 --- a/src/Umbraco.Core/Configuration/Models/ModelsBuilderSettings.cs +++ b/src/Umbraco.Core/Configuration/Models/ModelsBuilderSettings.cs @@ -16,6 +16,7 @@ public class ModelsBuilderSettings internal const string StaticModelsDirectory = "~/umbraco/models"; internal const bool StaticAcceptUnsafeModelsDirectory = false; internal const int StaticDebugLevel = 0; + internal const bool StaticIncludeVersionNumberInGeneratedModels = true; private bool _flagOutOfDateModels = true; /// @@ -78,4 +79,16 @@ public class ModelsBuilderSettings /// 0 means minimal (safe on live site), anything else means more and more details (maybe not safe). [DefaultValue(StaticDebugLevel)] public int DebugLevel { get; set; } = StaticDebugLevel; + + /// + /// Gets or sets a value indicating whether the version number should be included in generated models. + /// + /// + /// By default this is written to the output in + /// generated code for each property of the model. This can be useful for debugging purposes but isn't essential, + /// and it has the causes the generated code to change every time Umbraco is upgraded. In turn, this leads + /// to unnecessary code file changes that need to be checked into source control. Default is true. + /// + [DefaultValue(StaticIncludeVersionNumberInGeneratedModels)] + public bool IncludeVersionNumberInGeneratedModels { get; set; } = StaticIncludeVersionNumberInGeneratedModels; } diff --git a/src/Umbraco.Infrastructure/ModelsBuilder/Building/TextBuilder.cs b/src/Umbraco.Infrastructure/ModelsBuilder/Building/TextBuilder.cs index 22160b0ef4..7484741b58 100644 --- a/src/Umbraco.Infrastructure/ModelsBuilder/Building/TextBuilder.cs +++ b/src/Umbraco.Infrastructure/ModelsBuilder/Building/TextBuilder.cs @@ -143,14 +143,17 @@ public class TextBuilder : Builder // // note that the blog post above clearly states that "Nor should it be applied at the type level if the type being generated is a partial class." // and since our models are partial classes, we have to apply the attribute against the individual members, not the class itself. - private static void WriteGeneratedCodeAttribute(StringBuilder sb, string tabs) => sb.AppendFormat( + private void WriteGeneratedCodeAttribute(StringBuilder sb, string tabs) => sb.AppendFormat( "{0}[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"Umbraco.ModelsBuilder.Embedded\", \"{1}\")]\n", - tabs, ApiVersion.Current.Version); + tabs, + Config.IncludeVersionNumberInGeneratedModels ? ApiVersion.Current.Version : null); // writes an attribute that specifies that an output may be null. // (useful for consuming projects with nullable reference types enabled) private static void WriteMaybeNullAttribute(StringBuilder sb, string tabs, bool isReturn = false) => - sb.AppendFormat("{0}[{1}global::System.Diagnostics.CodeAnalysis.MaybeNull]\n", tabs, + sb.AppendFormat( + "{0}[{1}global::System.Diagnostics.CodeAnalysis.MaybeNull]\n", + tabs, isReturn ? "return: " : string.Empty); private static string MixinStaticGetterName(string clrName) => string.Format("Get{0}", clrName);