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

Generate server side asp.net (c#) code? #1469

Closed
bagusflyer opened this issue Oct 29, 2015 · 33 comments
Closed

Generate server side asp.net (c#) code? #1469

bagusflyer opened this issue Oct 29, 2015 · 33 comments

Comments

@bagusflyer
Copy link

I'm wondering if it's possible to generate server side asp.net code based on the swagger json or yaml file? Thanks.

@wing328
Copy link
Contributor

wing328 commented Oct 30, 2015

@bagusflyer currently we do not have server generator for C# (WebApi). Do you have cycle to help implement it based on other server generator (e.g. JAX-RS, Sinatra, PHP Silex, etc) ?

@wing328 wing328 added this to the Future milestone Nov 6, 2015
@NahumLitvin
Copy link

Unfortunatly I have no time to contribute nowadays but I am joining the request.

@wing328
Copy link
Contributor

wing328 commented Nov 16, 2015

The beta version of the C# WebApi generator can be as simple as the following to start with:

  1. Community implements getPetById of Petstore in a WebApi project
  2. Swagger core team converts the files in the WebApi project back into mustache template.

Then we'll implement other methods (POST, PUT, DELETE) one by one.

@jimschubert
Copy link
Contributor

Also commenting on this to join the request.

@NahumLitvin
Copy link

I discovered https://github.com/domaindrivendev/Swashbuckle
it fits my needs.

@wing328
Copy link
Contributor

wing328 commented Jan 25, 2016

@NahumLitvin I believe Swashbuckle is for generating OpenAPI/Swagger spec and integration with other Swagger components (e.g. swagger-ui).

The request here is to generate the server stub in C# Web Api given an OpenAPI/Swagger spec.

@nin9creative
Copy link

So what exactly are you looking for as a start? A complete ASP. net WebAPI project?

I may be able to help with that. I came here looking for the capability as we are moving to a design first approach, and highly leverage WebAPI at the company I'm working at.

@wing328
Copy link
Contributor

wing328 commented Jan 30, 2016

@nin9creative thanks for offering help. Yes, ideally a ASP.net WebApi that implements the following endpoints to start with:

Then we will construct the mustache template based on that.

@jimschubert
Copy link
Contributor

@nin9creative @wing328 I've started on two examples using .net 4.5 and asp.net 5. https://github.com/jimschubert/webapi-swagger-samples

@wing328
Copy link
Contributor

wing328 commented Jan 30, 2016

@jimschubert I think that's what we need as a starting point 🍻

Are you working on the generator for WebApi as well? (just want to avoid duplicated efforts)

I'm working on automatically generating test cases for C# API client so I won't have time until I finish that.

@jimschubert
Copy link
Contributor

There are still some things I need to work out.

The one project is .net 4.5 because mono doesn't do 4.5.2 and I kept getting errors on 4.5.1 and 4.6. Even then, the project doesn't run correctly through xsp4. I was going to see if there's a Windows 10 IoT container I can use. If not, I figured a successful compile would be enough verification.

I think the swagger attributes for the asp.net 5 project can just be pulled from the other source. To make them equivalent, I would need to set up Swagger to pull from xml comments.

Then I was going to create an integration test project like I have here for the asp.net 5 verification tool.

@jimschubert
Copy link
Contributor

@wing328 to answer your other question, no I hadn't yet started on the generator stuff.

@wing328
Copy link
Contributor

wing328 commented Jan 30, 2016

@jimschubert that's ok :)

whoever has cycle to start working on the webapi generator please reply to let the community know to avoid duplicated efforts.

@jimschubert
Copy link
Contributor

I have a little time today, I think I'll start on the ASP.NET 5 web api server generator.

@jimschubert
Copy link
Contributor

I've committed my work in progress. https://github.com/jimschubert/swagger-codegen/tree/csharp_webapi_aspnet5_server

May not have a lot of time this week, but don't want the code to get lost/forgotten.

@wing328
Copy link
Contributor

wing328 commented Feb 4, 2016

To all, thanks to contribution from @jimschubert we now have server stub generator for ASP.NET5.

Please pull the latest and give it a try. If you want to help improve the generator, please refer to #2024 for a list of open tasks that are pending community contribution.

@wing328 wing328 closed this as completed Feb 4, 2016
@akrowczyk
Copy link

@jimschubert How much work would be required to get a ASP.NET WebAPI codegen working on the .NET 4.6 Framework?

I am not familar with the codegen aspects, but see that you did implement another service stub in .NET for < ASP.NET 5.

Our use cases here are more slanted towards enterprise web api's on the current .NET 4.6 framework and stack.

@jimschubert
Copy link
Contributor

@andrewkrowczyk

I think it would be relatively easy to create a single Web API generator that could target .NET 4.5.2 and .NET 4.6. I think the csharp, aspnet5, and CsharpDotNet2 generators should probably be abstracted to a common base type before adding another one specifically for Web API. I was planning to work on that next so there's not a lot of duplicated code.

The models for Web API in .NET 4.6 should be identical to those in ASP.NET 5. You could create an empty Web API project, then generate the models and controllers only.

# bash command, outputs to temp/webapi46 under user directory
java -Dapis -Dmodels -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar \
generate -i http://petstore.swagger.io/v2/swagger.json \
-l aspnet5 -o ~/temp/webapi46
:: Windows output to C:\temp\webapi46
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar
set JAVA_OPTS=%JAVA_OPTS% -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties
set ags=generate -t modules\swagger-codegen\src\main\resources\aspnet5 -i modules\swagger-codegen\src\test\resources\2_0\petstore.json -l aspnet5 -o C:\temp\webapi46

java %JAVA_OPTS% -jar %executable% %ags%

This would get you pretty far. I think you'd need to change Controller to ApiController in the generated APIs, and maybe add a reference to System.Web.Http.

@wing328
Copy link
Contributor

wing328 commented Feb 9, 2016

@jimschubert @andrewkrowczyk what about adding CLI options to so support .NET 4.5.2 and .NET 4.6 in the aspnet5 generator?

@jimschubert
Copy link
Contributor

@wing328 I was also thinking about maybe renaming it to aspnet and supporting 4.x as libraries.

@akrowczyk
Copy link

I may have some time to help out this week if there are specifics that need work....

-------- Original Message --------
From: Jim Schubert [email protected]
Date: Mon, February 08, 2016 8:53 PM -0600
To: swagger-api/swagger-codegen [email protected]
CC: "Krowczyk, Andrew" [email protected]
Subject: Re: [swagger-codegen] Generate server side asp.net (c#) code? (#1469)

@wing328https://github.com/wing328 I was also thinking about maybe renaming it to aspnet and supporting 4.x as libraries.

Reply to this email directly or view it on GitHubhttps://github.com//issues/1469#issuecomment-181679275.

This email is for the use of the intended recipient(s) only. If you have received this email in error, please notify the sender immediately and then delete it. If you are not the intended recipient, you must not keep, use, disclose, copy or distribute this email without the author's prior permission. We have taken precautions to minimize the risk of transmitting software viruses, but we advise you to carry out your own virus checks on any attachment to this message. We cannot accept liability for any loss or damage caused by software viruses. The information contained in this communication may be confidential and may be subject to the attorney-client privilege. If you are the intended recipient and you do not wish to receive similar electronic messages from us in the future then please respond to the sender to this effect.

@wing328
Copy link
Contributor

wing328 commented Feb 9, 2016

@andrewkrowczyk if it's not too much work, can you please modify the existing PetStore ASP.NET server stub to make it work with .NET 4.5.2/.NET 4.6?

Ref: https://github.com/swagger-api/swagger-codegen/tree/master/samples/server/petstore/aspnet5

Then we will have a better idea on what needs to be modified to make the generator works with different .NET frameworks and start the reverse engineering.

@nin9creative
Copy link

Ok will take a look.
Andrew
From: wing328 [email protected]
To: swagger-api/swagger-codegen [email protected]
Cc: Andrew [email protected]
Sent: Monday, February 8, 2016 9:23 PM
Subject: Re: [swagger-codegen] Generate server side asp.net (c#) code? (#1469)

@andrewkrowczyk if it's not too much work, can you please modify the existing PetStore ASP.NET server stub to make it work with .NET 4.5.2/.NET 4.6?Ref: https://github.com/swagger-api/swagger-codegen/tree/master/samples/server/petstore/aspnet5Then we will have a better idea on what needs to be modified to make the generator works with different .NET frameworks and start the reverse engineering.—
Reply to this email directly or view it on GitHub.

@jimschubert
Copy link
Contributor

@andrewkrowczyk if you're implementing these changes, please be aware of pull #2062 in which the file structure of AbstractCSharpCodegen will have changed.

edit: Sorry, I meant AspNet5ServerCodegen.

@wing328
Copy link
Contributor

wing328 commented Feb 10, 2016

@andrewkrowczyk FYI. #20162 has been merged into master. Please pull the latest master and let us know if you've any questions.

@akrowczyk
Copy link

From what I see, the two solutions for generating ASPNET5 vs the earlier versions will be a bit different overall - especially around the .NET solution and project file. Also, at work I'm almost thinking that as a go forward approach we'd be better off targeting the new ASPNET5 project structure and framework. All that to say, maybe flushing out the ASPNET5 version first would be better for the long term...

@zh-wowtv
Copy link

@jimschubert Your fork for swagger-codegen will support ASP.net server stub code generation?

@wing328
Copy link
Contributor

wing328 commented Jul 29, 2016

@zh-wowtv the ASP.NET generator has been merged into master. You can find it in the v2.2.0 stable release or the latest master.

(there's also NancyFx generator that you may find useful)

@zh-wowtv
Copy link

@wing328 Great. Thank you so much.

@bmarshalsea
Copy link

bmarshalsea commented Aug 11, 2016

@wing328 thanks to everyone involved in this.
At my current employment we're currently looking at how we move forward with our documentation and API's and it's become a bit of a fight between Swagger and RAML. I'm heavily pushing some Swagger around, and this c# generator has been a bomb shell onto the RAML supporters.

@wing328
Copy link
Contributor

wing328 commented Aug 11, 2016

@bmarshalsea thanks :)

Credits to @jimschubert who has done most of the work for the ASP.NET core generator.

(I believe you're also aware of the NancyFX generator for C# developers)

If you or your team has any questions on swagger codegen during the evaluation, please let us know by opening a ticket

@wing328 wing328 modified the milestones: v2.2.1, Future, v2.2.0 Aug 11, 2016
@atbe
Copy link

atbe commented Jun 22, 2017

Just a quick question regarding the asp 4.5 support, was that ever merged in? I don't see the option and currently we can only generate .net core projects which is very limiting given it isn't compatible with any asp.net 4.X projects (for referencing).

@jimschubert had an example of the pet store working with asp .net 4.5 and @wing328 recommended adding cli options to toggle between the different versions of asp.net but I don't see any of that documented here.

@jimschubert
Copy link
Contributor

@atbe We decided to include only ASP.NET Core as a server generator. This was mostly because I thought maintaining non-core would be more difficult (MVC, Web Forms, and WebAPI). Core standardizes on things like DI and tooling, and supports the inclusion of .NET Framework artifacts (see https://docs.microsoft.com/en-us/dotnet/standard/net-standard).

Would a configurable .NET Standard version allow you to consume those artifacts in .NET Core, or do you specifically need that additional Web framework support?

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

No branches or pull requests

9 participants