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

[Server][Spring] fix support interface and implementation classes for API controllers #16945

Merged
merged 10 commits into from Nov 4, 2023

Conversation

kapilguptavz
Copy link
Contributor

@kapilguptavz kapilguptavz commented Oct 31, 2023

added enhancement for java spring asked in #426 (comment)

Reproduction Steps for java spring:

  1. Create sample.yaml with endpoint containing only get method and generate server stub with below command:
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i sample.yaml -g spring -o springserver
 sample.yaml(paths details with get)
	 paths:
	  /pets:
		get:
		  summary: List all pets
		  operationId: listPets
		  tags:
			- pets
		  parameters:
			- name: limit.....
  1. Update sample.yaml with post method in exiting endpoint and generate server stub with same command given in step 1.
    updated sample.yaml (paths details with post method added)
		  paths:
			/pets:
			get:
			  summary: List all pets
			  operationId: listPets
			  tags:
				- pets
			  parameters:
				- name: limit
			post:
			  summary: Create a pet
			  operationId: createPets
			  tags:
				- pets
			  responses:
				'201':....

Result: In step 1, it will create PetsApi.java(interface ), PetsApiController.java(implementation class) as new and in 2nd step
it overwrites both file.

Enhancement with result:
added checks for file creation for java spring server for controller class.
Unit Test Case: Tested for multiple endpoints
sample.yaml

    paths:
	  /pets:
		get:
		  operationId: listPets
		  tags:
			- pets
		  parameters:
			- name: limit
			  in: query
			  description: How many items to return at one time (max 100)
			  required: false
			  schema:
				type: integer
				maximum: 100
				format: int32
		  responses:
			'200':
			 ....
		post:
			summary: Create a pet
			operationId: createPets
			tags:
			  - pets
			responses:
			  '201':
				description: Null response
			  default:
				description: unexpected error
				content:
				  application/json:
					schema:
					  $ref: "#/components/schemas/Error"
	  /pots/{potId}:
		get:
		  summary: Info for a specific pet
		  operationId: showPetById
		  tags:
			- pets
		  parameters:
			- name: potId
			  in: path
			  required: true
			  description: The id of the pet to retrieve
			  schema:
				type: string
		  responses:
			'200':`

Result: generated server stubs multiple times. It only overwrites interfaces (pets.api and pots.api). Implementation classes(PetsApiController.java and PotsApiController.java) are created once.
@cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @Zomzog @martin-mfg @wing328

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Copy link

@saibalroyvz saibalroyvz left a comment

Choose a reason for hiding this comment

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

Code looks ok

@kapilguptavz
Copy link
Contributor Author

kapilguptavz commented Oct 31, 2023

@martin-mfg, As all test cases are passed and I also have added changes suggested by you in forked repo(kapilguptavz#1 (review)), could you please merger the PR in master branch.

@kapilguptavz kapilguptavz changed the title [Server][Spring] support interface and implementation classes for API controllers [Server][Spring] fix support interface and implementation classes for API controllers Nov 1, 2023
@martin-mfg
Copy link
Contributor

I don't have the rights to merge PRs. Please wait for the project maintainer wing328 to do this.

@kapilguptavz
Copy link
Contributor Author

Thanks! @martin-mfg. @wing328, We are trying our first contribution on behalf of Verizon. Kindly review the PR.

@wing328
Copy link
Member

wing328 commented Nov 3, 2023

Result: In step 1, it will create PetsApi.java(interface ), PetsApiController.java(implementation class) as new and in 2nd step
it overwrites both file.

Agreed with you that it shouldn't overwrite both files. Only interface files (e.g. PetsApi.java) should be overwritten.

@wing328
Copy link
Member

wing328 commented Nov 3, 2023

tested locally and confirmed implementation file is not overwritten (only interface file is overwritten)

@wing328
Copy link
Member

wing328 commented Nov 3, 2023

@kapilguptavz btw, are you aware of the following option?

	interfaceOnly
	    Whether to generate only API interface stubs without the server files. (Default: false)

which skip the generation of the implementation files.

Looks like it's trying to address the issue you encountered.

@kapilguptavz
Copy link
Contributor Author

@kapilguptavz btw, are you aware of the following option?

	interfaceOnly
	    Whether to generate only API interface stubs without the server files. (Default: false)

which skip the generation of the implementation files.

Looks like it's trying to address the issue you encountered.

Not aware of this option as I followed the problem statement. If anything needs to be done from our end?, if not you can merger the PR.

@wing328
Copy link
Member

wing328 commented Nov 3, 2023

I'll merge it over the weekend if no question/feedback from anyone else.

Have a nice weekend.

@kapilguptavz
Copy link
Contributor Author

I'll merge it over the weekend if no question/feedback from anyone else.

Have a nice weekend.

Thanks! same to you!

@kapilguptavz
Copy link
Contributor Author

kapilguptavz commented Nov 3, 2023

And in addition to above, same logic can be added for other java servers(like java play framework and all), we can make generatorCheck and templateCheck as configurable input.

// checking if apiController file is already existed for spring generator
private boolean apiFilePreCheck(String filename, String generator, String templateName, String apiControllerTemplate){
File apiFile = new File(filename);
if(apiFile.exists() && config.getName().equals(generator) && templateName.equals(apiControllerTemplate)){
Copy link
Contributor

Choose a reason for hiding this comment

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

you can directly return the boolean value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MelleD , have removed else and directly returned Boolean.

Copy link
Contributor

Choose a reason for hiding this comment

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

Better :-)
But you can also remove completely the whole if, right?

return apiFile.exists() || config.getName().equals(generator) ||templateName.equals(apiControllerTemplate)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

below logic I am applying here:
if file is already existed && generator is spring && template is controller, then only skip creating the file , other wise we have to create files if any of the conditions holds false.
but in above suggestion it will create file if any of the condition holds true.
If I remove if I will have to add as below:
return apiFile.exists() && config.getName().equals(generator) && templateName.equals(apiControllerTemplate)

additionally "!" I have to add where I m using this function.
Please let me know if it is needed ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes or set brackets !(condition).
Up to you but to make a if and directly return a Boolean looks redundant :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed the suggested changes 👍

@MelleD
Copy link
Contributor

MelleD commented Nov 3, 2023

@wing328 so far looks good for me

@wing328 wing328 merged commit c4b18f2 into OpenAPITools:master Nov 4, 2023
57 checks passed
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.

None yet

5 participants