package Whelk::Formatter::JSON;
$Whelk::Formatter::JSON::VERSION = '1.04';
use Kelp::Base 'Whelk::Formatter';
attr response_format => sub { 'json' };
1;
odos')
->code_is(200)
->json_cmp([]);
$t->request(GET '/todos/1')
->code_is(404);
$t->request(
PUT '/todos',
Content_Type => 'application/json',
Content => $app->json->encode({name => 't1', co
ntent => 't1 content'})
)
->code_is(200)
->json_cmp({id => 1});
$t->request(
PUT '/todos',
Content_Type => 'application/json',
Content => $app->json->encode({name => 't2', content => 't2 conten
)
->json_cmp({id => 2});
$t->request(GET '/todos/1')
->code_is(200)
->json_cmp({name => 't1', content => 't1 content', date => ignore()});
$t->request(GET '/todos/2')
->code_is(200)
->json_cmp(
ead1 DESCRIPTION
Whelk schema is an easy validation language for defining validations similar to
JSON Schema. It's designed to be a bit more concise and crafted specifically
for Whelk needs.
Whelk s
type. The value must not be a reference and the output will be coerced
to a string value. Unlike JSON schema, this also accepts numbers.
Extra configuration fields:
=over
=item * default
A defaul
tput value to JSON::PP::true and
JSON::PP::false objects.
Same extra configuration as in L</string>.
=head3 number
A numeric type. Will coerce the output value to a number. Unlike JSON schema,
this
my ($self, $app) = @_;
my $app_encoders = $app->encoder_modules;
my %supported = (
json => 'application/json',
yaml => 'text/yaml',
);
foreach my $encoder (keys %supported) {
delete $suppo
my ($self, $app) = @_;
my $format = $self->match_format($app);
return
$format eq 'json' ? $app->req->json_content :
$format eq 'yaml' ? $app->req->yaml_content :
undef;
}
sub format_respon
rmatter';
# at the very least, this attribute must be given a default
attr response_format => 'json';
=head1 DESCRIPTION
Whelk::Formatter is a base class for formatters. Formatter's job is to
imp
es and error handling. Wrappers do not handle
encoding requests and responses (for example with C<JSON>), that's a job for
L<Whelk::Formatter>.
In addition, wrapper decides how to treat failures. It
::data,
{
modules => [qw(JSON YAML Whelk)],
modules_init => {
Routes => {
base => 'Whelk::Resource',
rebless => 1,
fatal => 1,
},
JSON => {
utf8 => 0, # wil
YAML => {
kelp_extensions => 1,
boolean => 'perl,JSON::PP',
},
},
persistent_controllers => 1,
encoders => {
json => {
openapi => {
pretty => 1,
canonical
se;
use Carp;
use Kelp::Util;
use Scalar::Util qw(blessed);
use Clone qw();
use Data::Dumper;
use JSON::PP;
use Whelk::Schema::ExtraRule;
# no import loop, load Whelk::Schema for child classes
requi
@{$self->rules // []}]);
$self->_resolve;
return $self;
}
sub _bool
{
return pop() ? JSON::PP::true : JSON::PP::false;
}
sub _resolve { }
sub _build
{
my ($self, $item) = @_;
if (blessed $it
n) {
$res->{description} = $self->description;
}
if ($self->nullable) {
$res->{nullable} = JSON::PP::true;
}
return $res;
}
sub exhale
{
my ($self, $value) = @_;
...;
}
sub inhale
{
my
ition::Boolean::VERSION = '1.04';
use Whelk::StrictBase 'Whelk::Schema::Definition::_Scalar';
use JSON::PP;
use List::Util qw(none);
sub openapi_dump
{
my ($self, $openapi_obj, %hints) = @_;
my $r
_ } (JSON::PP::true, JSON::PP::false);
}
else {
$inhaled = 'boolean'
if none { $value eq $_ } (1, 0, !!1, !!0);
}
return $inhaled;
}
sub _exhale
{
return pop() ? JSON::PP::true : JSON::PP:
formatter
inhale_response
);
}
$args->{wrapper} //= 'Simple';
$args->{formatter} //= 'JSON';
$self->inhale_response($args->{inhale_response})
if defined $args->{inhale_response};
$se
w(JSON YAML Whelk)],
modules_init => {
Routes => {
base => 'MyApp/Controller',
rebless => 1,
},
Whelk => {
resources => {
Res => '/',
},
openapi => '/openapi.json',
elp you build the required hashes.
Data types are defined in Whelk's own format, very similar to JSON schema but
simpler and not as strict when it comes to validation. Reusable, named schemas
can be
and some utility
functions to handle formats on Kelp request and response objects. By default,
a JSON formatter is used. See L<Whelk::Formatter> for more information.
Lastly, Whelk uses I<wrappers>
ting up the app, you can immediatelly execute C<plackup> and navigate
to C<localhost:5000/openapi.json> to get an OpenAPI document describing the
generated resource. Tools like L<Swagger Editor|https: