SlideShare a Scribd company logo
Slicing, Dicing, and
Linting OpenAPI
Go Conference 2018 Autumn
Nov 25, 2018
Daisuke Maki (@lestrrat) / HDE Inc.
• @lestrrat
• Perl/Go hacker, author, father
• Author of github.com/peco/peco
• Organizer for builderscon
tl;dr
• github.com/lestrrat-go/openapi
• API designed for spec consumer
• Safety + Predictability
• Tools to work with specs
OpenAPI
Who _Knows_ OpenAPI?
…in its entirety?
Specification for
REST services
「んー僕もopenapiへの恨み言は、
いくらでも出てきそうだ」
「別名、SOAP 2nd generationだからなー 」
What They Are Saying
“I think I can go all night with
complaints about OpenAPI”
“It’s also known as SOAP the
Next Generation”
JSON or YAML
Slicing, Dicing, And Linting OpenAPI
OpenAPI v2
OpenAPI v3
OpenAPI v2
OpenAPI v3
“I don’t understand any of it: We’re just pretending to understand OpenAPI"
My Goals
• Use Cloud Endpoints (GCP)
• Generate gRPC server/client
• Generate ES6/Golang REST client
• Write definitions in ONE file
Cloud Endpoints
gRPC
…because it’s easier to write the server
REST Clients
…because the web isn’t ready for gRPC-only
LB
openapi
endpoints-runtime
use config=2018-10-01r1
fetch configuration
(sidecar)
HTTP2 protobuf
protobuf
Endpoints config
generated
gRPC Server
generated (stub)
HTTP/1.1
JSON payload
(REST in Go or ES6)
generated
Cloud Endpoints
register
versioned configurations
config 2018-10-01r0
config 2018-10-01r1
config 2018-10-04r0
…
Support
“I wish the ecosystem/rest of the world would support v3”
OpenAPI 3.0.2
OAI Specification
OpenAPI 2.0
Google Cloud
hmmm…
!!!!
AWS
…
(look at the bright side: we’re going
to need tools to do v2 → v3)
Prior Art (1)
Gripes
Gripes
• Already tried tinkering w/ it
• Only generates protobuf
• Does NOT check correctness
Prior Art (2)
Gripes
• Where’s v3?
• How do I generate custom code?
• Does NOT check correctness
Where’s v3?
Sooner or later
we will need to move to v3
github.com/go-openapi/spec3
hmm…
Ensuring Correctness
Remember?
“I don’t understand any of it: We’re just pretending to understand OpenAPI"
We will make mistakes
surely
(really horrible schemas)
API should defend us
from making mistakes
everything is exported
(no defense against invalid values)
completely relies on encoding/json
(must match how encoding/json works, not necessarily how
the user wants to code)
required fields, but can’t tell
_Should_ scream FAIL
spec := openapi.Swagger{
Version: “3.0.2”, // Wrong Version
// Missing Info

// Missing Paths
}
bonus: Processing paths
• Need context to process leaves
• e.g. need PathItem.Path while
processing Operation objects
paths:
/pets:
get:
description: …
operationId: findPets
parameters:
- name: tags
in: query
required: false
…
- name: limit
in: query
required: false
type: integer
format: int32
responses:
200: …
Sample Spec
Paths object
PathItem object
Operation object
Operation object by itself does
not know the HTTP verb it’s
associated with
func processOperation(verb string, oper Operation) {
…
}
Currying is, meh…
• You have to remember what to curry
• API signature is no longer standardized
across methods
bonus: concurrent updates
_Should_ be safe
go func() { spec.Info.Title = “foo” }()
go func() { spec.Info.Title = “bar” }()
• Exposing always leaves a way for
users to screw up
Yak Shaving Time
github.com/lestrrat-go/openapi
github.com/lestrrat-go/openapi
• Model: v2 usable, v3 experimental
• Generation: gRPC, ES6+flow, Go
generation: usable
• Lint: usable
General Direction
• Don’t allow users to screw up
• Provide API to easily consume data
• Provide Linting/Code Generation
Protection from Errors
// github.com/lestrrat-go/openapi
type OpenAPI = Swagger
type Swagger interface { … }
spec := Swagger{} // Can’t do it
Prohibited Direct Instantiation
spec, err := openapi.NewSwagger([ mandatory params ]).
BasePath(…). // Optional params
…
Build() // Finally, build the object
Object Creation via Builder
Object Creation API
✓ Can’t instantiate invalid objects without
proper initialization
✓ Required/Optional parameters can be
distinguished visually
✓ Automatic validation
err := openapi.MutateSwagger(spec).
Info(…). // Any values that needs changing
…
Apply() // Finally, mutate the object
Object Mutation via Mutator
Object Mutation API
✓ Can’t mutate objects into invalid state
✓ Consistency can be controlled across
same spec tree
Data Access API
Data Traversal
• OpenAPI is a tree structure
• You need to traverse through the tree
• … and you need to know the context of
the current node
Iterating
• Giving access to raw slices and maps
could mean danger
• Allow access to objects, but no
mutation
for pIter := paths.Paths(); pIter.Next(); {
pathName, pathItem := pIter.Item()
for piIter := pathItem.Operations(); piIter.Next(); {
oper := piIter.Item()
}
}
Consistent Iterator API
Context
• HTTP verb gives context to Operation
object
• But the data is far from the Operation
object
• Currying is, meh…
verb := oper.Verb() // NOT part of OpenAPI spec
Utility Accessors
✓ Creation/Mutation API ensures consistency
with context
✓ No need to curry extra parameters
Linting
Linting is a MUST
• OpenAPI is too hard
• Programs should tell us of
inconsistencies
oalint
go get github.com/lestrrat-go/openapi/cmd/oalint
✓ Checks for semantic errors
✓ Formats output
finch% git diff spec
…
--- a/spec/examples/v2.0/petstore-expanded.yaml
+++ b/spec/examples/v2.0/petstore-expanded.yaml
@@ -1,4 +1,4 @@
-swagger: "2.0"
+swagger: "2.0.1"
info:
version: 1.0.0
title: Swagger Petstore
Sample Spec
finch% ./oalint -file spec/examples/v2.0/petstore-
expanded.yaml
2018/11/15 09:24:59 failed to parse input: failed to validate
spec: failed to visit Swagger element: swagger field must be
"2.0" (got "2.0.1")
Catches Errors!
Code Generation
(caveat emptor: needs polishing)
oagen protobuf 
-package myapp 
-output api.proto 
-annotate 
-global-option go_package=pb 
api.yaml
Create gRPC protobuf
oagen rest client 
-target=go 
-directory=/path/to/myapp 
api.yaml
Create Go REST Client
oagen restclient 
-target=es6flow 
-directory=/path/to/myapp 
api.yaml
Create ES6(+flow) REST Client
# This does not yet exist
oagen v2tov3 api.yaml
(Future) v2tov3
Recap
LB
openapi
endpoints-runtime
use config=2018-10-01r1
fetch configuration
(sidecar)
HTTP2 protobuf
protobuf
Endpoints config
generated
gRPC Server
generated (stub)
HTTP/1.1
JSON payload
(REST in Go or ES6)
generated
Cloud Endpoints
register
versioned configurations
config 2018-10-01r0
config 2018-10-01r1
config 2018-10-04r0
…
Current Status
• Working gRPC/Go REST client
• ES6 client should work
• V3 is mostly done
• v2tov3 is still in my head
Questions?

More Related Content

PDF
Learn To Test Like A Grumpy Programmer - 3 hour workshop
PDF
NLP using JavaScript Natural Library
PDF
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
PDF
PHP 7.1 : elegance of our legacy
PDF
Preparing code for Php 7 workshop
PPTX
Php extensions
PDF
Design attern in php
PDF
Swift, a quick overview
Learn To Test Like A Grumpy Programmer - 3 hour workshop
NLP using JavaScript Natural Library
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
PHP 7.1 : elegance of our legacy
Preparing code for Php 7 workshop
Php extensions
Design attern in php
Swift, a quick overview

What's hot (20)

PDF
The secret of PHP7's Performance
PDF
Reference Semantik mit C# und .NET Core - BASTA 2019
PDF
Symfony + GraphQL
PPTX
Graphql + Symfony | Александр Демченко | CODEiD
PDF
Hourglass Interfaces for C++ APIs - CppCon 2014
PDF
C# What's next? (7.x and 8.0)
PDF
Php7 傳說中的第七隻大象
PDF
Get cfml Into the Box 2018
PDF
Coder sans peur du changement avec la meme pas mal hexagonal architecture
PPTX
Migrating to PHP 7
PDF
Android Malware and Machine Learning
PDF
PHP7 - The New Engine for old good train
PDF
Android Deobfuscation: Tools and Techniques
PDF
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
PPTX
Functional Programming
PDF
Functional Programming in Java
PPTX
PDF
20140408 tdd puppetcamp-paris
PDF
GooglePropsal
ODP
A quick and dirty intro to objective c
The secret of PHP7's Performance
Reference Semantik mit C# und .NET Core - BASTA 2019
Symfony + GraphQL
Graphql + Symfony | Александр Демченко | CODEiD
Hourglass Interfaces for C++ APIs - CppCon 2014
C# What's next? (7.x and 8.0)
Php7 傳說中的第七隻大象
Get cfml Into the Box 2018
Coder sans peur du changement avec la meme pas mal hexagonal architecture
Migrating to PHP 7
Android Malware and Machine Learning
PHP7 - The New Engine for old good train
Android Deobfuscation: Tools and Techniques
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Functional Programming
Functional Programming in Java
20140408 tdd puppetcamp-paris
GooglePropsal
A quick and dirty intro to objective c
Ad

Similar to Slicing, Dicing, And Linting OpenAPI (20)

KEY
Motion Django Meetup
PPTX
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
PPT
Apache Persistence Layers
PDF
Enforcing API Design Rules for High Quality Code Generation
KEY
ajn13 BeerTalk FlexRemoteApi
PDF
Developing Brilliant and Powerful APIs in Ruby & Python
PPTX
ISI work
PDF
Drilling Cyber Security Data With Apache Drill
PDF
TAKING PHP SERIOUSLY - Keith Adams
PPTX
2022 APIsecure_Securing APIs with Open Standards
PPTX
Gohan
PPT
Php training100%placement-in-mumbai
PPTX
API Docs with OpenAPI 3.0
PDF
From SaltStack to Puppet and beyond...
PPT
Introduction to PHP - SDPHP
PDF
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
PDF
Walter api
Motion Django Meetup
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
Apache Persistence Layers
Enforcing API Design Rules for High Quality Code Generation
ajn13 BeerTalk FlexRemoteApi
Developing Brilliant and Powerful APIs in Ruby & Python
ISI work
Drilling Cyber Security Data With Apache Drill
TAKING PHP SERIOUSLY - Keith Adams
2022 APIsecure_Securing APIs with Open Standards
Gohan
Php training100%placement-in-mumbai
API Docs with OpenAPI 3.0
From SaltStack to Puppet and beyond...
Introduction to PHP - SDPHP
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
Walter api
Ad

More from lestrrat (20)

PDF
Future of Tech "Conferences"
PDF
ONIの世界 - ONIcon 2019 Winter
PDF
Oxygen Not Includedをやるべき4つの理由
PDF
Rejectcon 2018
PDF
Builderscon tokyo 2018 speaker dinner
PDF
GoらしいAPIを求める旅路 (Go Conference 2018 Spring)
PDF
Google container builderと友だちになるまで
PDF
筋肉によるGoコードジェネレーション
PDF
iosdc 2017
PDF
シュラスコの食べ方 超入門
PDF
OSSの敵になるのもいいじゃない
PDF
Coding in the context era
PDF
Kubernetes in 30 minutes (2017/03/10)
PDF
Opening: builderscon tokyo 2016
PDF
Kubernetes in 20 minutes - HDE Monthly Technical Session 24
PDF
小規模でもGKE - DevFest Tokyo 2016
PDF
いまさら聞けないselectあれこれ
PDF
Don't Use Reflect - Go 1.7 release party 2016
PDF
How To Think In Go
PDF
On internationalcommunityrelations
Future of Tech "Conferences"
ONIの世界 - ONIcon 2019 Winter
Oxygen Not Includedをやるべき4つの理由
Rejectcon 2018
Builderscon tokyo 2018 speaker dinner
GoらしいAPIを求める旅路 (Go Conference 2018 Spring)
Google container builderと友だちになるまで
筋肉によるGoコードジェネレーション
iosdc 2017
シュラスコの食べ方 超入門
OSSの敵になるのもいいじゃない
Coding in the context era
Kubernetes in 30 minutes (2017/03/10)
Opening: builderscon tokyo 2016
Kubernetes in 20 minutes - HDE Monthly Technical Session 24
小規模でもGKE - DevFest Tokyo 2016
いまさら聞けないselectあれこれ
Don't Use Reflect - Go 1.7 release party 2016
How To Think In Go
On internationalcommunityrelations

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
Approach and Philosophy of On baking technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
KodekX | Application Modernization Development
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
Teaching material agriculture food technology
Approach and Philosophy of On baking technology
“AI and Expert System Decision Support & Business Intelligence Systems”
KodekX | Application Modernization Development
The Rise and Fall of 3GPP – Time for a Sabbatical?
Unlocking AI with Model Context Protocol (MCP)
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
cuic standard and advanced reporting.pdf
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
Per capita expenditure prediction using model stacking based on satellite ima...
Review of recent advances in non-invasive hemoglobin estimation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.

Slicing, Dicing, And Linting OpenAPI