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

Rewrite the PHP Laravel generator #20526

Merged
merged 16 commits into from
Feb 17, 2025

Conversation

gijs-blanken
Copy link
Contributor

@gijs-blanken gijs-blanken commented Jan 22, 2025

This PR is currently a WIP, in order to get feedback on it. I can already see some issues in the sample generated code 😅 .

This PR completely rewrites the current Laravel generator to move away from the current stub paradigm to an interface paradigm. I've drawn inspiration from the PHP Symfony generator and the Rust Server generator.

As I've removed all the files belonging to the old generator, the diff is quite big and intertwined. It's easiest to look at the diff for this specific commit.

I incorporate the following components

  • Generating a routes.php file in the Laravel format. The user is responsible for registering this file in their Laravel application.
  • Request validation using Laravel's request validation classes.
  • Generating the controllers which pass the validated data to the API interface and generate a response based on the returned value. The controllers use Laravel methods to extract and deserialise the primitive request parameters. The complex parameters are deserialised by Serde. The controllers rely on the Laravel service container to resolve the API interface to a class instance. The user of this generator is responsible for binding the class instance to the interface.
  • Generating the API interfaces which contain a method typed to return a union type of all possible return types.

Current shortcomings

  • Validating complex request parameters is currently not supported. I rely on Serde to return null when deserialising fails. If anyone has ideas on how to incorporate this, I'm open to it!
  • I want a model for each response to ensure as much type safety as possible on the API interface. I'm currently looping through all ApiResponse objects and adding a dummy property to empty responses to ensure we generate a corresponding model for the response. I'd like a more elegant solution for this.
  • I'm currently not able to distinguish between different array responses for the same API interface as PHP does not support typing arrays. I'm open to suggestions on how to tackle this.

Beside these specific shortcoming I'm interested in hearing general suggestions on how to better structure my code or approach certain issues. However, this is my first time contributing to any open source project; go easy on me! 😅

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 || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    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*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)

I wouldn't say these are breaking changes as the current generator generates a server stub which you would not generate twice. The changes are however a big change in the usage of the generator so this might still belong in the next major version.

@wing328
Copy link
Member

wing328 commented Jan 23, 2025

I'm currently not able to distinguish between different array responses for the same API interface as PHP does not support typing arrays. I'm open to suggestions on how to tackle this.

we can leave that as a day 2 requirement instead (if there's a demand to better handle it)

@wing328
Copy link
Member

wing328 commented Jan 23, 2025

suggestion: add the sample folder to https://github.com/OpenAPITools/openapi-generator/blob/master/.github/workflows/samples-php8.yaml so that it will be tested by CI moving forward.

@wing328
Copy link
Member

wing328 commented Feb 5, 2025

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@gijs-blanken gijs-blanken marked this pull request as ready for review February 10, 2025 14:23
@gijs-blanken
Copy link
Contributor Author

I think it's ready for a proper review! There's still a few limitations that I would like to tackle but that can wait till it's been merged.

@wing328
Copy link
Member

wing328 commented Feb 10, 2025

thanks. triggered the workflow but got some errors.

can you please review those when you've time?

@gijs-blanken
Copy link
Contributor Author

I'm not sure on how to fix the failed test on CircleCI, would you mind taking a quick look? @wing328

@wing328
Copy link
Member

wing328 commented Feb 11, 2025

for the circleci issue, please ignore it for the time being

@wing328
Copy link
Member

wing328 commented Feb 11, 2025

https://github.com/OpenAPITools/openapi-generator/actions/runs/13258611613/job/37018725916?pr=20526

looks like a new file has not been committed. please have a look when you've time

@gijs-blanken
Copy link
Contributor Author

Sorry about the pipelines, hopefully it's all good now.

@gijs-blanken
Copy link
Contributor Author

Do you have time to look at it today? @wing328
I'd like to start integrating this generator into my pipelines soon 😄

@wing328 wing328 added this to the 7.12.0 milestone Feb 17, 2025
@wing328
Copy link
Member

wing328 commented Feb 17, 2025

@gijs-blanken
Copy link
Contributor Author

gijs-blanken commented Feb 17, 2025

I'm not sure how the 'Samples up-to-date' keeps failing. I'm very careful to run the 3 commands listed in the pull request template before each push.

@wing328
Copy link
Member

wing328 commented Feb 17, 2025

ok. i will take a look later today

@wing328
Copy link
Member

wing328 commented Feb 17, 2025

all tests passed. let's give it a try

thanks for rewriting the generator

@wing328 wing328 merged commit 65df3c2 into OpenAPITools:master Feb 17, 2025
28 checks passed
@gijs-blanken
Copy link
Contributor Author

Happy to help!

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