Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have generated a perl client for a rest api with openapi generator. The slimmed down AuthorizeRequestDTO class is :
package WWW::OpenAPIClient::Object::AuthorizeRequestDTO; 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); use Date::Parse; use DateTime; use base ("Class::Accessor", "Class::Data::Inheritable"); __PACKAGE__->mk_classdata('attribute_map' => {}); __PACKAGE__->mk_classdata('openapi_types' => {}); __PACKAGE__->mk_classdata('method_documentation' => {}); __PACKAGE__->mk_classdata('class_documentation' => {}); # new plain object sub new { my ($class, %args) = @_; my $self = bless {}, $class; $self->init(%args); return $self; } # initialize the object sub init { my ($self, %args) = @_; foreach my $attribute (keys %{$self->attribute_map}) { my $args_key = $self->attribute_map->{$attribute}; $self->$attribute( $args{ $args_key } ); } } __PACKAGE__->method_documentation({ 'username' => { datatype => 'string', base_name => 'Username', description => '', format => '', read_only => '', }, 'password' => { datatype => 'string', base_name => 'Password', description => '', format => '', read_only => '', }, }); __PACKAGE__->openapi_types( { 'username' => 'string', 'password' => 'string' } ); __PACKAGE__->attribute_map( { 'username' => 'USERNAME', 'password' => 'PASSWORD' } ); __PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1;
I'm trying to create a new object by setting the username and password fields but they always come up undefined !!
my %h= ('username' => 'TEST_USER','password' => 'dsds'); my $authobj=WWW::OpenAPIClient::Object::AuthorizeRequestDTO->new(%h); print Dumper $authobj; $VAR1 = bless( { 'username' => undef, 'password' => undef }, 'WWW::OpenAPIClient::Object::AuthorizeRequestDTO' );
The init method does
$self->$attribute
what does that mean ? that it calls a $self->update() and a $self->password() methods? But these do not exist in the code!They are only described in __PACKAGE__->method_documentation
Am I missing something or hasn't the openapi generator generated those two methods?

Replies are listed 'Best First'.
Re: OpenAPI generator issue
by hippo (Archbishop) on Nov 30, 2023 at 13:40 UTC

    If I'm reading that code right, the keys of your arguments hash should be in uppercase, ie:

    my %h = (USERNAME => 'TEST_USER', PASSWORD => 'dsds');

    See if that works for you?


    🦛