Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixing in roles to generated classes #35

Closed
rabbiveesh opened this issue Jan 17, 2022 · 1 comment
Closed

Mixing in roles to generated classes #35

rabbiveesh opened this issue Jan 17, 2022 · 1 comment
Assignees
Labels

Comments

@rabbiveesh
Copy link
Contributor

rabbiveesh commented Jan 17, 2022

The Problem

I've been setting up API clients for amazon's advertising APIs, and in the course of doing so, it was most convenient to mix in certain shared behaviors with a role.

The issue that I'm having is that I can't mix in roles nicely to the generated classes. For example:

OpenAPI::Client->with_roles('My::Awesome::Role')->new('path/to/spec.yaml')

Will still return a subclass of OpenAPI::Client, rather than a subclass of the package created by mixing in my role.

The alternative of

OpenAPI::Client->new('path/to/spec.yaml')->with_roles('My::Awesome::Role')

works, except that the behavior is surprising, b/c the role never gets instantiated, so the defaults for fields in the role are not set.

For reference, I use Moo::Role to create attributes (access to redis for storing access tokens, for example).

Ideas

Either the constructor for OpenAPI::Client should use a dynamic classname when generating the child class on lines 71-74, or there should be an extra argument for putting more stuff into the generated class.
Or perhaps give access to the generated class before instantiating it?

Thanks for this wonderful module!

@jhthorsen jhthorsen self-assigned this Jan 20, 2022
@jhthorsen
Copy link
Owner

Thanks for the feedback!

I'm sorry, but I don't see any issue with doing "with_roles" after the class has been generated. The documentation states that a new class is generated when calling new:

Returns an object of a generated class, with methods generated from the Open
API specification located at C<$specification>. See L<JSON::Validator/schema>
for valid versions of C<$specification>.

I don't mind a PR though, as long as it's not too complex.

rabbiveesh added a commit to rabbiveesh/openapi-client that referenced this issue May 31, 2022
jhthorsen added a commit that referenced this issue Jun 3, 2022
 - Allow inheritance and roles to be applied before new() #35 #37
   Contributor: Veesh Goldman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants