|
| 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 | + |
0 commit comments