Skip to content

Generating models using the VS extension can cause namespace issues #228

@tompipe

Description

@tompipe

When using the VS extension and the .mb file to generate models, the extension generates namespaces using the .mb file's RootNamespace and a relative namespace generated from the filesystem folders.
https://github.com/zpqrtbnk/Zbu.ModelsBuilder/blob/12aec7920ea45da5af0d1271197620cbdcb48267/src/ZpqrtBnk.ModelsBuilder.Extension/Generator.cs#L70-L78

This generated namespace is sent through to the API to build models. Although the namespace can be overridden with either the ModelsNamespaceAttribute or the Umbraco.ModelsBuilder.ModelsNamespace appsettings value, it appears there is a case where this isn't being used, and is falling back to the namespace passed to the API by the extension.

I think the issue is caused by the Builder class which passes through the namespace passed in from the VS extension through to the MapModelTypes method.
https://github.com/zpqrtbnk/Zbu.ModelsBuilder/blob/bf0da79aee8f52ec153bbb200131d1914ee95b2b/src/Umbraco.ModelsBuilder/Building/Builder.cs#L115

https://github.com/zpqrtbnk/Zbu.ModelsBuilder/blob/bf0da79aee8f52ec153bbb200131d1914ee95b2b/src/Umbraco.ModelsBuilder/Building/TypeModel.cs#L212-L223

This results in models being generated with the namespace passed in from the VS extension, which could become invalid if the attribute, or appsettings value modifies the namespace, e.g:

image

[assembly: ModelsNamespace("Website.Core.Models")]
namespace Website.Core.Models
{
  /// <summary>Homepage</summary>
  [PublishedModel("homepage")]
  public partial class Homepage : PublishedContentModel
  {
    // snip

    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.1.0")]
    [ImplementPropertyType("footerSocialMediaLinks")]
    public IEnumerable<Website.Core.Models.Generated.SocialMediaLinksSchema> FooterSocialMediaLinks => this.Value<IEnumerable<Website.Core.Models.Generated.SocialMediaLinksSchema>>("footerSocialMediaLinks");
  }
}

When this is what was expected

[assembly: ModelsNamespace("Website.Core.Models")]
namespace Website.Core.Models
{
  /// <summary>Homepage</summary>
  [PublishedModel("homepage")]
  public partial class Homepage : PublishedContentModel
  {
    // snip

    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.1.0")]
    [ImplementPropertyType("footerSocialMediaLinks")]
    public IEnumerable<SocialMediaLinksSchema> FooterSocialMediaLinks => this.Value<IEnumerable<SocialMediaLinksSchema>>("footerSocialMediaLinks");
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions