-
-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorect validation of nullable in allOf #171
Comments
Same problem here: I have this object that is used as a response: MyObject:
type: object
description: MyObject description
properties:
nullableRef:
allOf:
- $ref: '#/components/schemas/NotNullableObject'
- nullable: true
- description: nullableRef description
- example: 5432
required:
- nullableRef
additionalProperties: false And when validating I'm geting the error: |
The example by @pali conflicts with https://github.com/OAI/OpenAPI-Specification/blob/master/proposals/003_Clarify-Nullable.md nullable: true doesn't add "null" to the values of the enum and thus is not allowed. The allOf needs to look like this:
or simply:
Still, we also have issues with allOf and nullable. |
I'm having a similar issue with validation of nullable values not working as I expect it to. I have a few types declared that mostly shouldn't be null, but in a few cases should. I've tried to solve it using I've created a minimal example showing my problem:
openapi: 3.0.1
info:
title: "test"
version: 0.0.0
paths:
/not_nullable:
get:
responses:
"200":
description: ""
content:
application/json:
schema:
$ref: "#/components/schemas/NotNullable"
/nullable:
get:
responses:
"200":
description: ""
content:
application/json:
schema:
allOf:
- $ref: "#/components/schemas/NotNullable"
- type: object
nullable: true
components:
schemas:
NotNullable:
type: object
properties:
key:
type: integer
example: 1 I'd like to reuse the type Here's an example program showing what I'm doing: package main
import (
"bytes"
"context"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/getkin/kin-openapi/routers/gorillamux"
)
func main() {
const openAPISpec = "openapi/nullable_api.yml"
ctx := context.Background()
mockResponse := []byte(`null`)
// mockResponse := []byte(`{"key": 1}`) // NOTE: it works with this body
loader := &openapi3.Loader{Context: ctx, IsExternalRefsAllowed: true}
doc, err := loader.LoadFromFile(openAPISpec)
if err != nil {
log.Fatalf("failed to load openapi spec: %s", err)
}
err = doc.Validate(ctx)
if err != nil {
log.Fatalf("failed to validate openapi spec: %s", err)
}
router, err := gorillamux.NewRouter(doc)
if err != nil {
log.Fatalf("failed to create gorilla router: %s", err)
}
httpReq := httptest.NewRequest(http.MethodGet, "http://127.0.0.1/nullable", nil)
// Find route
route, pathParams, err := router.FindRoute(httpReq)
if err != nil {
log.Fatalf("failed to find route: %s", err)
}
// Validate request
requestValidationInput := &openapi3filter.RequestValidationInput{
Request: httpReq,
PathParams: pathParams,
Route: route,
Options: &openapi3filter.Options{
AuthenticationFunc: openapi3filter.NoopAuthenticationFunc,
},
}
err = openapi3filter.ValidateRequest(ctx, requestValidationInput)
if err != nil {
log.Fatalf("request not valid: %s", err)
}
// Validate response
responseValidationInput := &openapi3filter.ResponseValidationInput{
RequestValidationInput: requestValidationInput,
Status: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: ioutil.NopCloser(bytes.NewReader(mockResponse)),
}
err = openapi3filter.ValidateResponse(ctx, responseValidationInput)
if err != nil {
log.Fatalf("response not valid: %s", err)
}
log.Printf("success!")
} The above program outputs the following when I use
|
I'm curious about the case of a nullable ref without allOf For example the following:
Results in:
|
Signed-off-by: Pierre Fenoll <[email protected]>
When
allOf
contains two structures and both of them have defined"nullable" : true
then validator should acceptnull
value (asnull
value is valid for both defined structures).But kin-openapi validator currently reject
null
value. Below is schema and test case.Test input:
{ "value" : null }
. Validator should accept this input, but currently it rejects it.Looks like that PR #169 did not fixed this case.
The text was updated successfully, but these errors were encountered: