jhthorsen / mojolicious-plugin-openapi

OpenAPI / Swagger plugin for Mojolicious
54 stars 44 forks source link

Defaults in openapi3 #115

Closed plk closed 4 years ago

plk commented 5 years ago

Direct copy of #53 converted to openapi3 spec but defaults seems to have stopped working? Using 2.12 and JSON::Validator 3.06. Last time this was a JSON::Validator problem I believe ...

use Mojo::Base -strict;
use Test::Mojo;
use Test::More;
use Mojolicious::Lite;

get '/test' => sub {
  my $c = shift->openapi->valid_input or return;
  $c->render(status => 200, openapi => $c->param('pcversion'));
  },
  'File';

plugin OpenAPI => {schema => 'v3', url => 'data://main/file.yaml'};

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

$t->get_ok('/api/test')->status_is(200)->content_is('"10.1.0"');

done_testing;

package main;
__DATA__
@@ file.yaml
openapi: 3.0.0
info:
  title: Test defaults
  version: "1"
servers:
  - url: /api
paths:
  /test:
    get:
      operationId: File
      parameters:
        - $ref: "#/components/parameters/PCVersion"
      responses:
        "200":
          description: thing
          content:
            "*/*":
              schema:
                type: string
components:
  parameters:
    PCVersion:
      name: pcversion
      in: query
      description: version of commands which will run on backend
      schema:
        type: string
        enum:
          - 9.6.1
          - 10.1.0
        default: 10.1.0
jhthorsen commented 5 years ago

It haven’t stopped working, it simply haven’t been implemented for v3.

You can see https://github.com/mojolicious/json-validator/issues/55 for a full discussion, and I still don’t know how to do it.

plk commented 5 years ago

Is there anything that I might do to help this along? I believe it stopped at your new JSON::Validator version which didn't fix the defaults thing for me and which currently gives a deprecation warning?

plk commented 5 years ago

Should this work now with the latest versions? Doesn't seem to for me with the test file above. Is there a consensus on what needs to be done? This is still a blocker for me for several migrations to v3.

SebMourlhou commented 4 years ago

I think that a modification around the current line 92 of JSON::Validator::OpenAPI::Mojolicious should do the job :

($exists, $value) = (1, $p->{default}) if !$exists and exists $p->{default};

For v3, the key "default" is placed at an extra level : $p->{schema}

I can create a pull request but I'm not sure if it solves all the cases or if it is the right place to do it ?

SebMourlhou commented 4 years ago

151 made a PR for this issue.

jhthorsen commented 4 years ago

I think I've misunderstood this issue: I thought this was the general "default" wherever inside the schema, but just supporting defaults for the top level is broken in 3.x indeed, and used to work.

Thanks for the PR @SebMourlhou. I'm going to close this issue, since #151 is close to being merged.