41
Putting API Design First with Swagger Tony Tam @fehguy

API Design first with Swagger

Embed Size (px)

Citation preview

Page 1: API Design first with Swagger

Putting API Design First with Swagger

Tony Tam@fehguy

Page 2: API Design first with Swagger

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

Page 3: API Design first with Swagger

Swagger Example

JSON

For Robots

Page 4: API Design first with Swagger

Swagger Example

YAML

For Humans

Page 6: API Design first with Swagger

Swagger ExampleTaken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0

Host Metadata

Page 8: API Design first with Swagger

Swagger ExampleTaken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0

Organizational Tags

Page 9: API Design first with Swagger

Swagger ExampleTaken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0

Codegen Hints

Page 11: API Design first with Swagger

Swagger ExampleTaken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0

Expected Responses

Page 12: API Design first with Swagger

Swagger ExampleTaken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0

Response Payload

Page 13: API Design first with Swagger

Swagger ExampleTaken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0

Model Schemas

Page 14: API Design first with Swagger

Swagger ExampleTaken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0

Required Properties

Page 15: API Design first with Swagger

Swagger ExampleTaken from https://swaggerhub.com/api/fehguy/meetup-api/1.0.0

Properties and Datatypes

Page 16: API Design first with Swagger

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

Page 17: API Design first with Swagger

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)

Page 18: API Design first with Swagger

Annotation Example

Page 19: API Design first with Swagger

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

Page 20: API Design first with Swagger

Design First Example

Page 21: API Design first with Swagger

Architecting for MaintainabilityWhoever

thought of this was an

*#&&!

Let’s go with a dramatic,

50 pitch roof

Page 22: API Design first with Swagger

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!

Page 23: API Design first with Swagger

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

Page 24: API Design first with Swagger

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

Page 25: API Design first with Swagger

Example projecthttps://github.com/fehguy/javaone-inflector-demo

Page 26: API Design first with Swagger

Setup

• Add dependency

• Enable as JaxRS Application

Page 27: API Design first with Swagger

Setup

• Create and configure inflector.yaml

• Save a swagger specification

Page 28: API Design first with Swagger

Run it!

... Now what?• Definition parsed, loaded• Swagger definition mounted at basePath

Page 29: API Design first with Swagger

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

Page 30: API Design first with Swagger

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

Page 31: API Design first with Swagger

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)

Page 32: API Design first with Swagger

Complex Arguments

1. Matched by mapping in inflector.yaml2. Extension on model3. “Raw” JsonNode

public ResponseContext addMeetup (RequestContext request, JsonNode meetup)

Page 33: API Design first with Swagger

Hitting Endpoints

• No implemented controller! Get mock data

• Implement a controller…But it’s Wrong!

Page 34: API Design first with Swagger

Hitting Endpoints

• Response schema declares array!

Schema Validation!

Page 35: API Design first with Swagger

Input Parameters

• Design it the right way!

Page 36: API Design first with Swagger

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!

Page 37: API Design first with Swagger

Incremental Development

• What’s done? What’s Missing?

Page 38: API Design first with Swagger

Incremental Development

• Moving to through SDLC

Page 39: API Design first with Swagger

Extend it as needed

• Security? – Use the RequestContext!

• Content Types?– Implement an EntityProcessor

• Type Conversion?– Implement a Converter

• Custom Input Validation?– Implement a Validator

Page 40: API Design first with Swagger

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

Page 41: API Design first with Swagger

Thank you!