question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model Properties - Default Values

See original GitHub issue

Hi, First of all, brilliant work. I’m new to Swagger and Swashbuckle made life very easy for us to integrate into our API, a few well written comments later and we’re good.

We are facing one minor issue in which our models assume default values which don’t look that appealing. See the image attached.

default-values

Is there a way to specify a default value for model properties? I have had no luck using default value attributes so far e.g.

[System.ComponentModel.DefaultValue("My Default String Value")]
public string Value { get; set; }

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
tarunkhannancommented, Apr 18, 2017

Is Swashbuckle available for EclipseEE?

3reactions
wongmcommented, Oct 13, 2017

This extension of @AndyPook’s code also flags optional parameters as "required": false in the Swagger file, as well as including the default value.

public class UpdateOptionalParamatersWithDefaultValues : IOperationFilter
{
	public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
	{
		if (operation.parameters == null)
			return;

		var parameterValuePairs =
			apiDescription.ActionDescriptor.GetParameters()
				.Where(parameter => parameter.DefaultValue != null)
				.ToDictionary(parameter => parameter.ParameterName, parameter => parameter.DefaultValue);

		foreach (var param in operation.parameters)
		{
			object defaultValue;
			if (parameterValuePairs.TryGetValue(param.name, out defaultValue))
			{
				param.@default = defaultValue;
				param.@required = false;
			}
		}
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - implementing default values in a Model
Assuming it's a POCO, I've always found the constructor is fine for establishing default values. I usually take that opportunity to declare ...
Read more >
Set Default Value to Property in C#
Here you will learn how to assign the default value to a property or auto-implemented property in a class.
Read more >
Property Default Values - SoftFluent
In the Business Object Model layer, the default value of a property is the initialization value that takes this property in its parent...
Read more >
DefaultValueAttribute Class (System.ComponentModel)
You can create a DefaultValueAttribute with any value. A member's default value is typically its initial value. A visual designer can use the...
Read more >
Using Spring @Value with Defaults
A quick and practical guide to setting default values when using the @Value annotation in Spring.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found