feat(nullable): nullable.go for OpenAPI 3.1 compatibility#2068
Closed
frenchi wants to merge 1 commit into
Closed
Conversation
Introduce pkg/codegen/nullable.go with IsSchemaNullable(specVersion, openapi3.SchemaRef) - Support 3.1 JSON Schema null patterns: type: ["T","null"], oneOf/anyOf with null-only arm - Fall back to 3.0 schema.Nullable - add tests pkg/codegen/nullable_test.go - No integration yet; generation behavior unchanged until wired in
Kusari Analysis Results:
No pinned version dependency changes, code issues or exposed secrets detected!
Found this helpful? Give it a 👍 or 👎 reaction! |
Member
|
Hi @frenchi , thanks for doing this, however, now that kin-openapi supports OpenAPI 3.1, we're doing this on top of that code, and it'll be in the next minor release. I'm closing this since we'll do it differently. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Hello maintainers 👋
New contributor to this project, so feedback very much welcome & appreciated!
We just upgraded our internal OAS Spec to version 3.1, before running in to #373 3.1 compatibility issues. Instead of downgrading, I have some cycles to spend on hopefully progressing some of the tasks under OpenAPI 3.1 support.
This is the first in a small series of PRs to hopefully make progress towards the changed nullable behaviour in OAS 3.1, while ensuring backwards compatibility.
Open Questions up-front:
This PR
nullable.go handles null in line with JSON schema core 2020-12 spec for OAS 3.1, with one explicit generator policy:
Empty schemas
{}are treated as non-nullable to avoid over-detection in codegen.What's included:
pkg/codegen/nullable.go(this PR) but not wired up yet.IsSchemaNullable(specVersion string, schema *openapi3.SchemaRef) booloutput-options.nullable-typeexists inconfiguration-schema.json, defaultfalse)type(including union with'null'),oneOf/anyOf/allOf,enum(null present), andnot(blocks null if it accepts null)schema.Value.Nullablefornullable: truecompatibilitypkg/codegen/nullability_test.goNote: The detector intentionally diverges from raw JSON Schema for
{}as explained above.The logic & test coverage of
oneOf/anyOf/allOfgot a little hairy, so I found I understood it better once I sketched it out.e.g.
[ {'type':'string'} ]is zero subschemas, k=0[ {'type':'null'} ]is one subschema, k=1[ {'type':'null'}, {'type':'null'} ]k=2, etc...appendix below with full test coverage matrix.
Future Work
IsSchemaNullable()into the broader codebase,output-options.nullable-typeconfigoutput-options.nullable-typeis enabled, usenullable.Value[T]fromgithub.com/oapi-codegen/nullable(previouslynullable.Nullable[T]), non-blocking PR: feat(codegen): emit nullable.Value[T] whennullable-typeis enabled #2067openapi: 3.1.*Delivery plan
rough PR split
Checklist
Documentation updated (README/CONTRIBUTING/examples as needed)N/Amake tidyis run locallymake testis run locallyN/Amake generaterun is run locally and outputs committed (if generated code is affected)make lintis run locallyAdditional context
Test Coverage of all cases:
typetype: 'null'typetype: 'string'typetype: ['string','null']typetype: ['string','integer']oneOf[ {type:'string'}, {type:'null'} ]oneOf[ {type:'null'}, {type:'null'} ]oneOf[ {type:['string','null']}, {type:'null'}, {type:'integer'} ]anyOf[ {type:'integer'}, {type:'null'} ]anyOf[ {type:'null'}, {type:'null'} ]allOf[ {type:'null'}, {type:'string'} ]allOf[ {type:'null'}, {type:'null'} ]anyOf+typetype:'string', anyOf: [ {type:'null'}, {minLength:1} ]enumenum: [null, 'x']enum+ basetype:'string', enum: [null, 'x']nottype: ['string','null'], not: {type:'null'}{}{type:'string', nullable:true}{type:'string'}