Swashbuckle, Swagger Pattern and Format annotation or XML comment










1















I'am trying to add the format and/or pattern keywords in my Swagger documentation. I'am using Swashbuckle and currenlty I'am using XML Comments to insert descriptions for my different fields. For example:
The controller:



/// <summary>
/// Shows a list with states
/// </summary>
/// <param name="id">The state ID</param>
/// <returns>Offers the state</returns>
[HttpGet("id", Name = "Get")]
public async Task<ActionResult<State>> GetState(string id)

State result = GetState(id);
return Ok(result);



I want the swagger documentation to show the pattern keyword with the regex expression:



'^[(]?[0-9A-F]8[-]?(?:[0-9A-F]4[-]?)3[0-9A-F]12[)]?$'


Is it possible to add this documentation while using XML comments or annotations. I'd like to ask the same for the format keyword. For example:
'format: date-time'



Are these things possible using XML comments or annotations with Swashbuckle to auto generate Swagger documentation or would this require a static swagger file?










share|improve this question






















  • Is your question just how do I put RegEx into my Xml Comments?

    – matt_lethargic
    Nov 15 '18 at 9:51











  • @matt_lethargic Yes I want to put a regex pattern into my Xml Comments so it would show up in my Swagger documentation file generated by Swashbuckle. At the moment the Description in <param name="id">The state ID</param> shows up but if I add <pattern> "regexstring" </patern> nothing happens.

    – vimes
    Nov 15 '18 at 9:57















1















I'am trying to add the format and/or pattern keywords in my Swagger documentation. I'am using Swashbuckle and currenlty I'am using XML Comments to insert descriptions for my different fields. For example:
The controller:



/// <summary>
/// Shows a list with states
/// </summary>
/// <param name="id">The state ID</param>
/// <returns>Offers the state</returns>
[HttpGet("id", Name = "Get")]
public async Task<ActionResult<State>> GetState(string id)

State result = GetState(id);
return Ok(result);



I want the swagger documentation to show the pattern keyword with the regex expression:



'^[(]?[0-9A-F]8[-]?(?:[0-9A-F]4[-]?)3[0-9A-F]12[)]?$'


Is it possible to add this documentation while using XML comments or annotations. I'd like to ask the same for the format keyword. For example:
'format: date-time'



Are these things possible using XML comments or annotations with Swashbuckle to auto generate Swagger documentation or would this require a static swagger file?










share|improve this question






















  • Is your question just how do I put RegEx into my Xml Comments?

    – matt_lethargic
    Nov 15 '18 at 9:51











  • @matt_lethargic Yes I want to put a regex pattern into my Xml Comments so it would show up in my Swagger documentation file generated by Swashbuckle. At the moment the Description in <param name="id">The state ID</param> shows up but if I add <pattern> "regexstring" </patern> nothing happens.

    – vimes
    Nov 15 '18 at 9:57













1












1








1








I'am trying to add the format and/or pattern keywords in my Swagger documentation. I'am using Swashbuckle and currenlty I'am using XML Comments to insert descriptions for my different fields. For example:
The controller:



/// <summary>
/// Shows a list with states
/// </summary>
/// <param name="id">The state ID</param>
/// <returns>Offers the state</returns>
[HttpGet("id", Name = "Get")]
public async Task<ActionResult<State>> GetState(string id)

State result = GetState(id);
return Ok(result);



I want the swagger documentation to show the pattern keyword with the regex expression:



'^[(]?[0-9A-F]8[-]?(?:[0-9A-F]4[-]?)3[0-9A-F]12[)]?$'


Is it possible to add this documentation while using XML comments or annotations. I'd like to ask the same for the format keyword. For example:
'format: date-time'



Are these things possible using XML comments or annotations with Swashbuckle to auto generate Swagger documentation or would this require a static swagger file?










share|improve this question














I'am trying to add the format and/or pattern keywords in my Swagger documentation. I'am using Swashbuckle and currenlty I'am using XML Comments to insert descriptions for my different fields. For example:
The controller:



/// <summary>
/// Shows a list with states
/// </summary>
/// <param name="id">The state ID</param>
/// <returns>Offers the state</returns>
[HttpGet("id", Name = "Get")]
public async Task<ActionResult<State>> GetState(string id)

State result = GetState(id);
return Ok(result);



I want the swagger documentation to show the pattern keyword with the regex expression:



'^[(]?[0-9A-F]8[-]?(?:[0-9A-F]4[-]?)3[0-9A-F]12[)]?$'


Is it possible to add this documentation while using XML comments or annotations. I'd like to ask the same for the format keyword. For example:
'format: date-time'



Are these things possible using XML comments or annotations with Swashbuckle to auto generate Swagger documentation or would this require a static swagger file?







json asp.net-web-api swagger-ui swagger-2.0 swashbuckle






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 9:11









vimesvimes

335




335












  • Is your question just how do I put RegEx into my Xml Comments?

    – matt_lethargic
    Nov 15 '18 at 9:51











  • @matt_lethargic Yes I want to put a regex pattern into my Xml Comments so it would show up in my Swagger documentation file generated by Swashbuckle. At the moment the Description in <param name="id">The state ID</param> shows up but if I add <pattern> "regexstring" </patern> nothing happens.

    – vimes
    Nov 15 '18 at 9:57

















  • Is your question just how do I put RegEx into my Xml Comments?

    – matt_lethargic
    Nov 15 '18 at 9:51











  • @matt_lethargic Yes I want to put a regex pattern into my Xml Comments so it would show up in my Swagger documentation file generated by Swashbuckle. At the moment the Description in <param name="id">The state ID</param> shows up but if I add <pattern> "regexstring" </patern> nothing happens.

    – vimes
    Nov 15 '18 at 9:57
















Is your question just how do I put RegEx into my Xml Comments?

– matt_lethargic
Nov 15 '18 at 9:51





Is your question just how do I put RegEx into my Xml Comments?

– matt_lethargic
Nov 15 '18 at 9:51













@matt_lethargic Yes I want to put a regex pattern into my Xml Comments so it would show up in my Swagger documentation file generated by Swashbuckle. At the moment the Description in <param name="id">The state ID</param> shows up but if I add <pattern> "regexstring" </patern> nothing happens.

– vimes
Nov 15 '18 at 9:57





@matt_lethargic Yes I want to put a regex pattern into my Xml Comments so it would show up in my Swagger documentation file generated by Swashbuckle. At the moment the Description in <param name="id">The state ID</param> shows up but if I add <pattern> "regexstring" </patern> nothing happens.

– vimes
Nov 15 '18 at 9:57












2 Answers
2






active

oldest

votes


















1














On the project there is an example how to use it for models:
https://github.com/domaindrivendev/Swashbuckle/blob/5489aca0d2dd7946f5569341f621f581720d4634/Swashbuckle.Dummy.Core/Controllers/MetadataAnnotatedTypesController.cs#L21



public class PaymentMetadata

[Required]
public decimal Amount get; set;

[Required, RegularExpression("^[3-6]?\d12,15$")]
public string CardNumber get; set;

[Required, Range(1, 12)]
public int ExpMonth get; set;

[Required, Range(14, 99)]
public int ExpYear get; set;

[StringLength(500, MinimumLength = 10)]
public string Note get; set;




For a primitive like in your case I'm not sure that is fully supported, you can try with something like:



public async Task<ActionResult<State>> GetState([RegularExpression("PATTERN")]string id)


If that does not work you can inject it using an IDocumentFilter






share|improve this answer























  • Thank you that worked. Adding it in the function parameter field worked to.

    – vimes
    Nov 16 '18 at 15:14











  • Great, unfortunately the version of the swagger-ui used in swashbuckle is not taking advantage of that, there was some movement to upgrade: github.com/domaindrivendev/Swashbuckle/pull/1132 but did not get any traction... I started my own project based on that: nuget.org/packages/Swagger-Net and I'm using the latest UI, try it if you have time.

    – Helder Sepulveda
    Nov 16 '18 at 17:24


















0














I just tried adding the regex into one of my projects that uses swagger and it worked fine



/// <param name="customerId">^[(]?[0-9A-F]8[-]?(?:[0-9A-F]4[-]?)3[0-9A-F]12[)]?$</param>





share|improve this answer


















  • 1





    I think that will show in the example not in the pattern keyword

    – Helder Sepulveda
    Nov 16 '18 at 14:16










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%2f53315885%2fswashbuckle-swagger-pattern-and-format-annotation-or-xml-comment%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














On the project there is an example how to use it for models:
https://github.com/domaindrivendev/Swashbuckle/blob/5489aca0d2dd7946f5569341f621f581720d4634/Swashbuckle.Dummy.Core/Controllers/MetadataAnnotatedTypesController.cs#L21



public class PaymentMetadata

[Required]
public decimal Amount get; set;

[Required, RegularExpression("^[3-6]?\d12,15$")]
public string CardNumber get; set;

[Required, Range(1, 12)]
public int ExpMonth get; set;

[Required, Range(14, 99)]
public int ExpYear get; set;

[StringLength(500, MinimumLength = 10)]
public string Note get; set;




For a primitive like in your case I'm not sure that is fully supported, you can try with something like:



public async Task<ActionResult<State>> GetState([RegularExpression("PATTERN")]string id)


If that does not work you can inject it using an IDocumentFilter






share|improve this answer























  • Thank you that worked. Adding it in the function parameter field worked to.

    – vimes
    Nov 16 '18 at 15:14











  • Great, unfortunately the version of the swagger-ui used in swashbuckle is not taking advantage of that, there was some movement to upgrade: github.com/domaindrivendev/Swashbuckle/pull/1132 but did not get any traction... I started my own project based on that: nuget.org/packages/Swagger-Net and I'm using the latest UI, try it if you have time.

    – Helder Sepulveda
    Nov 16 '18 at 17:24















1














On the project there is an example how to use it for models:
https://github.com/domaindrivendev/Swashbuckle/blob/5489aca0d2dd7946f5569341f621f581720d4634/Swashbuckle.Dummy.Core/Controllers/MetadataAnnotatedTypesController.cs#L21



public class PaymentMetadata

[Required]
public decimal Amount get; set;

[Required, RegularExpression("^[3-6]?\d12,15$")]
public string CardNumber get; set;

[Required, Range(1, 12)]
public int ExpMonth get; set;

[Required, Range(14, 99)]
public int ExpYear get; set;

[StringLength(500, MinimumLength = 10)]
public string Note get; set;




For a primitive like in your case I'm not sure that is fully supported, you can try with something like:



public async Task<ActionResult<State>> GetState([RegularExpression("PATTERN")]string id)


If that does not work you can inject it using an IDocumentFilter






share|improve this answer























  • Thank you that worked. Adding it in the function parameter field worked to.

    – vimes
    Nov 16 '18 at 15:14











  • Great, unfortunately the version of the swagger-ui used in swashbuckle is not taking advantage of that, there was some movement to upgrade: github.com/domaindrivendev/Swashbuckle/pull/1132 but did not get any traction... I started my own project based on that: nuget.org/packages/Swagger-Net and I'm using the latest UI, try it if you have time.

    – Helder Sepulveda
    Nov 16 '18 at 17:24













1












1








1







On the project there is an example how to use it for models:
https://github.com/domaindrivendev/Swashbuckle/blob/5489aca0d2dd7946f5569341f621f581720d4634/Swashbuckle.Dummy.Core/Controllers/MetadataAnnotatedTypesController.cs#L21



public class PaymentMetadata

[Required]
public decimal Amount get; set;

[Required, RegularExpression("^[3-6]?\d12,15$")]
public string CardNumber get; set;

[Required, Range(1, 12)]
public int ExpMonth get; set;

[Required, Range(14, 99)]
public int ExpYear get; set;

[StringLength(500, MinimumLength = 10)]
public string Note get; set;




For a primitive like in your case I'm not sure that is fully supported, you can try with something like:



public async Task<ActionResult<State>> GetState([RegularExpression("PATTERN")]string id)


If that does not work you can inject it using an IDocumentFilter






share|improve this answer













On the project there is an example how to use it for models:
https://github.com/domaindrivendev/Swashbuckle/blob/5489aca0d2dd7946f5569341f621f581720d4634/Swashbuckle.Dummy.Core/Controllers/MetadataAnnotatedTypesController.cs#L21



public class PaymentMetadata

[Required]
public decimal Amount get; set;

[Required, RegularExpression("^[3-6]?\d12,15$")]
public string CardNumber get; set;

[Required, Range(1, 12)]
public int ExpMonth get; set;

[Required, Range(14, 99)]
public int ExpYear get; set;

[StringLength(500, MinimumLength = 10)]
public string Note get; set;




For a primitive like in your case I'm not sure that is fully supported, you can try with something like:



public async Task<ActionResult<State>> GetState([RegularExpression("PATTERN")]string id)


If that does not work you can inject it using an IDocumentFilter







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 14:38









Helder SepulvedaHelder Sepulveda

4,03431230




4,03431230












  • Thank you that worked. Adding it in the function parameter field worked to.

    – vimes
    Nov 16 '18 at 15:14











  • Great, unfortunately the version of the swagger-ui used in swashbuckle is not taking advantage of that, there was some movement to upgrade: github.com/domaindrivendev/Swashbuckle/pull/1132 but did not get any traction... I started my own project based on that: nuget.org/packages/Swagger-Net and I'm using the latest UI, try it if you have time.

    – Helder Sepulveda
    Nov 16 '18 at 17:24

















  • Thank you that worked. Adding it in the function parameter field worked to.

    – vimes
    Nov 16 '18 at 15:14











  • Great, unfortunately the version of the swagger-ui used in swashbuckle is not taking advantage of that, there was some movement to upgrade: github.com/domaindrivendev/Swashbuckle/pull/1132 but did not get any traction... I started my own project based on that: nuget.org/packages/Swagger-Net and I'm using the latest UI, try it if you have time.

    – Helder Sepulveda
    Nov 16 '18 at 17:24
















Thank you that worked. Adding it in the function parameter field worked to.

– vimes
Nov 16 '18 at 15:14





Thank you that worked. Adding it in the function parameter field worked to.

– vimes
Nov 16 '18 at 15:14













Great, unfortunately the version of the swagger-ui used in swashbuckle is not taking advantage of that, there was some movement to upgrade: github.com/domaindrivendev/Swashbuckle/pull/1132 but did not get any traction... I started my own project based on that: nuget.org/packages/Swagger-Net and I'm using the latest UI, try it if you have time.

– Helder Sepulveda
Nov 16 '18 at 17:24





Great, unfortunately the version of the swagger-ui used in swashbuckle is not taking advantage of that, there was some movement to upgrade: github.com/domaindrivendev/Swashbuckle/pull/1132 but did not get any traction... I started my own project based on that: nuget.org/packages/Swagger-Net and I'm using the latest UI, try it if you have time.

– Helder Sepulveda
Nov 16 '18 at 17:24













0














I just tried adding the regex into one of my projects that uses swagger and it worked fine



/// <param name="customerId">^[(]?[0-9A-F]8[-]?(?:[0-9A-F]4[-]?)3[0-9A-F]12[)]?$</param>





share|improve this answer


















  • 1





    I think that will show in the example not in the pattern keyword

    – Helder Sepulveda
    Nov 16 '18 at 14:16















0














I just tried adding the regex into one of my projects that uses swagger and it worked fine



/// <param name="customerId">^[(]?[0-9A-F]8[-]?(?:[0-9A-F]4[-]?)3[0-9A-F]12[)]?$</param>





share|improve this answer


















  • 1





    I think that will show in the example not in the pattern keyword

    – Helder Sepulveda
    Nov 16 '18 at 14:16













0












0








0







I just tried adding the regex into one of my projects that uses swagger and it worked fine



/// <param name="customerId">^[(]?[0-9A-F]8[-]?(?:[0-9A-F]4[-]?)3[0-9A-F]12[)]?$</param>





share|improve this answer













I just tried adding the regex into one of my projects that uses swagger and it worked fine



/// <param name="customerId">^[(]?[0-9A-F]8[-]?(?:[0-9A-F]4[-]?)3[0-9A-F]12[)]?$</param>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 10:14









matt_lethargicmatt_lethargic

2,10511227




2,10511227







  • 1





    I think that will show in the example not in the pattern keyword

    – Helder Sepulveda
    Nov 16 '18 at 14:16












  • 1





    I think that will show in the example not in the pattern keyword

    – Helder Sepulveda
    Nov 16 '18 at 14:16







1




1





I think that will show in the example not in the pattern keyword

– Helder Sepulveda
Nov 16 '18 at 14:16





I think that will show in the example not in the pattern keyword

– Helder Sepulveda
Nov 16 '18 at 14:16

















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%2f53315885%2fswashbuckle-swagger-pattern-and-format-annotation-or-xml-comment%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto