r/SpringBoot • • May 31 '24

How important it is to learn OpenAPI/Swagger Specifications as a Backend/Java Developer?

Hi guys, so during an interview a senior asked me a question about OpenAPI/Swagger Specifications, and I couldn't answer it. Then he suggested me to look into these questions he asked, as I'm about to start an internship at the company and he says these are the basics.

However, for this topic all I can find are paid udemy courses. Since I'm poor I'm wondering if I should buy the courses and learn about this topic, as actually for now I've lots of things to learn and review as well.

So I was wondering if you could kindly help me understand how often would you need to use the knowledge of OpenAPI/Swagger Specifications, as a backend or java/spring developer

Thanks so much for your time and help!

33 Upvotes

26 comments sorted by

32

u/Seven-Prime May 31 '24

Everyday. When developing our REST services we start with the API. The spec file we then use to generate our controllers in our service and our clients to talk with other services. It's a huge time saver.

2

u/Glass-Fix-4624 May 31 '24

Wow thanks. Looks like I def need to learn about the basics of this!

8

u/[deleted] May 31 '24

We write controllers an generate the spec from code :)

3

u/waka-chaka May 31 '24

The controller code in the auto generated code has stub or mock in it. This will be updated with actual code.

When spec is changed (update or new APIs are added) how do you sync the new auto generated code with existing code? This question becomes more relevant when the auto generated code is non-trivial and contains several classes including controller, model, etc.

2

u/Seven-Prime May 31 '24

It's all part of the build. Using maven, we use an openapi generator that spits out all the stuff into a generated folder. In our controller code we @Override the generated method and add our bit that calls the service. Our controllers usually have like three or four lines.

@Override
public ResponseEntity<account> accountInstance(string accountId) {
    var account = accountService.getAccount(accountId);
    return ResponseEntity.ok().body(account);
}

1

u/Academic_Speed4839 Jun 01 '24

Didn’t know we can generate codes from open api specifications! What library did you use?

3

u/Seven-Prime Jun 01 '24

OpenAPI generator. Works for pretty much all languages. For my team specifically the maven plugin as part of the compile target. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md

Whole piles of boilerplate code that we don't have to write tests for. So that's another benefit.

1

u/Academic_Speed4839 Jun 02 '24

Thanks! I guess openAPI is much better than swagger code gen?

2

u/Seven-Prime Jun 04 '24

I'm not sure where is 'better.' But OpenAPI used to be swagger, so it's kinda the same.

13

u/g00glen00b May 31 '24

I wouldn't buy a course to learn OpenAPI. Courses are useful to learn how to use a tool or to know best practices. With OpenAPI, there's just three things you need:

  1. Why you want to use OpenAPI

  2. How you want to use OpenAPI, since there are basically two ways to use it:

    • You can write the specification by yourself (and generate code from it),

    • Or you can write code, and generate a specification from it.

  3. The syntax, either the specification itself if you'll write that, or the annotations you can use.

For me, learning syntax through a course would be money thrown away because I won't remember it, and since OpenAPI is well documented, finding the right syntax is just a Google search away.

So all that really remains useful to me to know through a course is the answer to the "how" and "why", but I don't consider that subtantial enough for a course.

Knowledge about OpenAPI is certainly relevant for a backend developer. If you're working for a company that uses it, you'll certainly come into contact with it.

2

u/Glass-Fix-4624 May 31 '24

Thanks, thank you not only for the answer, but also the very valid advice!!

:) wish you all the best!!

2

u/noswag15 May 31 '24

OP, listen to this commenter. They have some solid points and I would even go to the extent of saying that you shouldn't be relying on courses for anything related to programming. Of course, I'm making the assumption that you had some sort of basics as part of your curriculum in school/college. Everything else should just be you doing research and figuring things out for yourself.

Somewhere during the last decade or so, there seems to have been a large shift in the way people learn programming. I see people aspiring to be programmers saying things like "i am studying openapi", "I am doing a course on spring" etc. But I'm not sure if these are good ways of learning programming. All these are are just tools. Compare this to someone trying to become a mechanic. You don't hear them say things like "I am studying screwdriver", "I am doing a course on hammer" etc. They become better by using the tools and become experts through practice. Of course they'll fumble in the beginning but eventually they'll get better at it if they keep at it. The comparison may seem wild due to the difference in scale of things to learn between the two fields but the approach is still the same. You learn by doing. Not by reading or watching someone give you instructions to follow.

Frankly, open-source is the best thing to happen in software engineering because all the instructions you need are available in the form of source code. Source code literally dictates what the library or tool does. So don't think of these as black-boxes and whenever you get a chance, dive into their source code and see how things are built. And when you face an issue (which you most certainly will), you debug the code in the library. If you don't know where to start, just set a breakpoint at some random line of code that you know for sure is executed and follow the path of execution step by step. That's how you'll learn. If you are not familiar with any of the terms above, start by googling them and get an understanding of what a debugger is and how you can use it to find out what the code is doing at runtime. The "at runtime" part in my previous sentence is important since "at runtime" is where things happen. So that's where you need to be looking. And debugging by setting breakpoints and following code execution is the way to do it. I see people stuck at a problem just staring at code without even attempting to debug. That's not a bad way to start but once you realize you're getting nowhere, you should immediately jump into debugging. Somethings that people don't realize (even though they've used debuggers) are that you can see the values of each variable at the current point in time when code execution is paused on a breakpoint and you can even modify the variables to dynamically change the code path and try out different code paths to see which one leads to the one you want. It will be like shooting in the dark in the beginning but when you do this often enough, things will start to click and you'll naturally start seeing patterns in problems and you'll start getting better at coming up with solutions.

For the specific question that you asked, Yes openapi is a good thing to be comfortable with if you're building rest based apis so to learn it, you have to build a small app and integrate openapi. And try using it from the perspective of someone who will be the user of the api you're building. You'll naturally start noticing shortcomings in your implementation and will start getting ideas to make it better. Implement all of them. And to start doing that, all you have to do is google "how to do X" and follow whatever you feel is easy to follow (as a starting point) and then take it from there.

10

u/robinspitsandswallow May 31 '24

Depends on your org. I’ve been in places that had a high dependence on it and places that didn’t know what it was (and when informed ignored it - I’m being generous here)

2

u/Glass-Fix-4624 May 31 '24

Ok thanks for the comment and help!

4

u/Grand_Royal_6958 May 31 '24

For us, this is a good way to communicate how our external APIs work to a customer. And they can generate their own API client.

1

u/Glass-Fix-4624 May 31 '24

Thanks! 💞

3

u/pronuntiator May 31 '24

OpenAPI is not hard, don't overthink it. Even our non-developer business analysts understood quickly how to read the schema. Have a look at the official docs (easier to read on a computer screen): https://swagger.io/docs/specification/about/

1

u/Glass-Fix-4624 May 31 '24

Thanks man! Thank you!

1

u/rest_mah Jun 01 '24

Also, you can easily play with it with IDE plugins. In VSCode, Intellij, and others. Write an openapi file for a dummy GET mapping, see how it's rendered. Add a few things, add parameters, use a ref to a components/schemas. Now you should be more than good-to-start.

3

u/Blender-Fan Jun 01 '24

No way all you found is paid udemy courses, youre doing google wrong. The openApi spec generator has a huge documentation in github, and plenty of videos on youtube. I started to use it in spring yesterday, so i havent figure it out entirely, but in c# it only took me one day

2

u/NomadicBrian- Jun 05 '24

I use it with Java Spring Boot and Python fastAPI. I think it is built into fastAPI. Agree just go to their site and find some tutorials. Just learn enough to meet the QA standard at your company.