jhthorsen / mojolicious-plugin-openapi

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

More content negotion in v3 #113

Closed plk closed 5 years ago

plk commented 5 years ago

Sorry to be a pain. I am still working on getting a v3 spec to work and I'm getting 500 errors for everything beyond the simple example of #110 (which works now). Here's a simple example of a post with a simple body which give me the same No responses rules defined for Accept application/json error.

use v5.16;
use Mojo::Base -strict;
use Mojolicious::Lite;

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

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

my $ua = Mojo::UserAgent->new;
my $res = $ua->post("/v1/test" => {Accept => 'application/json'} =>
                                   json => {username => 'testuser'})->res;

package main;
__DATA__
@@ file.yaml
openapi: 3.0.0
info:
  title: Title
  description: Description
  version: 1.0.0
servers:
  - url: 'https://server1.com/v1'
  - url: 'https://server2.com/v1'
paths:
  '/test':
    post:
      operationId: "File"
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Test"
        required: true
      responses:
        "200":
          content:
            application/json:
              schema:
                type: string
components:
  schemas:
    Test:
      type: object
      required:
        - username
      properties:
        username:
          type: string
jhthorsen commented 5 years ago

Well... there’s a reason why you have to be a pain: My implementation isn’t good enough 🙈😭😉

I don’t know when I will find time to look at this, but this will be a priority once I free up some time.

plk commented 5 years ago

Any chance to look at this? I really want to move a project to OpenAPI3 ...

jhthorsen commented 5 years ago

Your example is invalid @plk: You have "post" in your spec, but "get" in the example application.

I'll reopen the issue if there is still and issue after you change the code.

jhthorsen commented 5 years ago

Also, I did convert your script to an OK test:

use Mojo::Base -strict;
use Test::Mojo;

use Mojolicious::Lite;
post '/test' => sub {
  my $c = shift->openapi->valid_input or return;
  $c->render(status => 200, openapi => 'test');
}, 'File';

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

my $t = Test::Mojo->new;
$t->post_ok('/v1/test', {Accept => 'application/json'} => json => {username => 'testuser'})
  ->status_is(200)->content_is('"test"');

done_testing;

__DATA__
@@ file.yaml
openapi: 3.0.0
info:
  title: Title
  description: Description
  version: 1.0.0
servers:
  - url: 'https://server1.com/v1'
  - url: 'https://server2.com/v1'
paths:
  '/test':
    post:
      operationId: "File"
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Test"
        required: true
      responses:
        "200":
          description: Description
          content:
            application/json:
              schema:
                type: string
components:
  schemas:
    Test:
      type: object
      required:
        - username
      properties:
        username:
          type: string