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

Move swagger param description creation under generator/swagger/param_description #810

Merged

Conversation

PanosCodes
Copy link
Contributor

@PanosCodes PanosCodes commented Jan 12, 2023

Why

I'm woking on PR to make generators pluggable and allow external generators to be used by apipie.
This PR refactors and moves the param description generation under lib/apipie/generator/swagger/param_description/ namespace.

How

Introduced Apipie::Generator::Swagger::ParamDescription::Composite

Nested ParamDescritpions like

param :resource_param, Hash, :desc => 'Param description for all methods' do
  param :ausername, String, :desc => "Username for login", :required => true
  param :apassword, String, :desc => "Password for login", :required => true
end

are converted to swagger by Apipie::Generator::Swagger::ParamDescription::Composite that recursively loops though it's children.

json_schema_obj_from_params_array and json_schema_param_defs_from_params_array replaced by Apipie::Generator::Swagger::Composite


Introduced Apipie::Generator::Swagger::ParamDescription::Builder

swagger_atomic_param replaced by Apipie::Generator::Swagger::ParamDescription::Builder that provides an interface responsible for constructing the Swagger parameters.


ParamDescription Swagger parameters

Swagger parameters that have some king of logic like description, in, name, type, items, enum

live under their respected classes

  • Apipie::Generator::Swagger::ParamDescription::Description
  • Apipie::Generator::Swagger::ParamDescription::In
  • Apipie::Generator::Swagger::ParamDescription::Name
  • Apipie::Generator::Swagger::ParamDescription::Type (This one also handles items & enum attributes)

and get instantiated through the Apipie::Generator::Swagger::ParamDescription::Builder 's api.

Missing attributes

Instead of creating an OpenStruct the mimics ParamDescription we create a ParamDescription from that is required and has in it's options the information that is created from path (added_from_path) and not from the DSL. (ParamDescription.create_for_missing_param)

Final thoughts

Although this PR adds more lines than it removes, I think it makes it easer to navigate the generator code by having different classes for different responsibilities.

@PanosCodes PanosCodes force-pushed the swagger-param-description-builder branch from 5757703 to 2ddf301 Compare January 12, 2023 18:56
@PanosCodes PanosCodes changed the title Swagger param description builder Move swagger param description under generator/swagger Jan 12, 2023
@PanosCodes PanosCodes force-pushed the swagger-param-description-builder branch 8 times, most recently from 2f9d697 to f2bdaf1 Compare January 15, 2023 09:58
instead of `OpenStruct`

Instead of creating an OpenStruct the mimics `ParamDescription` it
creates a `ParamDescription` that is marked as `required` and has in
it's options the information that is created from path (`added_from_path`)
and not from the DSL.
- Introduces `Apipie::Generator::Swagger::ParamDescription::Composite`
for nested param descritpions, that recursively loops though it's children.

Replaces the `json_schema_obj_from_params_array` & `json_schema_param_defs_from_params_array`
with `Apipie::Generator::Swagger::ParamDescription::Composite`

- Introduces `Apipie::Generator::Swagger::ParamDescription::Builder`
that provides a builder interface responsible for constructing the
Swagger parameters.

Replaces the `swagger_atomic_param` method with `Apipie::Generator::Swagger::ParamDescription::Builder`

Swagger parameters that have some king of logic like
`description`, `in`, `name`, `type`, `items` & `enum` live under their
respected classes

- `Apipie::Generator::Swagger::ParamDescription::Description`
- `Apipie::Generator::Swagger::ParamDescription::In`
- `Apipie::Generator::Swagger::ParamDescription::Name`
- `Apipie::Generator::Swagger::ParamDescription::Type` (This one also handles `items` & `enum` attributes)

and get instantiated through the `Apipie::Generator::Swagger::ParamDescription::Builder` api.
@PanosCodes PanosCodes force-pushed the swagger-param-description-builder branch from f2bdaf1 to 233ff9b Compare January 15, 2023 11:16
@PanosCodes PanosCodes marked this pull request as ready for review January 15, 2023 11:24
@PanosCodes PanosCodes changed the title Move swagger param description under generator/swagger Move swagger param description creation under generator/swagger/param_description Jan 15, 2023
@mathieujobin mathieujobin self-requested a review January 16, 2023 04:37
@@ -434,105 +430,14 @@ def add_missing_params(method, path)

missing.each do |name|
warn_path_parameter_not_described(name, path)
result[name.to_sym] = OpenStruct.new({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Always happy to see an OpenStruct being removed

Copy link
Collaborator

@mathieujobin mathieujobin left a comment

Choose a reason for hiding this comment

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

would be great to see an update to the README about the new Composite feature.

@PanosCodes
Copy link
Contributor Author

would be great to see an update to the README about the new Composite feature.

It's meant to be an internal implementation for swagger generator, maybe once the refactor is done we could have a README.md under generator/swagger giving some context 🤷‍♂️

@mathieujobin mathieujobin merged commit 3d5276b into Apipie:master Jan 17, 2023
@PanosCodes PanosCodes deleted the swagger-param-description-builder branch January 17, 2023 04:25
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