SQL-Translator-Parser-OpenAPI
view release on metacpan or search on metacpan
lib/SQL/Translator/Parser/OpenAPI.pm view on Meta::CPAN
my ($tr, $data) = @_;
my $args = $tr->parser_args;
my $openapi_schema = JSON::Validator::OpenAPI::Mojolicious->new->schema($data)->schema;
my %defs = %{ $openapi_schema->get("/definitions") };
DEBUG and _debug('OpenAPI.definitions', \%defs);
my $schema = $tr->schema;
DEBUG and $schema->translator(undef); # reduce debug output
_remove_fields(\%defs, 'x-artifact');
_remove_fields(\%defs, 'x-input-only');
%defs = %{ _merge_allOf(\%defs) };
my $bestmap = definitions_non_fundamental(\%defs);
delete @defs{keys %$bestmap};
%defs = %{ _extract_objects(\%defs, $args) };
%defs = %{ _extract_array_simple(\%defs, $args) };
my (@fixups, %view2real);
%defs = %{ _fixup_addProps(\%defs) };
%defs = %{ _absorb_nonobject(\%defs) };
my $def2relationalid = _decide_id_fields(\%defs);
for my $name (sort keys %defs) {
my ($table, $thesefixups) = _def2table($name, $defs{$name}, $schema, 0, \%view2real, $def2relationalid, $args);
push @fixups, @$thesefixups;
}
my ($newfixups) = _make_many2many(\@fixups, $schema, $def2relationalid, $args);
for my $fixup (@$newfixups) {
_fk_hookup($schema, @{$fixup}{qw(from fromkey to tokey required)}, \%view2real);
}
1;
}
=encoding utf-8
=head1 NAME
SQL::Translator::Parser::OpenAPI - convert OpenAPI schema to SQL::Translator schema
=begin markdown
# PROJECT STATUS
| OS | Build status |
|:-------:|--------------:|
| Linux | [](https://travis-ci.org/mohawk2/SQL-Translator-Parser-OpenAPI) |
[](https://metacpan.org/pod/SQL::Translator::Parser::OpenAPI) [;
$translator->producer("YAML");
$translator->translate($file);
# or...
$ sqlt -f OpenAPI -t MySQL <my-openapi.json >my-mysqlschema.sql
# or, applying an overlay:
$ perl -MHash::Merge=merge -Mojo \
-e 'print j merge map j(f($_)->slurp), @ARGV' \
t/06-corpus.json t/06-corpus.json.overlay |
sqlt -f OpenAPI -t MySQL >my-mysqlschema.sql
=head1 DESCRIPTION
This module implements a L<SQL::Translator::Parser> to convert
a L<JSON::Validator::OpenAPI::Mojolicious> specification to a L<SQL::Translator::Schema>.
It uses, from the given API spec, the given "definitions" to generate
tables in an RDBMS with suitable columns and types.
To try to make the data model represent the "real" data, it applies heuristics:
=over
=item *
to remove object definitions considered non-fundamental; see
L</definitions_non_fundamental>.
=item *
for definitions that have C<allOf>, either merge them together if there
is a C<discriminator>, or absorb properties from referred definitions
=item *
creates object definitions for any properties that are an object
=item *
creates object definitions for any properties that are an array of simple
OpenAPI types (e.g. C<string>)
=item *
creates object definitions for any objects that are
C<additionalProperties> (i.e. freeform key/value pairs), that are
key/value rows
=item *
absorbs any definitions that are in fact not objects, into the referring
property
=item *
injects foreign-key relationships for array-of-object properties, and
creates many-to-many tables for any two-way array relationships
=back
=head1 ARGUMENTS
=head2 snake_case
If true, will create table names that are not the definition names, but
instead the pluralised snake_case version, in line with SQL convention. By
default, the tables will be named after simply the definitions.
=head1 PACKAGE FUNCTIONS
( run in 0.491 second using v1.01-cache-2.11-cpan-efa8479b9fe )