Skip to content

Allow for passing in Definition when loading OpenAPI definitions#356

Merged
ahx merged 4 commits intoahx:mainfrom
smizell:smizell/support-passing-in-definition
May 1, 2025
Merged

Allow for passing in Definition when loading OpenAPI definitions#356
ahx merged 4 commits intoahx:mainfrom
smizell:smizell/support-passing-in-definition

Conversation

@smizell
Copy link
Copy Markdown
Contributor

@smizell smizell commented May 1, 2025

This implements the functionality discussed in #353. The approach modifies the Openapi.load method to take either a Definition or a String. It then modifies the Test Coverage functionality to allow registering a Definition object. To make this work, this PR adds a Definition#key method that builds a key from the info.title and info.version. If those aren't present, then it raises an error.

@smizell smizell requested a review from ahx as a code owner May 1, 2025 16:26
Comment thread spec/test_spec.rb
# Verify the plan was registered with the definition key
expect(plan).to be_a(OpenapiFirst::Test::Coverage::Plan)
expect(plan.definition_key).to eq(expected_key)
expect(plan.api_identifier).to eq(expected_key)
Copy link
Copy Markdown
Contributor Author

@smizell smizell May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created a definition_key and api_identifier on the plan. However, it seems like they'll always be the same. Thoughts? Maybe always use api_identifier?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. api_identifier is the better option here.

"Please add these fields to your OpenAPI document."
end

"#{title} @ #{version}"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts? You mentioned using a hash of the definition. Would you prefer that over this?

Copy link
Copy Markdown
Owner

@ahx ahx May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until now openapi_first did not have an opinion about the contents of the OAD…, but I think its' fine the way you did it, because it will lead to a human readable key and the code is easy to follow.

@smizell smizell marked this pull request as draft May 1, 2025 16:46
Comment thread lib/openapi_first.rb
# @param filepath_or_definition [String, Definition] The path to the file or a Definition object
# @return [Definition]
def self.load(filepath, only: nil, &)
def self.load(filepath_or_definition, only: nil, &)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you want .load to be able to accept a Definition instance? In order to avoid something like if path_or_definition.is_a?(Definition) in test.rb?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was treating .load as a single place to handle this. A definition or file path goes in at the edges and then you always get a definition. However, if you prefer to not do that, I'm happy to change it! I leave to you.


def self.for(oad, skip_response: nil)
plan = new(filepath: oad.filepath)
plan = new(definition_key: oad.key, filepath: oad.filepath)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you changed this. This makes sense to me.

Comment thread spec/test/coverage_spec.rb Outdated
after(:each) do
described_class.uninstall
described_class.reset
OpenapiFirst::Test.definitions.clear
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenapiFirst::Test.definitions.clear is already called globally after each test. See spec_helper.rb. This this should not be necessary here.

Comment thread lib/openapi_first/test/coverage/plan.rb Outdated
def initialize(definition_key:, filepath: nil)
@routes = []
@index = {}
@definition_key = definition_key
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's set @api_identifier right here and remove the definition_key reader.

Comment thread README.md Outdated

```ruby
existing_definition = OpenapiFirst.parse(...) # Returns an Openapi::Definition object
definition = OpenapiFirst.load(existing_definition) # Returns the same Definition object
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think describing this special behavior of OpenapiFirst.load (being able to accept a Definition instance) is too much for the Readme. Let's remove that okay?
But I like mentioning OpenapiFirst.parse somewhere in the Readme, because some people might need that, right?

Comment thread spec/test_spec.rb Outdated
end

it 'uses filepath as key for Definition objects with filepath' do
# Start coverage tracking
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate you taking the time to get this deep into the internal APIs.

Comment thread spec/test_spec.rb Outdated
Comment on lines +39 to +41
# Start coverage tracking
OpenapiFirst::Test::Coverage.install

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to call .install for this test to pass

Suggested change
# Start coverage tracking
OpenapiFirst::Test::Coverage.install

Comment thread spec/test_spec.rb Outdated
Comment on lines +58 to +60
# Start coverage tracking
OpenapiFirst::Test::Coverage.install

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Start coverage tracking
OpenapiFirst::Test::Coverage.install

Copy link
Copy Markdown
Owner

@ahx ahx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work @smizell. Looks good to me. Just some smaller changes.
I think the way Definition#key starts with the filename and adds a fall back makes sense.

@smizell
Copy link
Copy Markdown
Contributor Author

smizell commented May 1, 2025

@ahx I've tried to address your feedback. Let me know if I missed something!

@smizell smizell requested a review from ahx May 1, 2025 20:58
@ahx ahx marked this pull request as ready for review May 1, 2025 21:04
@ahx ahx merged commit c316858 into ahx:main May 1, 2025
17 checks passed
@ahx
Copy link
Copy Markdown
Owner

ahx commented May 1, 2025

@smizell Do you need a new version released with this soon? I can take of that, but if you have some time I would like to look at two other issues before releasing a new version.

@smizell smizell deleted the smizell/support-passing-in-definition branch May 2, 2025 02:10
@smizell
Copy link
Copy Markdown
Contributor Author

smizell commented May 2, 2025

No rush on my part! Thanks for merging this in!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants