Swashbuckle, Swagger Pattern and Format annotation or XML comment
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
add a comment |
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
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
add a comment |
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
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
json asp.net-web-api swagger-ui swagger-2.0 swashbuckle
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
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
add a comment |
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>
1
I think that will show in the example not in the pattern keyword
– Helder Sepulveda
Nov 16 '18 at 14:16
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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>
1
I think that will show in the example not in the pattern keyword
– Helder Sepulveda
Nov 16 '18 at 14:16
add a comment |
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>
1
I think that will show in the example not in the pattern keyword
– Helder Sepulveda
Nov 16 '18 at 14:16
add a comment |
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>
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>
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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