SlideShare a Scribd company logo
Putting API Design First with
Swagger
Tony Tam
@fehguy
What is Swagger?
• The basis of the Open API Specification
– A Linux Foundation Project
– https://openapis.org
• A simple, structured way to describe your
API
• Methods, Resources, Parameters, media
types
• Everything your consumers need to know
to consume your API
Swagger Example
JSON
For
Robots
Swagger Example
YAML
For
Humans
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
API Metadata
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Host Metadata
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Resources
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Organizational Tags
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Codegen Hints
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Parameters
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Expected Responses
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Response Payload
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Model Schemas
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Required Properties
Swagger Example
Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0
Properties and Datatypes
How do you create a Swagger?
• It’s just a JSON or YAML document!
– Code First
– Design First
Code drives
the Definition
Definition
drives the code
Code First
• Code Annotations, comments for
embedded documentation
– Processed at compile-time, post-compile,
runtime
– Docs are always right! (code is the
documentation)
• Like Javadocs for REST APIs (but tons better)
Annotation Example
Design First
• Start with the Swagger Definition as a
blueprint for the implementation
– Code from the definition
• Manual! Swagger Definition is just the
implementation guideline
• Automated with swagger-codegen
Design First Example
Architecting for Maintainability
Whoever
thought of
this was an
*#&&!
Let’s go with
a dramatic,
50 pitch roof
Removing the Cruft
• Keep documentation outside your code
• Don’t clutter up your code!
– Use the Swagger Definition to drive your API,
not just document it
• You have a DSL, use it!
Introducing Swagger Inflector
• Use Swagger as the Source of Truth for
the API
– Automatically route to controllers
– Automatically map models
– Generate Sample Data when controllers not
implemented
https://github.com/swagger-api/swagger-inflector
How does it work?
• Inflector loads a Swagger Definition at startup
• Parses routes, parameters, models
• Uses configuration / extensions to find
controllers by method signature
– If a controller doesn’t exist, produces mock data
• Routes requests to the Right Place
• No Magic™ Software
– Jersey 2.x
– Jackson 2.x
Example project
https://github.com/fehguy/javaone-inflector-demo
Setup
• Add dependency
• Enable as JaxRS Application
Setup
• Create and configure inflector.yaml
• Save a swagger specification
Run it!
... Now what?
• Definition parsed, loaded
• Swagger definition mounted at basePath
Finding Controllers
• Default location from inflector.yaml
• Class name from Tag convention
– controllerPackage + tag[0].name
• Explicitly set as operation extension
– x-swagger-router-controller: org.your.Controller
Finding Methods
• Method name
– Controller Class + explicit operationId
• operationId: myMethod
– Controller Class + generated operationId
• operationId: clean(path) + httpMethod
– Explicit extension
• x-swagger-router-controller:
org.your.Controller.methodName
Finding Methods
• Method Arguments
– Response class match
• io.swagger.inflector.models.ResponseContext
– Argument match
• io.swagger.inflector.models.RequestContext
• Arguments matching ordered operation parameters
public ResponseContext
getMeetups (RequestContext request,
String title)
Complex Arguments
1. Matched by mapping in inflector.yaml
2. Extension on model
3. “Raw” JsonNode
public ResponseContext
addMeetup (RequestContext request,
JsonNode meetup)
Hitting Endpoints
• No implemented controller! Get mock data
• Implement a controller…
But it’s
Wrong!
Hitting Endpoints
• Response schema declares array!
Schema
Validation!
Input Parameters
• Design it the right way!
Putting Inflector in Practice
• Complete decoupling of API definition from
business logic
• Define API, concurrent development!
Back-EndFront-End
Auto-gen
SDK
Mock
Data
AdminController
UsersController
LoginController
Incremental
Impl!
Incremental Development
• What’s done? What’s Missing?
Incremental Development
• Moving to through SDLC
Extend it as needed
• Security?
– Use the RequestContext!
• Content Types?
– Implement an EntityProcessor
• Type Conversion?
– Implement a Converter
• Custom Input Validation?
– Implement a Validator
Swagger Connected
• Swagger is FOSS
• Specification + Tools at http://swagger.io
• All source at https://github.com/swagger-api
• Real-time support at irc.freenode.net
#swagger
Thank you!

More Related Content

What's hot (20)

PPTX
Swagger in the API Lifecycle
Ole Lensmar
 
PPTX
Building APIs with Node.js and Swagger
Jeremy Whitlock
 
PDF
Swagger for-your-api
Tony Tam
 
PPT
A Tour of Swagger for APIs
Allen Dean
 
PDF
Swagger 2.0: Latest and Greatest
LaunchAny
 
PPTX
Swagger APIs for Humans and Robots (Gluecon)
Tony Tam
 
PPTX
Swagger 2.0 and Model-driven APIs
Apigee | Google Cloud
 
PPTX
Introducing swagger
Amr Ali
 
PDF
Streamlining API with Swagger.io
Victor Augusteo
 
PDF
Quick run in with Swagger
Mesh Korea
 
PDF
Designing APIs with OpenAPI Spec
Adam Paxton
 
PPTX
A Tasty deep-dive into Open API Specification Links
Tony Tam
 
PDF
Implement Web API with Swagger
Jiang Wu
 
PDF
Swagger / Quick Start Guide
Andrii Gakhov
 
PDF
Design Driven API Development
Sokichi Fujita
 
PPTX
Document your rest api using swagger - Devoxx 2015
johannes_fiala
 
PPTX
Understanding how to use Swagger and its tools
Swagger API
 
PDF
Exposing Salesforce REST Services Using Swagger
Salesforce Developers
 
PPTX
What is Swagger?
Philip Senger
 
PDF
Documenting your REST API with Swagger - JOIN 2014
JWORKS powered by Ordina
 
Swagger in the API Lifecycle
Ole Lensmar
 
Building APIs with Node.js and Swagger
Jeremy Whitlock
 
Swagger for-your-api
Tony Tam
 
A Tour of Swagger for APIs
Allen Dean
 
Swagger 2.0: Latest and Greatest
LaunchAny
 
Swagger APIs for Humans and Robots (Gluecon)
Tony Tam
 
Swagger 2.0 and Model-driven APIs
Apigee | Google Cloud
 
Introducing swagger
Amr Ali
 
Streamlining API with Swagger.io
Victor Augusteo
 
Quick run in with Swagger
Mesh Korea
 
Designing APIs with OpenAPI Spec
Adam Paxton
 
A Tasty deep-dive into Open API Specification Links
Tony Tam
 
Implement Web API with Swagger
Jiang Wu
 
Swagger / Quick Start Guide
Andrii Gakhov
 
Design Driven API Development
Sokichi Fujita
 
Document your rest api using swagger - Devoxx 2015
johannes_fiala
 
Understanding how to use Swagger and its tools
Swagger API
 
Exposing Salesforce REST Services Using Swagger
Salesforce Developers
 
What is Swagger?
Philip Senger
 
Documenting your REST API with Swagger - JOIN 2014
JWORKS powered by Ordina
 

Similar to API Design first with Swagger (20)

PPTX
Swagger - Making REST APIs friendlier
Miroslav Resetar
 
ODP
Introduction to Swagger
Knoldus Inc.
 
PDF
"Design First" APIs with Swagger
scolestock
 
PDF
Swagger With REST APIs.pptx.pdf
Knoldus Inc.
 
PPTX
Grails with swagger
NexThoughts Technologies
 
PPTX
dod-api-meetup-03262015-swagger-docs
John O'Brien III
 
PPTX
Everybody loves Swagger
BizTalk360
 
PPTX
OWASP PDX May 2016 : Scanning with Swagger (OAS) 2.0
Scott Lee Davis
 
PDF
Swagger code motion talk
Victor Trakhtenberg
 
PDF
Get Your Node.js API Swaggering with OpenAPI Spec
Adam Paxton
 
PDF
Swagger - Make your REST APIs accessible - Victor Trakhtenberg
Codemotion Tel Aviv
 
PPTX
Everybody loves Swagger (Massimo Crippa @ Integration Monday)
Codit
 
PPTX
Contract driven development
Stephen Erdman
 
PPTX
Love your API with Swagger (Gluecon lightning talk)
Tony Tam
 
PDF
Jcon 2017 How to use Swagger to develop REST applications
johannes_fiala
 
PPTX
How to generate a rest application - DevFest Vienna 2016
johannes_fiala
 
PDF
Swagger Notes.pdf
sagarpal60
 
PPTX
API Docs with OpenAPI 3.0
Fabrizio Ferri-Benedetti
 
PDF
Let your REST API talk @java2days
Nikolay Petkov
 
PPTX
Swagger for startups
Tony Tam
 
Swagger - Making REST APIs friendlier
Miroslav Resetar
 
Introduction to Swagger
Knoldus Inc.
 
"Design First" APIs with Swagger
scolestock
 
Swagger With REST APIs.pptx.pdf
Knoldus Inc.
 
Grails with swagger
NexThoughts Technologies
 
dod-api-meetup-03262015-swagger-docs
John O'Brien III
 
Everybody loves Swagger
BizTalk360
 
OWASP PDX May 2016 : Scanning with Swagger (OAS) 2.0
Scott Lee Davis
 
Swagger code motion talk
Victor Trakhtenberg
 
Get Your Node.js API Swaggering with OpenAPI Spec
Adam Paxton
 
Swagger - Make your REST APIs accessible - Victor Trakhtenberg
Codemotion Tel Aviv
 
Everybody loves Swagger (Massimo Crippa @ Integration Monday)
Codit
 
Contract driven development
Stephen Erdman
 
Love your API with Swagger (Gluecon lightning talk)
Tony Tam
 
Jcon 2017 How to use Swagger to develop REST applications
johannes_fiala
 
How to generate a rest application - DevFest Vienna 2016
johannes_fiala
 
Swagger Notes.pdf
sagarpal60
 
API Docs with OpenAPI 3.0
Fabrizio Ferri-Benedetti
 
Let your REST API talk @java2days
Nikolay Petkov
 
Swagger for startups
Tony Tam
 
Ad

More from Tony Tam (14)

PPTX
Writer APIs in Java faster with Swagger Inflector
Tony Tam
 
PDF
Fastest to Mobile with Scalatra + Swagger
Tony Tam
 
PPTX
Data Modeling for NoSQL
Tony Tam
 
PPTX
System insight without Interference
Tony Tam
 
PPTX
Keeping MongoDB Data Safe
Tony Tam
 
PPTX
Inside Wordnik's Architecture
Tony Tam
 
PPTX
Scaling with swagger
Tony Tam
 
PPTX
Running MongoDB in the Cloud
Tony Tam
 
PPTX
Scala & Swagger at Wordnik
Tony Tam
 
PPTX
Why Wordnik went non-relational
Tony Tam
 
PPTX
Building a Directed Graph with MongoDB
Tony Tam
 
PPTX
Managing a MongoDB Deployment
Tony Tam
 
PPTX
Keeping the Lights On with MongoDB
Tony Tam
 
PPTX
Migrating from MySQL to MongoDB at Wordnik
Tony Tam
 
Writer APIs in Java faster with Swagger Inflector
Tony Tam
 
Fastest to Mobile with Scalatra + Swagger
Tony Tam
 
Data Modeling for NoSQL
Tony Tam
 
System insight without Interference
Tony Tam
 
Keeping MongoDB Data Safe
Tony Tam
 
Inside Wordnik's Architecture
Tony Tam
 
Scaling with swagger
Tony Tam
 
Running MongoDB in the Cloud
Tony Tam
 
Scala & Swagger at Wordnik
Tony Tam
 
Why Wordnik went non-relational
Tony Tam
 
Building a Directed Graph with MongoDB
Tony Tam
 
Managing a MongoDB Deployment
Tony Tam
 
Keeping the Lights On with MongoDB
Tony Tam
 
Migrating from MySQL to MongoDB at Wordnik
Tony Tam
 
Ad

Recently uploaded (20)

PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 

API Design first with Swagger

  • 1. Putting API Design First with Swagger Tony Tam @fehguy
  • 2. What is Swagger? • The basis of the Open API Specification – A Linux Foundation Project – https://openapis.org • A simple, structured way to describe your API • Methods, Resources, Parameters, media types • Everything your consumers need to know to consume your API
  • 5. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 API Metadata
  • 6. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Host Metadata
  • 7. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Resources
  • 8. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Organizational Tags
  • 9. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Codegen Hints
  • 10. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Parameters
  • 11. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Expected Responses
  • 12. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Response Payload
  • 13. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Model Schemas
  • 14. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Required Properties
  • 15. Swagger Example Taken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0 Properties and Datatypes
  • 16. How do you create a Swagger? • It’s just a JSON or YAML document! – Code First – Design First Code drives the Definition Definition drives the code
  • 17. Code First • Code Annotations, comments for embedded documentation – Processed at compile-time, post-compile, runtime – Docs are always right! (code is the documentation) • Like Javadocs for REST APIs (but tons better)
  • 19. Design First • Start with the Swagger Definition as a blueprint for the implementation – Code from the definition • Manual! Swagger Definition is just the implementation guideline • Automated with swagger-codegen
  • 21. Architecting for Maintainability Whoever thought of this was an *#&&! Let’s go with a dramatic, 50 pitch roof
  • 22. Removing the Cruft • Keep documentation outside your code • Don’t clutter up your code! – Use the Swagger Definition to drive your API, not just document it • You have a DSL, use it!
  • 23. Introducing Swagger Inflector • Use Swagger as the Source of Truth for the API – Automatically route to controllers – Automatically map models – Generate Sample Data when controllers not implemented https://github.com/swagger-api/swagger-inflector
  • 24. How does it work? • Inflector loads a Swagger Definition at startup • Parses routes, parameters, models • Uses configuration / extensions to find controllers by method signature – If a controller doesn’t exist, produces mock data • Routes requests to the Right Place • No Magic™ Software – Jersey 2.x – Jackson 2.x
  • 26. Setup • Add dependency • Enable as JaxRS Application
  • 27. Setup • Create and configure inflector.yaml • Save a swagger specification
  • 28. Run it! ... Now what? • Definition parsed, loaded • Swagger definition mounted at basePath
  • 29. Finding Controllers • Default location from inflector.yaml • Class name from Tag convention – controllerPackage + tag[0].name • Explicitly set as operation extension – x-swagger-router-controller: org.your.Controller
  • 30. Finding Methods • Method name – Controller Class + explicit operationId • operationId: myMethod – Controller Class + generated operationId • operationId: clean(path) + httpMethod – Explicit extension • x-swagger-router-controller: org.your.Controller.methodName
  • 31. Finding Methods • Method Arguments – Response class match • io.swagger.inflector.models.ResponseContext – Argument match • io.swagger.inflector.models.RequestContext • Arguments matching ordered operation parameters public ResponseContext getMeetups (RequestContext request, String title)
  • 32. Complex Arguments 1. Matched by mapping in inflector.yaml 2. Extension on model 3. “Raw” JsonNode public ResponseContext addMeetup (RequestContext request, JsonNode meetup)
  • 33. Hitting Endpoints • No implemented controller! Get mock data • Implement a controller… But it’s Wrong!
  • 34. Hitting Endpoints • Response schema declares array! Schema Validation!
  • 35. Input Parameters • Design it the right way!
  • 36. Putting Inflector in Practice • Complete decoupling of API definition from business logic • Define API, concurrent development! Back-EndFront-End Auto-gen SDK Mock Data AdminController UsersController LoginController Incremental Impl!
  • 37. Incremental Development • What’s done? What’s Missing?
  • 39. Extend it as needed • Security? – Use the RequestContext! • Content Types? – Implement an EntityProcessor • Type Conversion? – Implement a Converter • Custom Input Validation? – Implement a Validator
  • 40. Swagger Connected • Swagger is FOSS • Specification + Tools at http://swagger.io • All source at https://github.com/swagger-api • Real-time support at irc.freenode.net #swagger