Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem in swagger output using empty Uri template #7

Closed
hermanlindner opened this issue Apr 5, 2016 · 20 comments
Closed

Problem in swagger output using empty Uri template #7

hermanlindner opened this issue Apr 5, 2016 · 20 comments

Comments

@hermanlindner
Copy link

When I have a method like this:

    [OperationContract]
    [SwaggerWcfPath("Gets a set of translations", "Creates a translation")]
    [WebGet(
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = ""
        )]
    TranslationSet Get();

I see that it is shown in Swagger like this:

/gw/translations/Get

I did expect: /gw/translations to be the path. This is also the path i give to call this functionality.

likewise with Post:

How can I use swagger wcf to get or post on the main svc without sub path

@abelsilva
Copy link
Owner

Can you check if this would solve your problem?
https://gist.github.com/abelsilva/786daf059ce32246865f5ef9ce19bcfe

putting translations in the UriTemplate of the methods

@hermanlindner
Copy link
Author

That should work.
Only i have multiple endpoints in multiple Svc files.
I did plan to separate the different endpoints and have each endpoint in a different class.
Is it hard to make the empty uri path for swagger to mean post on parent.
Because that is also how iis handles the uri paths.
Your solution does bring that in line but I had hoped to use a different svc file for every endpoint.

Maybe i can cut u[p the words to mkae it happen :)

like trans to delegate to TRanslations.Svc and then havel "lations" in the method path.
I do wonder if iis works with that.

@hermanlindner
Copy link
Author

nope does not work.

i do need a slash in between trans and lations now.

get /gw/trans/lations

@hermanlindner
Copy link
Author

One other issue is that although i have a nullable field and put required=false in the Swagger parameter attribute it still shows the parameter to be required.

@abelsilva
Copy link
Owner

Would it work if you changed like this?

on Global.asax:

new ServiceRoute("gw", [...]

to

new ServiceRoute("gw/trans",

and on Service:

[SwaggerWcf("/gw")]

to

[SwaggerWcf("/gw/trans")]

and also the methods

UriTemplate = "/translations"

to

UriTemplate = "/lations",

@abelsilva
Copy link
Owner

I'll check what implication it might have to remove the method name when UriTemplate is empty

It makes sense to not have the method name

@abelsilva
Copy link
Owner

I'll also check that issue with required

thanks

@abelsilva
Copy link
Owner

about the required issue, can you show me the function signature?

If I use

        public Book SpecialFunc(string id, Book[] books)

I get books required

If I use

        public Book SpecialFunc(string id, Book[] books = null)

books are not required

@hermanlindner
Copy link
Author

Hi abelsilva,

Here it goes.

This is in the interface:

[SwaggerWcfPath("By culture and page", "Retrieves a translation by culture and page")]
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/cult/{culture}/page/{page}?reftime={reftime}"
)]
TranslationSet GetCultPage(
[SwaggerWcfParameter(true,"The culture")]string culture,
[SwaggerWcfParameter(true, "The page")]string page,
[SwaggerWcfParameter(false, "The last time we updated the translations")]long reftime);

This is implementation (svc.cs) :

[SwaggerWcf("/gw/translations")]
public class TranslationService:ITranslationService

[SwaggerWcfTag("translations")]
    public TranslationSet GetCultPage(

         [SwaggerWcfParameter(true, "The culture")]string culture,
          [SwaggerWcfParameter(true, "The page")]string page,
          [SwaggerWcfParameter(false, "The last time we updated the translations")]long reftime)
    {
        ............
    }


        i know that long is not nullable so that it would be strange for optional field.
        But it seems to work and when i call without query parameters it just gets 0 as reftime.

this is the base path:

/gw/translations/cult/{culture}/page/{page}

And then in swagger output page I see all 3 fields
with all 3 having required in the value column.

@hermanlindner
Copy link
Author

oh yeah,
And nullable long is not allowed as query parameter, but i do not have a problem with using a string value.

If i say string and use =null then it will make it an optional parameter?

@hermanlindner
Copy link
Author

if i change it to:

 [SwaggerWcfTag("translations")]
    public TranslationSet GetCultPage(

         [SwaggerWcfParameter(true, "The culture")]string culture,
          [SwaggerWcfParameter(true, "The page")]string page,
          [SwaggerWcfParameter(false, "The last time we updated the translations")]string reftime=null)

I still see required in the fields of swagger.

@abelsilva
Copy link
Owner

ok, I was checking this issue with body parameters

now I see the issue you have, it makes sense, it's a bug, I'll check it

thanks

@abelsilva abelsilva reopened this Apr 8, 2016
@abelsilva
Copy link
Owner

If I have


        [SwaggerWcfPath("By culture and page", "Retrieves a translation by culture and page")]
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "/cult/{culture}/page/{page}?reftime={reftime}"
            )]
        [SwaggerWcfTag("translations")]
        public Book GetCultPage(
            [SwaggerWcfParameter(true, "The culture")] string culture,
            [SwaggerWcfParameter(true, "The page")] string page,
            [SwaggerWcfParameter(false, "The last time we updated the translations")] long reftime = 0)
        {
            WebOperationContext woc = WebOperationContext.Current;

            if (woc == null)
                return null;

            Book book = Store.Books.FirstOrDefault();
            if (book != null)
            {
                woc.OutgoingResponse.StatusCode = HttpStatusCode.OK;
                return book;
            }

            woc.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
            return null;
        }

I get this in swagger (field not required):
image

@abelsilva
Copy link
Owner

make sure you are using V 0.1.4

@abelsilva
Copy link
Owner

if I change the function to

public Book GetCultPage(
            [SwaggerWcfParameter(true, "The culture")] string culture,
            [SwaggerWcfParameter(true, "The page")] string page,
            [SwaggerWcfParameter(false, "The last time we updated the translations")] string reftime = null)

I also see the reftime as not required

@hermanlindner
Copy link
Author

Hi Abelsilva,

Thank you so much for improving the swagger functionality.

It is great that now it is working with the empty Uri templates.

This makes the interface so much better.

But alas, the fields are still all required in my solution.

I do not understand because I have the exact same parameters
and attibutes of the exact same type

And my swagger still says they are required.

@abelsilva
Copy link
Owner

are you able to paste the full interface and class?

you may omit the contents of the functions

@hermanlindner
Copy link
Author

Dear abelsilva,

Thank you so much for your help and quick response and fix.

I got swagger working now the way i hoped it would.

Thank you so much.

@hermanlindner
Copy link
Author

The required stuff is working alos :)

@abelsilva
Copy link
Owner

thanks!

nice to ear it is working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants