Skip to content
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

OpenAPI v3: Support for nullable fields #106

Closed
NGEfreak opened this issue Jan 30, 2019 · 5 comments
Closed

OpenAPI v3: Support for nullable fields #106

NGEfreak opened this issue Jan 30, 2019 · 5 comments
Assignees

Comments

@NGEfreak
Copy link

OpenAPI v3 features a new option to declare fields as nullable.
Reference: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-20

This doesn't appear to work yet.

#!/usr/bin/env perl

use 5.010;
use strict;
use warnings;
use utf8;

use Test::More;
use Test::Mojo;

use Mojolicious::Lite;

get '/test' => sub {
    shift->render( openapi => { foo => undef }, status => 200 );
}, 'test';

plugin 'OpenAPI' => {
    url    => 'data:///api.yml',
    schema => 'v3',
};

my $t = Test::Mojo->new();

$t->get_ok( '/test', { Accept => 'application/json' } )
    ->status_is(200)
    ->json_is( { foo => undef } );

done_testing;

__DATA__

@@ api.yml
openapi: 3.0.0
info:
  title: Test
  version: 0.0.0
paths:
  /test:
    get:
      x-mojo-name: test
      responses:
        '200':
          description: Test
          content:
            application/json:
              schema:
                properties:
                  foo:
                    type: string
                    nullable: true
@jhthorsen
Copy link
Owner

This whole issue is very depressing to me. Do you know why they don't encourage "type":[..., "null"] instead?

@NGEfreak
Copy link
Author

NGEfreak commented Feb 8, 2019

According to OAI/OpenAPI-Specification#229 (comment) they overlooked the fact that JSON Schema allows null and array values in type. I guess they introduced nullable to keep it consistent with v2.

In v2 null or nullable was not supported at all officially. However, many validators ignored this fact and applied the logic of JSON Schema or introduced keywords like x-nullable instead.

See also:
OAI/OpenAPI-Specification#1389
OAI/OpenAPI-Specification#1443
OAI/OpenAPI-Specification#397

@jhthorsen
Copy link
Owner

That's so sad 😥I was hoping there was a better reason, but I couldn't find one.

@jhthorsen
Copy link
Owner

Seems like there's an implementation more or less ready at jhthorsen/json-validator#146

@augensalat
Copy link
Contributor

Hi,
I understand why my change from jhthorsen/json-validator#146 is better done in the OpenAPI plugin.

In order to implement the nullable check in M:P::OpenAPI I thougt to override _validate(), but then I'd have to copy all that dereferencing and anti-recursion logic from the base class _validate() and this doesn't feel right.
It seems a "correct" solution would require a deeper intervention in the code, but I'm not familiar enough with it to do that.

jhthorsen pushed a commit that referenced this issue Feb 14, 2019
 - Fix HEAD requests #105
 - Fix using /servers/0/url as basePath for OpenAPI v3 #110
   Note: This could be breaking change
 - Fix getting basePath when using under #107
 - Add support for "nullable" in OpenAPI 3.0 #106
 - Improved handling of Accept header in OpenAPI v3 #104
   Can now handle wildcards, such as application/* and */*, even though not
   defined in the specification.
 - Bump JSON::Validator to 3.06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants