Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !12480

php-laravel, enum models, nullable and default values

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged William Cheng requested to merge virta-ltd-feature/php-laravel-enums-and-nullable into master 3 years ago
  • Overview 0
  • Commits 5
  • Pipelines 0
  • Changes 62
  • support for enum models & separating mustache templates for enums and generics
  • extract property 'type' and 'default value' from a #ref to an enum model
  • support for PascalCase naming convention for PHP generators
  • use a default value for known basic types when they are not nullable
  • use 'null' as default value for nullable types when they do not specify the default value
  • use defined constant path as enum default value when found

credits: @aghajani

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/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 (6.0.1) (patch release), 6.1.x (breaking changes with fallbacks), 7.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.

cc @jebentier (2017/07), @dkarlovi (2017/07), @mandrean (2017/08), @jfastnacht (2017/09), @ybelenko (2018/07), @renepardon (2018/12)

Compare
  • master (base)

and
  • latest version
    9d59caf2
    5 commits, 2 years ago

62 files
+ 435
- 173

    Preferences

    File browser
    Compare changes
modules/openapi-g‎enerator/src/main‎
java/org/openapitoo‎ls/codegen/languages‎
AbstractPhp‎Codegen.java‎ +2 -0
PhpLaravelServ‎erCodegen.java‎ +96 -0
resources/‎php-laravel‎
model.m‎ustache‎ +9 -13
model_enu‎m.mustache‎ +28 -0
model_gener‎ic.mustache‎ +11 -0
sam‎ples‎
open‎api3‎
client/petstore‎/spring-cloud-3‎
.openapi-‎generator‎
VER‎SION‎ +1 -1
src/main/java/org‎/openapitools/api‎
PetAp‎i.java‎ +1 -1
StoreA‎pi.java‎ +1 -1
UserAp‎i.java‎ +1 -1
server/petstor‎e/springboot-3‎
.openapi-‎generator‎
VER‎SION‎ +1 -1
src/main/java/org‎/openapitools/api‎
PetAp‎i.java‎ +1 -1
StoreA‎pi.java‎ +1 -1
UserAp‎i.java‎ +1 -1
server/petstore‎/…/…/app/Models‎
AdditionalProp‎ertiesClass.php‎ +2 -2
AllOfWithSi‎ngleRef.php‎ +3 -3
Anima‎l.php‎ +2 -2
ApiResp‎onse.php‎ +3 -3
ArrayOfArrayOf‎NumberOnly.php‎ +1 -1
ArrayOfNum‎berOnly.php‎ +1 -1
ArrayT‎est.php‎ +3 -3
Capitaliz‎ation.php‎ +6 -6
Cat‎.php‎ +3 -3
CatAll‎Of.php‎ +1 -1
Catego‎ry.php‎ +2 -2
ClassMo‎del.php‎ +2 -1
Clien‎t.php‎ +1 -1
Deprecated‎Object.php‎ +1 -1
Dog‎.php‎ +3 -3
DogAll‎Of.php‎ +1 -1
EnumArr‎ays.php‎ +2 -2
EnumCl‎ass.php‎ +22 -1
EnumTe‎st.php‎ +12 -12
File‎.php‎ +2 -1
FileSchemaT‎estClass.php‎ +2 -2
Foo‎.php‎ +1 -1
FooGetDefaul‎tResponse.php‎ +1 -1
FormatT‎est.php‎ +16 -16
HasOnlyRe‎adOnly.php‎ +2 -2
HealthChec‎kResult.php‎ +3 -2
InlineRespon‎seDefault.php‎ +1 -1
MapTe‎st.php‎ +4 -4
MixedPropertiesAndAddit‎ionalPropertiesClass.php‎ +3 -3
Model200Re‎sponse.php‎ +3 -2
ModelL‎ist.php‎ +1 -1
ModelRe‎turn.php‎ +2 -1
Name‎.php‎ +5 -4
Nullable‎Class.php‎ +22 -22
NumberO‎nly.php‎ +1 -1
ObjectWithDepre‎catedFields.php‎ +4 -4
Orde‎r.php‎ +6 -6
OuterComp‎osite.php‎ +3 -3
OuterE‎num.php‎ +22 -1
OuterEnumDef‎aultValue.php‎ +22 -1
OuterEnumI‎nteger.php‎ +22 -1
OuterEnumInteger‎DefaultValue.php‎ +22 -1
OuterObjectWith‎EnumProperty.php‎ +2 -2
Pet‎.php‎ +6 -6
ReadOnly‎First.php‎ +2 -2
SingleRe‎fType.php‎ +19 -1
SpecialMod‎elName.php‎ +1 -1
Tag‎.php‎ +2 -2
User‎.php‎ +8 -8
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java
+ 2
- 0
  • View file @ 9d59caf2

  • Edit in single-file editor

  • Open in Web IDE


@@ -408,6 +408,8 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
// return the name in camelCase style
// phone_number => phoneNumber
name = camelize(name, true);
} else if ("PascalCase".equals(variableNamingConvention)) {
name = camelize(name, false);
} else { // default to snake case
// return the name in underscore style
// PhoneNumber => phone_number
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpLaravelServerCodegen.java
+ 96
- 0
  • View file @ 9d59caf2

  • Edit in single-file editor

  • Open in Web IDE


@@ -18,12 +18,16 @@
package org.openapitools.codegen.languages;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.utils.ModelUtils;
import io.swagger.v3.oas.models.media.Schema;
import java.io.File;
import java.util.*;
@@ -296,4 +300,96 @@ public class PhpLaravelServerCodegen extends AbstractPhpCodegen {
return camelize(name, false) + "Controller";
}
@Override
protected String getEnumDefaultValue(String defaultValue, String dataType) {
return defaultValue;
}
@Override
public CodegenProperty fromProperty(String name, Schema p) {
CodegenProperty property = super.fromProperty(name, p);
Schema referencedSchema = ModelUtils.getReferencedSchema(this.openAPI, p);
//Referenced enum case:
if (!property.isEnum && referencedSchema.getEnum() != null && !referencedSchema.getEnum().isEmpty()) {
property.dataType = this.getSchemaType(referencedSchema);
property.defaultValue = this.toDefaultValue(referencedSchema);
List<Object> _enum = referencedSchema.getEnum();
Map<String, Object> allowableValues = new HashMap<>();
allowableValues.put("values", _enum);
if (allowableValues.size() > 0) {
property.allowableValues = allowableValues;
}
}
return property;
}
@Override
public String toDefaultValue(Schema p) {
if (ModelUtils.isBooleanSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "false";
}
} else if (ModelUtils.isDateSchema(p)) {
// TODO
} else if (ModelUtils.isDateTimeSchema(p)) {
// TODO
} else if (ModelUtils.isFileSchema(p)) {
// TODO
} else if (ModelUtils.isNumberSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "0";
}
} else if (ModelUtils.isIntegerSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "0";
}
} else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) {
return "'" + p.getDefault() + "'";
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "\"\"";
}
} else if (ModelUtils.isArraySchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "[]";
}
}
return null;
}
@Override
public String toEnumDefaultValue(String value, String datatype) {
return datatype + "::" + value;
}
@Override
public String toEnumVarName(String value, String datatype) {
if (value.length() == 0) {
return super.toEnumVarName(value, datatype);
}
// number
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
String varName = "NUMBER_" + value;
varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
varName = varName.replaceAll("\\.", "_DOT_");
return varName;
}
return super.toEnumVarName(value, datatype);
}
}
modules/openapi-generator/src/main/resources/php-laravel/model.mustache
+ 9
- 13
  • View file @ 9d59caf2

  • Edit in single-file editor

  • Open in Web IDE

<?php
{{#models}}{{#model}}/**
{{#models}}
{{#model}}
/**
* {{classname}}
*/
namespace {{modelPackage}};
/**
* {{classname}}
{{#description}}
* @description {{{.}}}
{{/description}}
*/
class {{classname}} {
{{#vars}}
/** @var {{{dataType}}} ${{name}} {{description}}*/
{{#deprecated}}
/** @deprecated */
{{/deprecated}}
private ${{name}};
{{/vars}}
}
{{/model}}{{/models}}
\ No newline at end of file
{{#isEnum}}{{>model_enum}}{{/isEnum}}{{^isEnum}}{{>model_generic}}{{/isEnum}}
{{/model}}
{{/models}}
\ No newline at end of file
modules/openapi-generator/src/main/resources/php-laravel/model_enum.mustache 0 → 100644
+ 28
- 0
  • View file @ 9d59caf2

  • Edit in single-file editor

  • Open in Web IDE

class {{classname}}
{
/**
* Possible values of this enum
*/
{{#allowableValues}}
{{#enumVars}}
const {{{name}}} = {{{value}}};
{{/enumVars}}
{{/allowableValues}}
/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
{{#allowableValues}}
{{#enumVars}}
self::{{{name}}}{{^-last}},
{{/-last}}
{{/enumVars}}
{{/allowableValues}}
];
}
}
\ No newline at end of file
modules/openapi-generator/src/main/resources/php-laravel/model_generic.mustache 0 → 100644
+ 11
- 0
  • View file @ 9d59caf2

  • Edit in single-file editor

  • Open in Web IDE

class {{classname}} {
{{#vars}}
/** @var {{{dataType}}}{{#isNullable}}|null{{/isNullable}} ${{name}} {{description}}*/
{{#deprecated}}
/** @deprecated */
{{/deprecated}}
public ${{name}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}{{#isNullable}} = null{{/isNullable}}{{/defaultValue}};
{{/vars}}
}
\ No newline at end of file
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
5.0.0
5.0.0 (expired)
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference: OpenAPITools/openapi-generator!6812
Source branch: virta-ltd-feature/php-laravel-enums-and-nullable

Menu

Explore Projects Groups Snippets