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

Support header parameters for CSharp controller code generation #2033

Merged

Conversation

rogersillito
Copy link
Contributor

@rogersillito rogersillito commented Mar 20, 2019

Work in progress - this fixes #1956 (and possibly #642?). I've not looked at test coverage yet, and have done a limited range of manual testing.

Essentially I'm creating a FromHeaderAttribute that handles binding of header-based parameters. I've only tested in a framework web project so far, but in theory this should happily co-exist with the one available in ASP.NET Core for now. Looking at the discussion around how Core should be supported with the templates, I wonder whether this approach might be sufficient until that is resolved; maybe then only including the custom attribute/binding when the proposed IsAspNetCore setting is false?

@RicoSuter
Copy link
Owner

I think it's imporant to first fix the web api vs asp.net core story and then generate which works for each framework... it seems to me that these custom attributes are web api only, right? There should be a check so that they are only generated for web api...

@rogersillito
Copy link
Contributor Author

I was working on the assumption that the HttpParameterBinding/ParameterBindingAttribute base classes were still available in Core (but having just tested my code in a Core project, I realise they are not). As such my assumption this would still happily work in Core was wrong.

Saying that, if we had a way to tell the template if we're using Core it would be pretty straightforward to switch the behaviour: this works fine using the built-in FromHeaderAttribute. (We could do a string comparison against the AspNetNamespace setting in the template, but that wouldn't be nice..!).

@rogersillito
Copy link
Contributor Author

If you did decide to drop support for Web API, I wondered about making use of custom templates as a workaround for my use case...

I've just been exploring the "Template Directory" setting (following your comment): providing an alternative version of Controller.liquid works as I would expect (despite the caution to only override empty/extension templates?). The only thing I would specifically need from this PR would be the extra properties on CSharpParameterModel and ParameterModelBase: the built-in template could then be fixed to work for Core only - and remove the custom attribute.

@RicoSuter
Copy link
Owner

Sorry for the delay, see #1965 (comment)

Will ping here when it’s ready

@RicoSuter
Copy link
Owner

Added PR with new ControllerTarget setting: #2064

@rogersillito
Copy link
Contributor Author

Great - thanks for your work on this @RicoSuter . I'll hopefully get my PR revised over the next week or so!

…ntroller generation (now we have IsAspNetCore available in template). Added test coverage.
@rogersillito rogersillito marked this pull request as ready for review April 2, 2019 13:14
@rogersillito
Copy link
Contributor Author

rogersillito commented May 15, 2019

@RicoSuter: having figured out the concerns around multiple controller generation I've made a few changes to this PR in line with your comments:

  • added a new template that gets run once all controllers have been generated: the custom FromHeaderAttribute code for AspNet gets inserted when it's needed (i.e. only if there are header parameters)
  • replaced the previous using statements with full namespace prefixing where required
  • added tests that cover the above (and use the GetSwaggerDocument() setup approached introduced by another contributor)

rogersillito added a commit to rogersillito/OpenBankingApi.Generation that referenced this pull request May 15, 2019
…change controller generation option - now groups related actions together in separate controllers. Replaced current single implementation class with 4 separate ones. Controller.liquid template now largely follows default (@ v12.2.5) - except implemenation interfaces are renamed. Relies on changes to NSwag submitted here: RicoSuter/NSwag#2033
@RicoSuter
Copy link
Owner

I've updated the code to use code artifacts (used for multiple file output later)

@rogersillito
Copy link
Contributor Author

rogersillito commented May 16, 2019

Having digested your changes it looks like that approach fits much better with the general approach of the code: I hadn't fully appreciated the CodeArtifact concept. The danger of focusing exclusively on the fix at hand without an overall view on the codebase!

@RicoSuter
Copy link
Owner

I hadn't fully appreciated the CodeArtifact concept. The danger of focusing exclusively on the fix at hand without an overall view on the codebase!

No worries - this whole CodeArtifact was not really incorporated yet - I just took this as an opportunity to add it and change it everywhere...

Everything looks fine now except some additional line breaks etc. will merge soon. Is your feature still working as expected?

@rogersillito
Copy link
Contributor Author

rogersillito commented May 16, 2019

I'll give it another try if you've finished integrating - at the last try it looked good.

@RicoSuter
Copy link
Owner

Ok. Now everything should be ready... with your go I'll merge

@rogersillito
Copy link
Contributor Author

I've got it building, the tests are passing and my code gen project is running OK - so fine by me: thanks for you help!

@RicoSuter RicoSuter merged commit b6f3e0a into RicoSuter:master May 16, 2019
@RicoSuter
Copy link
Owner

v12.3.0

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

Successfully merging this pull request may close these issues.

Custom request header params(in: header) are treated as query parameters
2 participants