mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-11 08:31:21 +00:00
seralization and deserialization work, get_pet_by_id and
update_pet_with_form work
This commit is contained in:
@@ -8,7 +8,7 @@ import java.util.*;
|
||||
import java.io.File;
|
||||
|
||||
public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
protected String invokerPackage = "com.wordnik.client";
|
||||
protected String invokerPackage = "SwaggerClient";
|
||||
protected String groupId = "com.wordnik";
|
||||
protected String artifactId = "swagger-client";
|
||||
protected String artifactVersion = "1.0.0";
|
||||
@@ -59,7 +59,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("List", "array");
|
||||
typeMapping.put("map", "map");
|
||||
|
||||
supportingFiles.add(new SupportingFile("Swagger.mustache", "", "Swagger.pl"));
|
||||
supportingFiles.add(new SupportingFile("APIClient.mustache", "lib/WWW/" + invokerPackage, "APIClient.pm"));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,11 +69,11 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
@Override
|
||||
public String apiFileFolder() {
|
||||
return outputFolder + "/WWW/Swagger/" + apiPackage().replace('.', File.separatorChar);
|
||||
return outputFolder + "/lib/WWW/" + invokerPackage + apiPackage().replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
public String modelFileFolder() {
|
||||
return outputFolder + "/WWW/Swagger/" + modelPackage().replace('.', File.separatorChar);
|
||||
return outputFolder + "/lib/WWW/" + invokerPackage + "/" + modelPackage().replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,229 +0,0 @@
|
||||
package WWW::Swagger::Swagger;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
|
||||
use LWP::UserAgent;
|
||||
use HTTP::Headers;
|
||||
use HTTP::Response;
|
||||
use HTTP::Status;
|
||||
use URI::Query;
|
||||
use JSON;
|
||||
|
||||
use Scalar::Util;
|
||||
|
||||
# class variables
|
||||
my $ua = LWP::UserAgent->new;
|
||||
my $http_user_agent = 'Perl-Swagger'; # HTTP user-agent
|
||||
my $http_timeout; #timeout
|
||||
my $base_url;
|
||||
|
||||
|
||||
sub new
|
||||
{
|
||||
my $class = shift;
|
||||
my %args = @_;
|
||||
|
||||
return bless \%args, $class;
|
||||
}
|
||||
|
||||
# Set the user agent of the API client
|
||||
#
|
||||
# @param string $user_agent The user agent of the API client
|
||||
#
|
||||
sub set_user_agent {
|
||||
my $user_agent = shift;
|
||||
$http_user_agent= $user_agent;
|
||||
}
|
||||
|
||||
# Set timeout
|
||||
#
|
||||
# @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
|
||||
#
|
||||
sub set_timeout {
|
||||
my $seconds = shift;
|
||||
if (!looks_like_number($seconds)) {
|
||||
croak('Timeout variable must be numeric.');
|
||||
}
|
||||
$http_timeout = $seconds;
|
||||
}
|
||||
|
||||
# make the HTTP request
|
||||
# @param string $resourcePath path to method endpoint
|
||||
# @param string $method method to call
|
||||
# @param array $queryParams parameters to be place in query URL
|
||||
# @param array $postData parameters to be placed in POST body
|
||||
# @param array $headerParams parameters to be place in request header
|
||||
# @return mixed
|
||||
sub call_api {
|
||||
my $self = shift;
|
||||
my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data) = @_;
|
||||
|
||||
my $headers = HTTP::Headers->new(%$header_params);
|
||||
|
||||
my $_url = $base_url . $resource_path;
|
||||
|
||||
# build query
|
||||
if ($query_params) {
|
||||
$_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify });
|
||||
}
|
||||
|
||||
# body data
|
||||
my $_body_data = $post_params ? $post_params : $body_data;
|
||||
|
||||
# Make the HTTP request
|
||||
my $_request = HTTP::Request->new(
|
||||
$method, $_url, $headers, $_body_data
|
||||
);
|
||||
|
||||
$ua->timeout($http_timeout);
|
||||
$ua->agent($http_user_agent);
|
||||
my $_response = $ua->request($_request);
|
||||
|
||||
if (!$_response->is_success) {
|
||||
#TODO croak("Can't connect ot the api ($_response{code}): $_response{message}");
|
||||
}
|
||||
|
||||
return $_response->content;
|
||||
|
||||
}
|
||||
|
||||
|
||||
# Build a JSON POST object
|
||||
#sub sanitize_for_serialization
|
||||
#{
|
||||
# my $data = shift;
|
||||
# if (is_scalar($data) || null === $data) {
|
||||
# $sanitized = $data;
|
||||
# } else if ($data instanceof \DateTime) {
|
||||
# $sanitized = $data->format(\DateTime::ISO8601);
|
||||
# } else if (is_array($data)) {
|
||||
# foreach ($data as $property => $value) {
|
||||
# $data[$property] = $this->sanitizeForSerialization($value);
|
||||
# }
|
||||
# $sanitized = $data;
|
||||
# } else if (is_object($data)) {
|
||||
# $values = array();
|
||||
# foreach (array_keys($data::$swaggerTypes) as $property) {
|
||||
# $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
|
||||
# }
|
||||
# $sanitized = $values;
|
||||
# } else {
|
||||
# $sanitized = (string)$data;
|
||||
# }
|
||||
#
|
||||
# return $sanitized;
|
||||
#}
|
||||
|
||||
|
||||
# Take value and turn it into a string suitable for inclusion in
|
||||
# the path, by url-encoding.
|
||||
# @param string $value a string which will be part of the path
|
||||
# @return string the serialized object
|
||||
sub to_path_value {
|
||||
my $value = shift;
|
||||
return uri_escape(to_string($value));
|
||||
}
|
||||
|
||||
|
||||
# Take value and turn it into a string suitable for inclusion in
|
||||
# the query, by imploding comma-separated if it's an object.
|
||||
# If it's a string, pass through unchanged. It will be url-encoded
|
||||
# later.
|
||||
# @param object $object an object to be serialized to a string
|
||||
# @return string the serialized object
|
||||
sub to_query_value {
|
||||
my $object = shift;
|
||||
if (is_array($object)) {
|
||||
return implode(',', $object);
|
||||
} else {
|
||||
return toString($object);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Take value and turn it into a string suitable for inclusion in
|
||||
# the header. If it's a string, pass through unchanged
|
||||
# If it's a datetime object, format it in ISO8601
|
||||
# @param string $value a string which will be part of the header
|
||||
# @return string the header string
|
||||
sub to_header_value {
|
||||
my $value = shift;
|
||||
return to_string($value);
|
||||
}
|
||||
|
||||
# Take value and turn it into a string suitable for inclusion in
|
||||
# the http body (form parameter). If it's a string, pass through unchanged
|
||||
# If it's a datetime object, format it in ISO8601
|
||||
# @param string $value the value of the form parameter
|
||||
# @return string the form string
|
||||
sub to_form_value {
|
||||
my $value = shift;
|
||||
return to_string($value);
|
||||
}
|
||||
|
||||
# Take value and turn it into a string suitable for inclusion in
|
||||
# the parameter. If it's a string, pass through unchanged
|
||||
# If it's a datetime object, format it in ISO8601
|
||||
# @param string $value the value of the parameter
|
||||
# @return string the header string
|
||||
sub to_string {
|
||||
my $value = shift;
|
||||
if (ref($value) eq "DateTime") { # datetime in ISO8601 format
|
||||
return $value->datetime();
|
||||
}
|
||||
else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Deserialize a JSON string into an object
|
||||
#
|
||||
# @param object $object object or primitive to be deserialized
|
||||
# @param string $class class name is passed as a string
|
||||
# @return object an instance of $class
|
||||
#sub deserialize
|
||||
#{
|
||||
# my ($data, $class) = @_;
|
||||
# if (null === $data) {
|
||||
# $deserialized = null;
|
||||
# } elseif (substr($class, 0, 4) == 'map[') {
|
||||
# $inner = substr($class, 4, -1);
|
||||
# $values = array();
|
||||
# if(strrpos($inner, ",") !== false) {
|
||||
# $subClass_array = explode(',', $inner, 2);
|
||||
# $subClass = $subClass_array[1];
|
||||
# foreach ($data as $key => $value) {
|
||||
# $values[] = array($key => self::deserialize($value, $subClass));
|
||||
# }
|
||||
# }
|
||||
# $deserialized = $values;
|
||||
# } elseif (strcasecmp(substr($class, 0, 6),'array[') == 0) {
|
||||
# $subClass = substr($class, 6, -1);
|
||||
# $values = array();
|
||||
# foreach ($data as $key => $value) {
|
||||
# $values[] = self::deserialize($value, $subClass);
|
||||
# }
|
||||
# $deserialized = $values;
|
||||
# } elseif ($class == 'DateTime') {
|
||||
# $deserialized = new \DateTime($data);
|
||||
# } elseif (in_array($class, array('string', 'int', 'float', 'bool'))) {
|
||||
# settype($data, $class);
|
||||
# $deserialized = $data;
|
||||
# } else {
|
||||
# $instance = new $class();
|
||||
# foreach ($instance::$swaggerTypes as $property => $type) {
|
||||
# if (isset($data->$property)) {
|
||||
# $original_property_name = $instance::$attributeMap[$property];
|
||||
# $instance->$property = self::deserialize($data->$original_property_name, $type);
|
||||
# }
|
||||
# }
|
||||
# $deserialized = $instance;
|
||||
# }
|
||||
#
|
||||
# return $deserialized;
|
||||
#}
|
||||
|
||||
1;
|
||||
@@ -17,29 +17,44 @@
|
||||
# NOTE: This class is auto generated by the swagger code generator program.
|
||||
# Do not edit the class manually.
|
||||
#
|
||||
package WWW::{{invokerPackage}}::{{classname}};
|
||||
|
||||
require 5.6.0;
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
use Exporter;
|
||||
use Carp qw( croak );
|
||||
use Log::Any qw($log);
|
||||
|
||||
|
||||
#use WWW::Swagger::Model::Category;
|
||||
#use WWW::Swagger::Model::Pet;
|
||||
|
||||
{{#operations}}
|
||||
package WWW::Swagger::{{classname}};
|
||||
|
||||
our $VERSION = '2.09';
|
||||
use WWW::{{invokerPackage}}::APIClient;
|
||||
|
||||
our @EXPORT_OK = qw(
|
||||
{{#operation}}{{{nickname}}}
|
||||
{{/operation}}
|
||||
);
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my $options = shift;
|
||||
my $default_api_client = WWW::{{invokerPackage}}::APIClient->new;
|
||||
#TODO fix default
|
||||
#my (%arg) = (
|
||||
# 'api_client' => $default_api_client,
|
||||
# @_
|
||||
#);
|
||||
|
||||
croak("You must supply an API client")
|
||||
unless $options->{api_client};
|
||||
#croak("You must supply an API client")
|
||||
# unless $options->{api_client};
|
||||
|
||||
my $self = {
|
||||
api_client => $options->{api_client}
|
||||
#api_client => $options->{api_client}
|
||||
api_client => $default_api_client
|
||||
};
|
||||
|
||||
bless $self, $class;
|
||||
@@ -56,63 +71,72 @@ sub new {
|
||||
{{/allParams}} # @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
|
||||
#
|
||||
sub {{nickname}} {
|
||||
my $self = shift;
|
||||
my %args = @_;
|
||||
my ($self, $args) = @_;
|
||||
|
||||
# parse inputs
|
||||
my $resource_path = "{{path}}";
|
||||
$resource_path =~ s/{format}/json/;
|
||||
{{#allParams}}{{#required}}
|
||||
# verify the required parameter '{{paramName}}' is set
|
||||
unless (exists $args->{'{{paramName}}'}) {
|
||||
croak("Missing the required parameter '{{paramName}}' when calling {{nickname}}");
|
||||
}
|
||||
{{/required}}{{/allParams}}
|
||||
|
||||
my $method = "{{httpMethod}}";
|
||||
my $query_params = {};
|
||||
my $header_params = {};
|
||||
my $form_params = {};
|
||||
|
||||
$header_params->{'Accept'} = '{{#produces}}{{mediaType}}{{#hasMore}},{{/hasMore}}{{/produces}}';
|
||||
$header_params->{'Content-Type'} = '{{#consumes}}{{mediaType}}{{#hasMore}},{{/hasMore}}{{/consumes}}';
|
||||
# parse inputs
|
||||
my $_resource_path = '{{path}}';
|
||||
$_resource_path =~ s/{format}/json/; # default format to json
|
||||
|
||||
my $_method = '{{httpMethod}}';
|
||||
my $query_params = {};
|
||||
my $header_params = {};
|
||||
my $form_params = {};
|
||||
|
||||
my $_header_accept = '{{#produces}}{{mediaType}}{{#hasMore}}, {{/hasMore}}{{/produces}}';
|
||||
if ($_header_accept ne '') {
|
||||
$header_params->{'Accept'} = $_header_accept;
|
||||
}
|
||||
my @_header_content_type = ({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}});
|
||||
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
|
||||
|
||||
{{#queryParams}} # query params
|
||||
if($args{ {{paramName}} }) {
|
||||
$query_params->{'{{baseName}}'} = $self->api_client->to_query_value($args{ {{paramName}} });
|
||||
if ( exists $args->{'{{paramName}}'}) {
|
||||
$query_params->{'{{baseName}}'} = WWW::SwaggerClient::APIClient::to_query_value($args->{'{{paramName}}'});
|
||||
}{{/queryParams}}
|
||||
{{#headerParams}} # header params
|
||||
if($args{ {{paramName}} }) {
|
||||
$header_params->{'{{baseName}}'} = $self->apiClient->to_header_value($args{ {{paramName}} });
|
||||
if ( exists $args->{'{{paramName}}'}) {
|
||||
$header_params->{'{{baseName}}'} = WWW::SwaggerClient::APIClient::to_header_value($args->{'{{paramName}}'});
|
||||
}{{/headerParams}}
|
||||
{{#pathParams}} # path params
|
||||
if( $args{ {{paramName}} }) {
|
||||
my $base_variable = "{" + "{{baseName}}" + "}";
|
||||
my $base_value = $self->api_client->to_path_value($args{ {{paramName}} });
|
||||
$resource_path = s/$base_variable/$base_value/;
|
||||
if ( exists $args->{'{{paramName}}'}) {
|
||||
my $_base_variable = "{" . "{{baseName}}" . "}";
|
||||
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args->{'{{paramName}}'});
|
||||
$_resource_path =~ s/$_base_variable/$_base_value/g;
|
||||
}{{/pathParams}}
|
||||
{{#formParams}} # form params
|
||||
if ($args{ {{paramName}} }) {
|
||||
$form_params->{'{{baseName}}'} = {{#isFile}}'@' . {{/isFile}}$self->api_client->to_form_value($args{ {{paramName}} });
|
||||
if ( exists $args->{'{{paramName}}'} ) {
|
||||
$form_params->{'{{baseName}}'} = {{#isFile}}'@' . {{/isFile}}WWW::SwaggerClient::APIClient::to_form_value($args->{'{{paramName}}'});
|
||||
}{{/formParams}}
|
||||
my $body;
|
||||
my $_body_data;
|
||||
{{#bodyParams}} # body params
|
||||
if (isset(${{paramName}})) {
|
||||
$body = ${{paramName}};
|
||||
if ( exists $args->{'{{paramName}}'}) {
|
||||
$_body_data = $args->{'{{paramName}}'};
|
||||
}{{/bodyParams}}
|
||||
|
||||
# for HTTP post (form)
|
||||
$body = $body ? undef : $form_params;
|
||||
|
||||
if ($header_params->{'Content-Type'} eq "application/x-www-form-urlencoded") {
|
||||
$body = http_build_query($body);
|
||||
}
|
||||
#$_body_data = $_body ? undef : $form_params;
|
||||
|
||||
# make the API Call
|
||||
my $response = $self->api_client->call_api($resource_path, $method,
|
||||
$query_params, $body,
|
||||
$header_params);
|
||||
|
||||
{{#returnType}}if(!$response) {
|
||||
{{#returnType}}my $response = $self->{api_client}->call_api($_resource_path, $_method,
|
||||
$query_params, $form_params,
|
||||
$header_params, $_body_data);
|
||||
if (!$response) {
|
||||
return;
|
||||
}
|
||||
|
||||
my $response_object = $self->api_client->deserialize($response, '{{returnType}}');
|
||||
return $response_object;{{/returnType}}
|
||||
my $_response_object = $self->{api_client}->deserialize('{{returnType}}', $response);
|
||||
return $_response_object;{{/returnType}}
|
||||
{{^returnType}}$self->{api_client}->call_api($_resource_path, $_method,
|
||||
$query_params, $form_params,
|
||||
$header_params, $_body_data);
|
||||
return;
|
||||
{{/returnType}}
|
||||
}
|
||||
{{/operation}}
|
||||
{{newline}}
|
||||
|
||||
@@ -1,66 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright 2015 Reverb Technologies, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* $model.description$
|
||||
*
|
||||
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
|
||||
*
|
||||
*/
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
package WWW::{{invokerPackage}}::Model::{{classname}};
|
||||
|
||||
class {{classname}} implements ArrayAccess {
|
||||
static $swaggerTypes = array(
|
||||
require 5.6.0;
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
use JSON qw(decode_json);
|
||||
use Data::Dumper;
|
||||
use Module::Runtime qw(use_module);
|
||||
use Log::Any qw($log);
|
||||
|
||||
|
||||
#
|
||||
#{{description}}
|
||||
#
|
||||
#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
|
||||
#
|
||||
|
||||
my $swagger_types = {
|
||||
{{#vars}}'{{name}}' => '{{{datatype}}}'{{#hasMore}},
|
||||
{{/hasMore}}{{/vars}}
|
||||
);
|
||||
};
|
||||
|
||||
static $attributeMap = array(
|
||||
my $attribute_map = {
|
||||
{{#vars}}'{{name}}' => '{{baseName}}'{{#hasMore}},
|
||||
{{/hasMore}}{{/vars}}
|
||||
);
|
||||
};
|
||||
|
||||
{{#vars}}{{#description}}
|
||||
/**
|
||||
* {{{description}}}
|
||||
*/{{/description}}
|
||||
public ${{name}}; /* {{{datatype}}} */{{/vars}}
|
||||
# new object
|
||||
sub new {
|
||||
my ($class, $args) = @_;
|
||||
my $self = {
|
||||
{{#vars}}#{{#description}}{{{description}}}{{/description}}
|
||||
'{{name}}' => $args->{'{{baseName}}'}{{#hasMore}},
|
||||
{{/hasMore}}{{/vars}}
|
||||
};
|
||||
|
||||
public function __construct(array $data) {
|
||||
{{#vars}}$this->{{name}} = $data["{{name}}"];{{#hasMore}}
|
||||
{{/hasMore}}{{/vars}}
|
||||
return bless $self, $class;
|
||||
}
|
||||
|
||||
# return json string
|
||||
sub to_hash {
|
||||
return decode_json(JSON->new->convert_blessed->encode( shift ));
|
||||
}
|
||||
|
||||
# used by JSON for serialization
|
||||
sub TO_JSON {
|
||||
my $self = shift;
|
||||
my $_data = {};
|
||||
foreach my $_key (keys $attribute_map) {
|
||||
if (defined $self->{$attribute_map->{$_key}}) {
|
||||
$_data->{$attribute_map->{$_key}} = $self->{$_key};
|
||||
}
|
||||
}
|
||||
return $_data;
|
||||
}
|
||||
|
||||
# from json string
|
||||
sub from_hash {
|
||||
my ($self, $hash) = @_;
|
||||
# loop through attributes and use swagger_types to deserialize the data
|
||||
while ( my ($_key, $_type) = each $swagger_types) {
|
||||
if ($_type =~ /^array\[/i) { # array
|
||||
my $_subclass = substr($_type, 6, -1);
|
||||
my @_array = ();
|
||||
foreach my $_element (@{$hash->{$attribute_map->{$_key}}}) {
|
||||
push @_array, $self->_deserialize($_subclass, $_element);
|
||||
}
|
||||
$self->{$_key} = \@_array;
|
||||
} elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime
|
||||
$self->{$_key} = $self->_deserialize($_type, $hash->{$_key});
|
||||
} else {
|
||||
$log->debugf("warning: %s not defined\n", $_key);
|
||||
}
|
||||
}
|
||||
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->$offset);
|
||||
}
|
||||
|
||||
public function offsetGet($offset) {
|
||||
return $this->$offset;
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value) {
|
||||
$this->$offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset) {
|
||||
unset($this->$offset);
|
||||
return $self;
|
||||
}
|
||||
|
||||
# deserialize non-array data
|
||||
sub _deserialize {
|
||||
my ($self, $type, $data) = @_;
|
||||
$log->debugf("deserializing %s with %s",Dumper($data), $type);
|
||||
|
||||
if ($type eq 'DateTime') {
|
||||
return DateTime->from_epoch(epoch => str2time($data));
|
||||
} elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) {
|
||||
return $data;
|
||||
} else { # hash(model)
|
||||
my $_instance = eval "WWW::SwaggerClient::Model::$type->new()";
|
||||
return $_instance->from_hash($data);
|
||||
}
|
||||
}
|
||||
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
|
||||
1;
|
||||
|
||||
Reference in New Issue
Block a user