jhthorsen / openapi-client

A client for talking to an Open API powered server
11 stars 17 forks source link

Cannot set Mojo::URL host or scheme after construction when constructing client with base_url param #19

Closed ravenhall closed 5 years ago

ravenhall commented 5 years ago

When constructing an OpenAPI::Client with a bare URL as the value of the base_url param, the host and scheme cannot be changed after construction.

However, if you instead construct a Mojo::URL object and pass that as a base_url, the methods can be accessed.

I'm including a test case that I wrote that demonstrates the issue below:

use Test::More;
use OpenAPI::Client;

my $client1;
my $client2;
my $base_url1 = Mojo::URL->new('http://example.com');
my $base_url2 = 'http://example.com';
my $otherhost = 'somewhereelse.com';

# Client 1: Use a Mojo::URL object for base_url

# Construct a client with a Mojo::URL object
ok($client1 = OpenAPI::Client->new("file://testswagger.yaml", base_url => $base_url1), "Constructed client1 - OpenAPI::Client with Mojo::URL object");

# Try to set client to use a different host
ok($client1->base_url->host($otherhost), "Set client1 to use host $host");

# Try to set client to use HTTPS
ok($client1->base_url->scheme('https'), "Set client1 to use https");

# Client 2: Use a bare URL for base_url

# Construct a client with a bare URL
ok($client2 = OpenAPI::Client->new("file://testswagger.yaml", base_url => $base_url2), "Constructed client2 - OpenAPI::Client with bare URL");

# Try to set client1 to use a different host
ok($client2->base_url->host($host), "Set client2 to use $host");

# Try to set client1 to use HTTPS
ok($client2->base_url->scheme('https'), "Set client2 to use https");

done_testing();

The test swagger spec:

swagger: '2.0'
info:
  version: 1.0.0
  title: Simple example API
  description: An API to illustrate Swagger
paths:
  /list:
    get:
      description: Returns a list of stuff              
      responses:
        200:
          description: Successful response
jhthorsen commented 5 years ago

Where does it say that you can use a string instead of a Mojo::URL object as "base_url"?

Also, what are you trying to accomplish with this issue? Do you want OpenAPI::Client to coerce a plain string into Mojo::URL? Or do you want some validation of base_url to be added? Or something else?

The subject is a bit confusing as well. Do you mean "Cannot set Mojo::URL host or scheme after construction with a plain scalar/string base_url"?

jhthorsen commented 5 years ago

I'm closing this since the issue is inactive. Let me know if I've misunderstood something.