How to define default values for parameters for the Swagger UI? The Next CEO of Stack OverflowSwagger default value for parameterHow to set two default fields to my input-model on ASP .Net webAPi .Net Core?How do I calculate someone's age in C#?How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to loop through all enum values in C#?What is a NullReferenceException, and how do I fix it?Why not inherit from List<T>?Swagger default value for parameterCan I use Swashbuckle with my own OpenAPI Specification Json?

What did the word "leisure" mean in late 18th Century usage?

Free fall ellipse or parabola?

Raspberry pi 3 B with Ubuntu 18.04 server arm64: what pi version

How can I separate the number from the unit in argument?

How seriously should I take size and weight limits of hand luggage?

Compilation of a 2d array and a 1d array

Why does freezing point matter when picking cooler ice packs?

Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico

Salesforce opportunity stages

Is it a bad idea to plug the other end of ESD strap to wall ground?

Read/write a pipe-delimited file line by line with some simple text manipulation

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

How can the PCs determine if an item is a phylactery?

Is it possible to make a 9x9 table fit within the default margins?

Avoiding the "not like other girls" trope?

Compensation for working overtime on Saturdays

How to show a landlord what we have in savings?

How to pronounce fünf in 45

Does int main() need a declaration on C++?

Is a distribution that is normal, but highly skewed, considered Gaussian?

Is it correct to say moon starry nights?

What difference does it make matching a word with/without a trailing whitespace?

How do I keep Mac Emacs from trapping M-`?

How dangerous is XSS



How to define default values for parameters for the Swagger UI?



The Next CEO of Stack OverflowSwagger default value for parameterHow to set two default fields to my input-model on ASP .Net webAPi .Net Core?How do I calculate someone's age in C#?How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to loop through all enum values in C#?What is a NullReferenceException, and how do I fix it?Why not inherit from List<T>?Swagger default value for parameterCan I use Swashbuckle with my own OpenAPI Specification Json?










0















I have Swagger/Swashbuckle integrated into a .NET Core 2.2 API project. Everything works great and what I am asking is purely for convenience. Consider the following API method:



public Model SomeEstimate(SomeRequest request) 
return Manager.GetSomeEstimate(request);

...
public class SomeRequest
public string StreetAddress get; set;
public string Zip get; set;



When I hit /swagger/index.html and want to try out this API, I always have to enter the StreetAddress and Zip values.



Is there a way to provide default values for StreetAddress and Zip? This answer suggested placing [DefaultValue("value here")] attribute each property of the SomeRequest class. It may have worked for the regular .NET, but not with .NET Core.



Is it possible to provide default values for parameters for the Swagger UI?










share|improve this question

















  • 1





    See this link: github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/… .You would have to create a schema filter to achieve the DefaultValue attribute working in .NET Core

    – Rahul Sharma
    Mar 7 at 19:39












  • @RahulSharma Excellent, that did the trick!

    – AngryHacker
    Mar 7 at 20:10











  • Glad to hear that @AngryHacker. Shoud I post this as the answer to your question?

    – Rahul Sharma
    Mar 7 at 20:14











  • @RahulSharma Do it so it's visible to whoever else has this question.

    – AngryHacker
    Mar 7 at 20:18















0















I have Swagger/Swashbuckle integrated into a .NET Core 2.2 API project. Everything works great and what I am asking is purely for convenience. Consider the following API method:



public Model SomeEstimate(SomeRequest request) 
return Manager.GetSomeEstimate(request);

...
public class SomeRequest
public string StreetAddress get; set;
public string Zip get; set;



When I hit /swagger/index.html and want to try out this API, I always have to enter the StreetAddress and Zip values.



Is there a way to provide default values for StreetAddress and Zip? This answer suggested placing [DefaultValue("value here")] attribute each property of the SomeRequest class. It may have worked for the regular .NET, but not with .NET Core.



Is it possible to provide default values for parameters for the Swagger UI?










share|improve this question

















  • 1





    See this link: github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/… .You would have to create a schema filter to achieve the DefaultValue attribute working in .NET Core

    – Rahul Sharma
    Mar 7 at 19:39












  • @RahulSharma Excellent, that did the trick!

    – AngryHacker
    Mar 7 at 20:10











  • Glad to hear that @AngryHacker. Shoud I post this as the answer to your question?

    – Rahul Sharma
    Mar 7 at 20:14











  • @RahulSharma Do it so it's visible to whoever else has this question.

    – AngryHacker
    Mar 7 at 20:18













0












0








0








I have Swagger/Swashbuckle integrated into a .NET Core 2.2 API project. Everything works great and what I am asking is purely for convenience. Consider the following API method:



public Model SomeEstimate(SomeRequest request) 
return Manager.GetSomeEstimate(request);

...
public class SomeRequest
public string StreetAddress get; set;
public string Zip get; set;



When I hit /swagger/index.html and want to try out this API, I always have to enter the StreetAddress and Zip values.



Is there a way to provide default values for StreetAddress and Zip? This answer suggested placing [DefaultValue("value here")] attribute each property of the SomeRequest class. It may have worked for the regular .NET, but not with .NET Core.



Is it possible to provide default values for parameters for the Swagger UI?










share|improve this question














I have Swagger/Swashbuckle integrated into a .NET Core 2.2 API project. Everything works great and what I am asking is purely for convenience. Consider the following API method:



public Model SomeEstimate(SomeRequest request) 
return Manager.GetSomeEstimate(request);

...
public class SomeRequest
public string StreetAddress get; set;
public string Zip get; set;



When I hit /swagger/index.html and want to try out this API, I always have to enter the StreetAddress and Zip values.



Is there a way to provide default values for StreetAddress and Zip? This answer suggested placing [DefaultValue("value here")] attribute each property of the SomeRequest class. It may have worked for the regular .NET, but not with .NET Core.



Is it possible to provide default values for parameters for the Swagger UI?







c# .net .net-core swagger swashbuckle






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 19:33









AngryHackerAngryHacker

27.9k76242452




27.9k76242452







  • 1





    See this link: github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/… .You would have to create a schema filter to achieve the DefaultValue attribute working in .NET Core

    – Rahul Sharma
    Mar 7 at 19:39












  • @RahulSharma Excellent, that did the trick!

    – AngryHacker
    Mar 7 at 20:10











  • Glad to hear that @AngryHacker. Shoud I post this as the answer to your question?

    – Rahul Sharma
    Mar 7 at 20:14











  • @RahulSharma Do it so it's visible to whoever else has this question.

    – AngryHacker
    Mar 7 at 20:18












  • 1





    See this link: github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/… .You would have to create a schema filter to achieve the DefaultValue attribute working in .NET Core

    – Rahul Sharma
    Mar 7 at 19:39












  • @RahulSharma Excellent, that did the trick!

    – AngryHacker
    Mar 7 at 20:10











  • Glad to hear that @AngryHacker. Shoud I post this as the answer to your question?

    – Rahul Sharma
    Mar 7 at 20:14











  • @RahulSharma Do it so it's visible to whoever else has this question.

    – AngryHacker
    Mar 7 at 20:18







1




1





See this link: github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/… .You would have to create a schema filter to achieve the DefaultValue attribute working in .NET Core

– Rahul Sharma
Mar 7 at 19:39






See this link: github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/… .You would have to create a schema filter to achieve the DefaultValue attribute working in .NET Core

– Rahul Sharma
Mar 7 at 19:39














@RahulSharma Excellent, that did the trick!

– AngryHacker
Mar 7 at 20:10





@RahulSharma Excellent, that did the trick!

– AngryHacker
Mar 7 at 20:10













Glad to hear that @AngryHacker. Shoud I post this as the answer to your question?

– Rahul Sharma
Mar 7 at 20:14





Glad to hear that @AngryHacker. Shoud I post this as the answer to your question?

– Rahul Sharma
Mar 7 at 20:14













@RahulSharma Do it so it's visible to whoever else has this question.

– AngryHacker
Mar 7 at 20:18





@RahulSharma Do it so it's visible to whoever else has this question.

– AngryHacker
Mar 7 at 20:18












1 Answer
1






active

oldest

votes


















2














To define default values for parameters for Swagger UI in .NET Core, the following article defines a custom schema filter for your DefaultValue attribute in your Model class. The code shown below has been taken from this article and is purely to inform anyone else who has this question or has been facing a similar problem:



Decorate the desired properties in a model:



public class Test 
[DefaultValue("Hello")]
public string Text get; set;



Main filter:



using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Project.Swashbuckle

public class SchemaFilter : ISchemaFilter

public void Apply(Schema schema, SchemaFilterContext context)
if (schema.Properties == null)
return;


foreach (PropertyInfo propertyInfo in context.SystemType.GetProperties())

// Look for class attributes that have been decorated with "[DefaultAttribute(...)]".
DefaultValueAttribute defaultAttribute = propertyInfo
.GetCustomAttribute<DefaultValueAttribute>();

if (defaultAttribute != null)
foreach (KeyValuePair<string, Schema> property in schema.Properties)

// Only assign default value to the proper element.
if (ToCamelCase(propertyInfo.Name) == property.Key)
property.Value.Example = defaultAttribute.Value;
break;






private string ToCamelCase(string name)
return char.ToLowerInvariant(name[0]) + name.Substring(1);





And finally registering it to your Swagger Options(In Startup.cs):



services.AddSwaggerGen(c => 
// ...
c.SchemaFilter<SchemaFilter>();
);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051497%2fhow-to-define-default-values-for-parameters-for-the-swagger-ui%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    To define default values for parameters for Swagger UI in .NET Core, the following article defines a custom schema filter for your DefaultValue attribute in your Model class. The code shown below has been taken from this article and is purely to inform anyone else who has this question or has been facing a similar problem:



    Decorate the desired properties in a model:



    public class Test 
    [DefaultValue("Hello")]
    public string Text get; set;



    Main filter:



    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Reflection;
    using Swashbuckle.AspNetCore.Swagger;
    using Swashbuckle.AspNetCore.SwaggerGen;

    namespace Project.Swashbuckle

    public class SchemaFilter : ISchemaFilter

    public void Apply(Schema schema, SchemaFilterContext context)
    if (schema.Properties == null)
    return;


    foreach (PropertyInfo propertyInfo in context.SystemType.GetProperties())

    // Look for class attributes that have been decorated with "[DefaultAttribute(...)]".
    DefaultValueAttribute defaultAttribute = propertyInfo
    .GetCustomAttribute<DefaultValueAttribute>();

    if (defaultAttribute != null)
    foreach (KeyValuePair<string, Schema> property in schema.Properties)

    // Only assign default value to the proper element.
    if (ToCamelCase(propertyInfo.Name) == property.Key)
    property.Value.Example = defaultAttribute.Value;
    break;






    private string ToCamelCase(string name)
    return char.ToLowerInvariant(name[0]) + name.Substring(1);





    And finally registering it to your Swagger Options(In Startup.cs):



    services.AddSwaggerGen(c => 
    // ...
    c.SchemaFilter<SchemaFilter>();
    );





    share|improve this answer



























      2














      To define default values for parameters for Swagger UI in .NET Core, the following article defines a custom schema filter for your DefaultValue attribute in your Model class. The code shown below has been taken from this article and is purely to inform anyone else who has this question or has been facing a similar problem:



      Decorate the desired properties in a model:



      public class Test 
      [DefaultValue("Hello")]
      public string Text get; set;



      Main filter:



      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Reflection;
      using Swashbuckle.AspNetCore.Swagger;
      using Swashbuckle.AspNetCore.SwaggerGen;

      namespace Project.Swashbuckle

      public class SchemaFilter : ISchemaFilter

      public void Apply(Schema schema, SchemaFilterContext context)
      if (schema.Properties == null)
      return;


      foreach (PropertyInfo propertyInfo in context.SystemType.GetProperties())

      // Look for class attributes that have been decorated with "[DefaultAttribute(...)]".
      DefaultValueAttribute defaultAttribute = propertyInfo
      .GetCustomAttribute<DefaultValueAttribute>();

      if (defaultAttribute != null)
      foreach (KeyValuePair<string, Schema> property in schema.Properties)

      // Only assign default value to the proper element.
      if (ToCamelCase(propertyInfo.Name) == property.Key)
      property.Value.Example = defaultAttribute.Value;
      break;






      private string ToCamelCase(string name)
      return char.ToLowerInvariant(name[0]) + name.Substring(1);





      And finally registering it to your Swagger Options(In Startup.cs):



      services.AddSwaggerGen(c => 
      // ...
      c.SchemaFilter<SchemaFilter>();
      );





      share|improve this answer

























        2












        2








        2







        To define default values for parameters for Swagger UI in .NET Core, the following article defines a custom schema filter for your DefaultValue attribute in your Model class. The code shown below has been taken from this article and is purely to inform anyone else who has this question or has been facing a similar problem:



        Decorate the desired properties in a model:



        public class Test 
        [DefaultValue("Hello")]
        public string Text get; set;



        Main filter:



        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Reflection;
        using Swashbuckle.AspNetCore.Swagger;
        using Swashbuckle.AspNetCore.SwaggerGen;

        namespace Project.Swashbuckle

        public class SchemaFilter : ISchemaFilter

        public void Apply(Schema schema, SchemaFilterContext context)
        if (schema.Properties == null)
        return;


        foreach (PropertyInfo propertyInfo in context.SystemType.GetProperties())

        // Look for class attributes that have been decorated with "[DefaultAttribute(...)]".
        DefaultValueAttribute defaultAttribute = propertyInfo
        .GetCustomAttribute<DefaultValueAttribute>();

        if (defaultAttribute != null)
        foreach (KeyValuePair<string, Schema> property in schema.Properties)

        // Only assign default value to the proper element.
        if (ToCamelCase(propertyInfo.Name) == property.Key)
        property.Value.Example = defaultAttribute.Value;
        break;






        private string ToCamelCase(string name)
        return char.ToLowerInvariant(name[0]) + name.Substring(1);





        And finally registering it to your Swagger Options(In Startup.cs):



        services.AddSwaggerGen(c => 
        // ...
        c.SchemaFilter<SchemaFilter>();
        );





        share|improve this answer













        To define default values for parameters for Swagger UI in .NET Core, the following article defines a custom schema filter for your DefaultValue attribute in your Model class. The code shown below has been taken from this article and is purely to inform anyone else who has this question or has been facing a similar problem:



        Decorate the desired properties in a model:



        public class Test 
        [DefaultValue("Hello")]
        public string Text get; set;



        Main filter:



        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Reflection;
        using Swashbuckle.AspNetCore.Swagger;
        using Swashbuckle.AspNetCore.SwaggerGen;

        namespace Project.Swashbuckle

        public class SchemaFilter : ISchemaFilter

        public void Apply(Schema schema, SchemaFilterContext context)
        if (schema.Properties == null)
        return;


        foreach (PropertyInfo propertyInfo in context.SystemType.GetProperties())

        // Look for class attributes that have been decorated with "[DefaultAttribute(...)]".
        DefaultValueAttribute defaultAttribute = propertyInfo
        .GetCustomAttribute<DefaultValueAttribute>();

        if (defaultAttribute != null)
        foreach (KeyValuePair<string, Schema> property in schema.Properties)

        // Only assign default value to the proper element.
        if (ToCamelCase(propertyInfo.Name) == property.Key)
        property.Value.Example = defaultAttribute.Value;
        break;






        private string ToCamelCase(string name)
        return char.ToLowerInvariant(name[0]) + name.Substring(1);





        And finally registering it to your Swagger Options(In Startup.cs):



        services.AddSwaggerGen(c => 
        // ...
        c.SchemaFilter<SchemaFilter>();
        );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 20:23









        Rahul SharmaRahul Sharma

        653416




        653416





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051497%2fhow-to-define-default-values-for-parameters-for-the-swagger-ui%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

            Алба-Юлія

            Захаров Федір Захарович