Skip to content

[core] cleanup: remove OpenAPI parameter from CodegenConfig methods #1953

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

Merged
merged 31 commits into from
Jan 28, 2019

Conversation

jmini
Copy link
Member

@jmini jmini commented Jan 21, 2019

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master, 3.4.x, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language. @OpenAPITools/generator-core-team

Description of the PR

In order to solve #1927 I will need to access the OpenAPI instance in DefaultCodegen#toDefaultValue(..) in order to be able to see if the schema contains a $ref and if this is the case, to see if the referenced schema has a default value or not.

The main change of this PR is to have:

  • public String toDefaultValue(Schema p, OpenAPI openAPI)
  • Instead of public String toDefaultValue(Schema p)

To fulfill this requirement the OpenAPI is added as parameter to some methods of DefaultCodegen.

Some of those methods also had Map<String, Schema> allDefinitions which corresponds to openAPI.getComponents().getSchemas(). This parameter was also removed from the methods, and the openAPI instance is used to access to all schemas.

Based on feedback the initial intention of this PR was changed.

Historically, to access the OpenAPI instance, or all schemas of the OpenAPI instance, parameters were used in different methods. Example:

  • CodegenConfig#fromOperation(String, String, Operation, Map<String, Schema>, OpenAPI)
  • CodegenConfig#fromModel(String, Schema, Map<String, Schema>)
  • DefaultCodegen#fromResponse(OpenAPI, String, ApiResponse)
  • DefaultCodegen#DefaultCodegen.fromRequestBody(RequestBody, Map<String, Schema>, Set<String>, String)
  • DefaultCodegen#createDiscriminator(String, Schema, Map<String, Schema>)
  • ...

Then @wing328 noticed that access to this information is requested in a lot of places (for unaliasing).
So he introduced a new approach: having two members globalOpenAPI and globalSchemas.

Having two ways for doing the same task is not consistent. It is harder to understand, to maintain and can lead to errors (for example all the unit tests were not using globalOpenAPI, meaning that they were not reproducing real cases, because when the generator is used for real, then the globalOpenAPI member is set).

This PR cleans the situation by removing the original approach completely. There is no openAPI or allDefinitions parameter anymore in the methods. Instead they use the globalOpenAPI member which is renamed openAPI.

In addition globalSchemas was only a convenient and null-pointer safe access to openAPI.getComponents().getSchemas(). It is also removed.

This change is mentioned in the migration guide "From 3.x to 4.0.0"

In my opinion the PR improves the readability of the code and reduces the potential coding mistakes.

@auto-labeler
Copy link

auto-labeler bot commented Jan 21, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@jmini
Copy link
Member Author

jmini commented Jan 21, 2019

Input @wing328

have you considered using

protected OpenAPI globalOpenAPI;
protected Map<String, Schema> globalSchemas;

?

This is a very good feedback.

After a short discussion on Gitter we agreed that:

  • We will rename this.globalOpenAPI to this.openAPI (and the setter accordingly)
  • We will use only this way to access to the openAPI instance, meaning that all methods fromModel(..), fromRequestBody(..) will no longer have openAPI has parameter.

This way we are consistent and have only one pattern in place.

@jmini jmini added the WIP Work in Progress label Jan 21, 2019
@jmini jmini changed the title [core] add OpenAPI as parameter and cleanup [wip][core] cleanup: remove OpenAPI parameter from CodegenConfig methods Jan 21, 2019
@jmini jmini removed the WIP Work in Progress label Jan 23, 2019
@jmini jmini changed the title [wip][core] cleanup: remove OpenAPI parameter from CodegenConfig methods [core] cleanup: remove OpenAPI parameter from CodegenConfig methods Jan 23, 2019
@jmini
Copy link
Member Author

jmini commented Jan 23, 2019

PR is updated with new commits.
Description of the PR was changed to reflect the new intention.
WIP Tag is removed.

@wing328 wing328 added this to the 4.0.0 milestone Jan 23, 2019
@wing328 wing328 merged commit 046db19 into OpenAPITools:master Jan 28, 2019
@jmini jmini deleted the add_openapi_parameter branch January 28, 2019 15:03
A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
…penAPITools#1953)

* Add OpenAPI parameter to DefaultCodegen#toDefaultValue(..)

* Remove CodegenConfig#fromOperation()

* Remove allDefinitions parameter from fromModel(..)

* Remove definitions parameter from CodegenConfig#fromOperation(..)

* remove schemas parameter from DefaultCodegen#fromRequestBody(..)

* remove schemas parameter from DefaultCodegen#fromCallback(..)

* Remove openAPI parameter from CodegenConfig#fromModel(..)

* Remove openAPI parameter from CodegenConfig#fromOperation(..)

* Remove openAPI parameter from DefaultCodegen#fromProperty(..)

* Remove openAPI parameter from DefaultCodegen#fromParameter(..)

* Remove OpenAPI parameter from several methods

* Use ModelUtils.getReferencedParameter(..)

* Remove unused variable

* Remove openAPI parameter from DefaultCodegen#fromResponse(..)

* Remove openAPI parameter from DefaultCodegen#addHeaders(..)

* Remove from addConsumesInfo(..)/addProducesInfo(..)

* Improve test: add property to prevent Pet from being a free-form object

* remove globalSchemas Map<String, Schema>

* remove deprecated method: postProcessOperations()

* Remove 'Map<String, Schema> allSchemas' from addProperties(..)

* Remove 'Map<String, Schema> allDefinitions' from createDiscriminator(..)

* Remove 'Map<String, Schema> allSchemas' from unaliasPropertySchema(..)

* Rename globalOpenAPI to openAPI

* Update documentation

* Run “Optimize import” in IntelliJ IDEA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants