Skip to content

Commit 72d9005

Browse files
feat(openapi3conv): canonicalization pass for 3.0 -> 3.x (#1162)
1 parent 03ab662 commit 72d9005

4 files changed

Lines changed: 886 additions & 0 deletions

File tree

.github/docs/openapi3conv.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package openapi3conv // import "github.com/getkin/kin-openapi/openapi3conv"
2+
3+
Package openapi3conv canonicalizes an OpenAPI 3.x document into the latest 3.x
4+
representation in place. Schema-level constructs serialize differently between
5+
OpenAPI 3.0 and 3.1, but represent the same semantics; this package rewrites the
6+
3.0 forms into their 3.1 equivalents and bumps the version string to the latest
7+
3.x patch release the package knows about.
8+
9+
The OAI commits to strict compatibility for 3.x going forward (see the 3.2.1
10+
and 3.3.0 milestones), so a tool that handles the 3.1+ form correctly handles
11+
all later 3.x versions correctly too. The 3.0 → 3.1 transition — the only break
12+
in the 3.x line — is the gap this package exists to bridge. 3.1 → 3.2 (and any
13+
future 3.x) is purely additive and requires no rewrites; the package handles
14+
those as a version-string bump.
15+
16+
Use this when a downstream consumer (diff tools, validators, code generators)
17+
needs a single canonical representation regardless of the source spec's declared
18+
version.
19+
20+
Scope:
21+
- In scope: 3.x → latest 3.x.
22+
- Out of scope: any → 3.0 (downgrade is lossy by nature).
23+
- Out of scope: cross-major upgrades (3 → 4 if/when v4 ships). Those belong
24+
in a dedicated package mirroring openapi2conv (which converts Swagger 2.0
25+
documents to OpenAPI 3.0).
26+
27+
Documents must be Validate()'d before calling Upgrade — passing an invalid
28+
document is undefined behaviour.
29+
30+
FUNCTIONS
31+
32+
func Upgrade(doc *openapi3.T, opts ...Option)
33+
Upgrade canonicalizes doc into the latest 3.x representation in place.
34+
35+
The schema-level rewrites the walker applies (nullable → type array,
36+
boolean exclusive bounds → numeric, example → examples) are idempotent and
37+
convergent on the 3.1+ form. Calling Upgrade on an already-3.1 (or later)
38+
document is a no-op aside from the version string bump.
39+
40+
Cross-major upgrades (3 → 4 if/when v4 ships) are not handled here; that
41+
belongs in a dedicated package mirroring the openapi2conv pattern.
42+
43+
doc must be Validate()'d before calling Upgrade; passing an invalid document
44+
is undefined behaviour.
45+
46+
func UpgradeSchema(s *openapi3.Schema)
47+
UpgradeSchema canonicalizes a single schema (and its descendants) in place.
48+
Exposed for callers that need to upgrade a sub-tree rather than a full
49+
document — e.g., a diff tool comparing isolated schemas.
50+
51+
52+
TYPES
53+
54+
type Option func(*upgradeOptions)
55+
Option configures an Upgrade pass. See WithWriter.
56+
57+
func WithWriter(w io.Writer) Option
58+
WithWriter routes one debug line per applied rewrite to w.
59+

openapi3conv/doc.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Package openapi3conv canonicalizes an OpenAPI 3.x document into the latest
2+
// 3.x representation in place. Schema-level constructs serialize differently
3+
// between OpenAPI 3.0 and 3.1, but represent the same semantics; this package
4+
// rewrites the 3.0 forms into their 3.1 equivalents and bumps the version
5+
// string to the latest 3.x patch release the package knows about.
6+
//
7+
// The OAI commits to strict compatibility for 3.x going forward (see the
8+
// 3.2.1 and 3.3.0 milestones), so a tool that handles the 3.1+ form
9+
// correctly handles all later 3.x versions correctly too. The 3.0 → 3.1
10+
// transition — the only break in the 3.x line — is the gap this package
11+
// exists to bridge. 3.1 → 3.2 (and any future 3.x) is purely additive and
12+
// requires no rewrites; the package handles those as a version-string bump.
13+
//
14+
// Use this when a downstream consumer (diff tools, validators, code
15+
// generators) needs a single canonical representation regardless of the
16+
// source spec's declared version.
17+
//
18+
// Scope:
19+
// - In scope: 3.x → latest 3.x.
20+
// - Out of scope: any → 3.0 (downgrade is lossy by nature).
21+
// - Out of scope: cross-major upgrades (3 → 4 if/when v4 ships). Those
22+
// belong in a dedicated package mirroring openapi2conv (which converts
23+
// Swagger 2.0 documents to OpenAPI 3.0).
24+
//
25+
// Documents must be Validate()'d before calling Upgrade — passing an
26+
// invalid document is undefined behaviour.
27+
package openapi3conv

0 commit comments

Comments
 (0)