title: NSwag autogenerated client does not show default values
body: <p>I have an api with Swagger enabled. In one of the endpoints, I have a request with some values set as default.</p>
  <pre><code>[DefaultValue(&quot;test value&quot;)]
  public string? Description { get; set; }
  </code></pre>
  <p>This looks fine in the Swagger page for the api. The default value is used in the Swagger example.</p>
  <p>We use NSwag to generate an api client which is consumed in the gateway. When I generate such a client, the request object looks like this:</p>
  <pre><code>[System.Text.Json.Serialization.JsonPropertyName(&quot;description&quot;)]
  public string? Description { get; set; } = &quot;test value&quot;;
  </code></pre>
  <p>Assigning the value have no effect on the Swagger definition in the gateway, and the value is therefore displayed as description : &quot;string&quot;</p>
  <p>How do I properly transfer the DefaultValue annotation to the autogenerated client, in such a way that I can reuse the request object and display the example with the test value string set?</p>
  <p>The swagger.json for the api looks like this for the description field:</p>
  <pre><code>&quot;properties&quot;: {
            &quot;description&quot;: {
              &quot;type&quot;: &quot;string&quot;,
              &quot;default&quot;: &quot;test value&quot;,
              &quot;nullable&quot;: true
            },
  
  Code based on the solution described below:
  </code></pre>
  <pre><code>public static string AddAttributesToParametersForNSwag(string source, OpenApiDocument doc)
          {
              Dictionary&lt;SyntaxNode, SyntaxNode&gt; dict = new Dictionary&lt;SyntaxNode, SyntaxNode&gt;();
              SyntaxTree tree = CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.LatestMajor, DocumentationMode.Parse, SourceCodeKind.Regular));
  
              var root = (CompilationUnitSyntax)tree.GetRoot();
  
              var classes = tree.GetRoot().DescendantNodes().OfType&lt;ClassDeclarationSyntax&gt;();
  
              if (classes.Count() == 0)
              {
                  return root.ToFullString();
              }
  
              foreach (var classDeclaration in classes.Skip(1))
              {
                  var className = classDeclaration.Identifier.ValueText;
                  var classParameters = classDeclaration.DescendantNodes().OfType&lt;PropertyDeclarationSyntax&gt;();
  
                  foreach (var classParameter in classParameters)
                  {
                      string classParameterName = classParameter.Identifier.ValueText;
  
                      //Try to find classname as schema in the swagger document
                      var scheme = doc.Components.Schemas.Where(s =&gt; s.Key.ToLower() == className.ToLower()).FirstOrDefault();
  
                      if (scheme.Key == null || scheme.Value == null)
                      {
                          continue;
                      }
  
                      //Check if schema has the property we're looking for
                      var swaggerProperty = scheme.Value.Properties.Where(p =&gt; p.Key.ToLower() == classParameterName.ToLower()).FirstOrDefault();
  
                      if (swaggerProperty.Key == null || swaggerProperty.Value == null)
                      {
                          continue;
                      }
  
                      //Nothing is specified for default value, so no attributes to add
                      if (swaggerProperty.Value.Default == null)
                      {
                          continue;
                      }
  
                      string attributeIdentifierName = string.Empty;
  
                      if (swaggerProperty.Value.Default != null)
                      {
                          if (swaggerProperty.Value.Default.AnyType == Microsoft.OpenApi.Any.AnyType.Null)
                          {
                              attributeIdentifierName = &quot;DefaultValue(null)&quot;;
                          }
                          else
                          {
                              //Additional types could be added here
                              continue;
                          }
                      }
  
                      var attributeAlreadyExist = classParameter.AttributeLists.Any(a =&gt; a.GetText().ToString().Contains(&quot;DefaultValue&quot;));
  
                      if (attributeAlreadyExist)
                      {
                          continue;
                      }
  
                      var parameterAttributes = classParameter.AttributeLists.Add(
                         SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(
                         SyntaxFactory.Attribute(SyntaxFactory.IdentifierName(attributeIdentifierName))
                      )));
  
                      dict.Add(classParameter, classParameter.WithAttributeLists(parameterAttributes));
                  }
              }
  
              return root.ReplaceNodes(dict.Keys, (original, rewritten) =&gt; dict[original]).NormalizeWhitespace().ToFullString();
          }
      }
  </code></pre>
  <pre><code>
  </code></pre>