Skip to content

Commit

Permalink
Rewrite the PHP Laravel generator (OpenAPITools#20526)
Browse files Browse the repository at this point in the history
* remove legacy laravel generator

* initial setup of my vision for the laravel generator

* update the php laravel samples

* update php laravel docs

* moved api validation into controller and handle edge cases presented by sample generation

* updated samples

* added php-laravel to github workflow php8 and removed php7 workflow as it only contained old laravel

* preemptive work to support union types as soon as php serde supports them

* updated samples

* update templates in accordance to samples output

* fix pipelines and update samples

* correct serde version

* fixed phpunit execution and updated samples

* added named routes

* remove

* readd samples

---------

Co-authored-by: [email protected] <[email protected]>
Co-authored-by: William Cheng <[email protected]>
  • Loading branch information
3 people authored Feb 17, 2025
1 parent c586362 commit 65df3c2
Show file tree
Hide file tree
Showing 327 changed files with 7,855 additions and 9,890 deletions.
32 changes: 0 additions & 32 deletions .github/workflows/samples-php7.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions .github/workflows/samples-php8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ on:
paths:
- samples/server/petstore/php-symfony/SymfonyBundle-php/**
- samples/server/petstore/php-flight/**
- samples/server/petstore/php-laravel/**
pull_request:
paths:
- samples/server/petstore/php-symfony/SymfonyBundle-php/**
- samples/server/petstore/php-flight/**
- samples/server/petstore/php-laravel/**
jobs:
build:
name: Build PHP projects
Expand All @@ -25,6 +27,7 @@ jobs:
# servers
- samples/server/petstore/php-symfony/SymfonyBundle-php/
- samples/server/petstore/php-flight/
- samples/server/petstore/php-laravel/
steps:
- uses: actions/checkout@v4
- name: Setup PHP with tools
Expand Down
24 changes: 13 additions & 11 deletions docs/generators/php-laravel.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ title: Documentation for the php-laravel Generator
| generator type | SERVER | |
| generator language | PHP | |
| generator default templating engine | mustache | |
| helpTxt | Generates a PHP laravel server library. | |
| helpTxt | Generates a php-laravel server. | |

## CONFIG OPTIONS
These options may be applied as additional-properties (cli) or configOptions (plugins). Refer to [configuration docs](https://openapi-generator.tech/docs/configuration) for more details.
Expand All @@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|apiPackage|package for generated api classes| |null|
|artifactUrl|artifact URL in generated pom.xml| |null|
|artifactVersion|The version to use in the composer package version field. e.g. 1.2.3| |null|
|autowire|Should autowire be enabled.| |false|
|composerPackageName|The name to use in the composer package name field. e.g. `vendor/project` (must be lowercase and consist of words separated by `-`, `.` or `_`).| |null|
|developerOrganization|developer organization in generated pom.xml| |null|
|developerOrganizationUrl|developer organization URL in generated pom.xml| |null|
Expand Down Expand Up @@ -57,6 +58,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl

<ul class="column-ul">
<li>\DateTime</li>
<li>\Illuminate\Http\UploadedFile</li>
<li>\SplFileObject</li>
<li>array</li>
<li>bool</li>
Expand Down Expand Up @@ -200,7 +202,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
### Documentation Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
|Readme||ToolingExtension
|Readme||ToolingExtension
|Model|✓|ToolingExtension
|Api|✓|ToolingExtension

Expand All @@ -220,7 +222,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|MultiServer|✗|OAS3
|ParameterizedServer|✗|OAS3
|ParameterStyling|✗|OAS3
|Callbacks||OAS3
|Callbacks||OAS3
|LinkObjects|✗|OAS3

### Parameter Feature
Expand All @@ -239,7 +241,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| ---- | --------- | ---------- |
|Simple|✓|OAS2,OAS3
|Composite|✓|OAS2,OAS3
|Polymorphism||OAS2,OAS3
|Polymorphism||OAS2,OAS3
|Union|✗|OAS3
|allOf|✗|OAS2,OAS3
|anyOf|✗|OAS3
Expand All @@ -249,14 +251,14 @@ These options may be applied as additional-properties (cli) or configOptions (pl
### Security Feature
| Name | Supported | Defined By |
| ---- | --------- | ---------- |
|BasicAuth||OAS2,OAS3
|ApiKey||OAS2,OAS3
|BasicAuth||OAS2,OAS3
|ApiKey||OAS2,OAS3
|OpenIDConnect|✗|OAS3
|BearerToken||OAS3
|OAuth2_Implicit||OAS2,OAS3
|OAuth2_Password||OAS2,OAS3
|OAuth2_ClientCredentials||OAS2,OAS3
|OAuth2_AuthorizationCode||OAS2,OAS3
|BearerToken||OAS3
|OAuth2_Implicit||OAS2,OAS3
|OAuth2_Password||OAS2,OAS3
|OAuth2_ClientCredentials||OAS2,OAS3
|OAuth2_AuthorizationCode||OAS2,OAS3
|SignatureAuth|✗|OAS3
|AWSV4Signature|✗|ToolingExtension

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public String getTypeDeclaration(Schema p) {
} else if (StringUtils.isNotBlank(p.get$ref())) { // model
String type = super.getTypeDeclaration(p);
return (!languageSpecificPrimitives.contains(type))
? "\\" + modelPackage + "\\" + type : type;
? "\\" + modelPackage + "\\" + toModelName(type) : type;
}
return super.getTypeDeclaration(p);
}
Expand Down Expand Up @@ -401,6 +401,10 @@ public String getSchemaType(Schema p) {
openAPIType = "UNKNOWN_OPENAPI_TYPE";
}

if ((p.getAnyOf() != null && !p.getAnyOf().isEmpty()) || (p.getOneOf() != null && !p.getOneOf().isEmpty())) {
return openAPIType;
}

if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
if (languageSpecificPrimitives.contains(type)) {
Expand Down Expand Up @@ -914,4 +918,28 @@ public boolean isDataTypeString(String dataType) {
public GeneratorLanguage generatorLanguage() {
return GeneratorLanguage.PHP;
}

@Override
public String toOneOfName(List<String> names, Schema composedSchema) {
List<Schema> schemas = ModelUtils.getInterfaces(composedSchema);

List<String> types = new ArrayList<>();
for (Schema s : schemas) {
types.add(getTypeDeclaration(s));
}

return String.join("|", types);
}

@Override
public String toAllOfName(List<String> names, Schema composedSchema) {
List<Schema> schemas = ModelUtils.getInterfaces(composedSchema);

List<String> types = new ArrayList<>();
for (Schema s : schemas) {
types.add(getTypeDeclaration(s));
}

return String.join("&", types);
}
}
Loading

0 comments on commit 65df3c2

Please sign in to comment.