RicoSuter/NSwag · error · InvalidOperationException

PropertyNameGenerator not set.

Error message

PropertyNameGenerator not set.

What it means

ParameterModelBase requires the generator settings to carry a PropertyNameGenerator, which converts schema property names into language-valid property identifiers for the generated model. If settings is null or PropertyNameGenerator is unset, the constructor throws InvalidOperationException('PropertyNameGenerator not set.').

Solutions

  1. Set settings.PropertyNameGenerator, or create settings via the factory method (e.g. CSharpGeneratorBaseSettings.CreateWithDefaults / CSharpClientGeneratorSettings defaults).
  2. Use NSwagStudio or the nswag CLI document pipeline which populates defaults automatically.
  3. Check that you are not passing null settings into the generator constructor.

Example fix

// before
var settings = new CSharpClientGeneratorSettings();
var generator = new CSharpClientGenerator(document, settings);
// after
var settings = new CSharpClientGeneratorSettings();
settings.PropertyNameGenerator = (property) => ConversionUtilities.ConvertToUpperCamelCase(property.Name, true);
var generator = new CSharpClientGenerator(document, settings);
Defensive patterns

Strategy: type-guard

Validate before calling

if (settings == null || settings.PropertyNameGenerator == null)
    throw new InvalidOperationException("Initialize PropertyNameGenerator before constructing the parameter model.");

Type guard

bool HasPropertyNameGenerator(ClientGeneratorBaseSettings s) => s?.PropertyNameGenerator != null;

Try / catch

try { var generator = new CSharpClientGenerator(document, settings); }
catch (InvalidOperationException ex) when (ex.Message == "PropertyNameGenerator not set.") { logger.LogError(ex, "Generator settings were not fully initialized"); throw; }

Prevention

When it happens

Trigger: Constructing a client generator (CSharpClientGenerator/TypeScriptClientGenerator etc.) with settings lacking PropertyNameGenerator, or passing null settings, e.g. new CSharpClientGenerator(document, new ClientGeneratorBaseSettings { ... } without PropertyNameGenerator).

Common situations: Using NSwag programmatically (not via NSwagStudio/nswag CLI) and building settings objects manually; forgetting to initialize base settings classes; custom generators not inheriting default settings factories.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14). Data as JSON: /api/errors/3b1605b508734bc0. Report an issue: GitHub.

Appendix: source

Thrown at src/NSwag.CodeGeneration/Models/ParameterModelBase.cs:47

        /// <param name="allParameters">All parameters.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="generator">The client generator base.</param>
        /// <param name="typeResolver">The type resolver.</param>
        protected ParameterModelBase(string parameterName, string variableName, string typeName,
            OpenApiParameter parameter, IList<OpenApiParameter> allParameters, CodeGeneratorSettingsBase settings,
            IClientGenerator generator, TypeResolverBase typeResolver)
        {
            _allParameters = allParameters;
            _parameter = parameter;
            _settings = settings;
            _generator = generator;
            _typeResolver = typeResolver;

            Type = typeName;
            Name = parameterName;
            VariableName = variableName;

            var propertyNameGenerator = settings?.PropertyNameGenerator ?? throw new InvalidOperationException("PropertyNameGenerator not set.");

            _properties = _parameter.ActualSchema.ActualProperties
                .Select(p => new PropertyModel(p.Key, p.Value, propertyNameGenerator.Generate(p.Value)))
                .ToList();
        }

        /// <summary>Gets the type of the parameter.</summary>
        public string Type { get; }

        /// <summary>Gets the name.</summary>
        public string Name { get; }

        /// <summary>Gets the variable name in (usually lowercase).</summary>
        public string VariableName { get; }

        /// <summary>Gets a value indicating whether a default value is available.</summary>
        public bool HasDefault => Default != null;

View on GitHub (pinned to 63daf8fcc3)