jhthorsen / mojolicious-plugin-openapi

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

SpecRenderer escape characters #244

Closed tadamo closed 1 year ago

tadamo commented 1 year ago

Is there any easy or recommended way to prevent the SpecRenderer from adding all the escape characters?

I'm trying to import a spec into a 3rd party tool that will accept a URL, but it's choking on the escape characters. I don't have the ability to preprocess the JSON before it attempts to import.

Sample error:

error converting YAML to JSON: yaml: found unknown escape character


I've been doing some testing with yq and it throws the same error.

curl -s "http://localhost:3000/api" | yq -P
Error: bad file '-': yaml: found unknown escape character

But, if I get the escape characters removed, it works ok.

curl -s "http://localhost:3000/api" | jq -crM | yq -P
basePath: /api
definitions:
  DefaultResponse:
    properties:
...

Thanks for the help! Tom

tadamo commented 1 year ago

I'm all set here. I just did my own Plugin to get around this problem:

package My::SpecRenderer;
use Mojo::Base 'Mojolicious::Plugin';
use Cpanel::JSON::XS;

sub register {
    my ( $self, $app, $config ) = @_;

    $app->routes()->get('/openapi-spec-for-import')->to(
        {   cb => sub {
                my $c = shift;
                $c->res->headers->content_type(
                    'application/json;charset=UTF-8');
                $c->render(
                    data => Cpanel::JSON::XS->new
                        ->canonical->allow_nonref->allow_unknown->allow_blessed
                        ->convert_blessed->stringify_infnan->allow_dupkeys
                        ->encode(
                        $config->{openapi}->validator()->data()
                        )
                );
                return;
            },
        }
    );

    return;
}

1;