Azure / autorest

OpenAPI (f.k.a Swagger) Specification code generator. Supports C#, PowerShell, Go, Java, Node.js, TypeScript, Python
MIT License
4.61k stars 737 forks source link

Failure trying to generate C# classes from Json file #1833

Closed galvesribeiro closed 7 years ago

galvesribeiro commented 7 years ago

I'm trying to use AutoRest to generate C# classes from https://docs.docker.com/engine/api/v1.26/swagger.yaml

I converted it to Json using http://editor.swagger.io and the converted file is attached (renamed to txt). swagger.txt

Code generation failed.
AutoRest.exe -Namespace Docker.DotNet.Models -Input swagger.json
error: [FATAL] Error parsing swagger file. Error reading string. Unexpected token: StartObject. Path 'default', line 19, position 15.

Can someone tell me what is wrong with it? Other tools like NSwag is able to run it perfectly.

Thank you!

galvesribeiro commented 7 years ago

Anyone?

olydis commented 7 years ago

Damn, I had an answer written up the day you filed this issue, but apparently I didn't post it... sorry for that. As far as I can remember, your Swagger contains a lot of features that AutoRest just doesn't support (yet). I see:

I think there was quite some more, I can investigate further if AutoRest still won't behave.

galvesribeiro commented 7 years ago

Thanks for get back @olydis.

Do you have any idea if it may be supported in a near future? I plan to change the way other Microsoft project generate code (Microsoft/Docker.Dotnet).

Docker specs for Swagger is 100% compatible with Swagger spec.

I may help and contribute it here, but I would need some guidance as I have no clue on AutoRest codebase.

Thank you!

olydis commented 7 years ago

@galvesribeiro Great to hear those plans! Whether we support those things in a near future is hard to say, currently we have other priorities. :-/ Contributions would of course be great, however be warned: We are going through some fundamental architectural changes this month so it might be worth awaiting those to prevent the frustration of learning about stuff right before it's being changed.

@fearthecowboy any suggestions?

fearthecowboy commented 7 years ago

Wow.

This is a great swagger doc. We're going to have to use this for some test cases. This thing is full of cases that the current modeler has no ability to handle 😄

Notably:

At the very least, we should not crash on those cases, so we'll queue them up to get fixed PDQ.

WRT the arrays of types.... we may have to hack some really dirty workaround to make that work. Possibly it to skip codegen for that and handle that case manually.

G

galvesribeiro commented 7 years ago

Yeah, I can't agree more that some stuff on Swagger spec are really dirty for us used to have type-safety and formal contracts but think that people who write that is used to untyped languages like JavaScript, so it make total sense for that to have a property that sometimes is of a type, sometimes is another.

I was about to start from scratch a generator based on YamlDotnet + Json.Net + Roslyn but I unfortunately had no time for that and the guys from Microsoft/Docker.DotNetty pointed me to AutoRest as a solution.

I hope you guys can get that sorted so we can use it soon :(

fearthecowboy commented 7 years ago

I'd highly recommend that you not try to write from scratch... AutoRest is a great example on how that's a lot more work that it looks 🗻

I'll see if we can't get fixes in to fix it pretty quick.

galvesribeiro commented 7 years ago

Yes, definitively... I did tried to start it and it take a lot of work :)

olydis commented 7 years ago

added features required for this to https://github.com/Azure/autorest/issues/2448 if they were not already there.

galvesribeiro commented 7 years ago

@olydis does it work with the swagger doc I pasted here?

olydis commented 7 years ago

no, while we're actually working on some of the features now, it probably won't work, yet. Note that #2448 is a collection of feature requests, which we created since we had countless similar/related issues and it got super hard to keep track of what people want - this issue right here is similar, things you encounter were essentially asked for before, so I just reference this issue from #2448 to make sure we review it (but also all related issues) when tackling the underlying problem.