Fixes for php-ze-ph generator (#340)

* Fixes for ze-ph:
- fixed gathering of required properties for query data model
- fixed setting of internal vendor extensions for query data model detection
- fixed small typo in generated README

* Updated samples for ze-ph fixes
This commit is contained in:
Arthur Mogliev 2018-05-07 06:22:08 +04:00 committed by William Cheng
parent e45b3784f1
commit d30fcbabba
5 changed files with 35 additions and 17 deletions

View File

@ -500,6 +500,7 @@ OpenAPI Generator is a fork of [Swagger Codegen](https://github.com/swagger-api/
- [Akihito Nakano](https://github.com/ackintosh) - [Akihito Nakano](https://github.com/ackintosh)
- [Artem Ocheredko](https://github.com/galaxie) - [Artem Ocheredko](https://github.com/galaxie)
- [Arthur Mogliev](https://github.com/Articus)
- [Bartek Kryza](https://github.com/bkryza) - [Bartek Kryza](https://github.com/bkryza)
- [Ben Wells](https://github.com/bvwells) - [Ben Wells](https://github.com/bvwells)
- [Benjamin Gill](https://github.com/bjgill) - [Benjamin Gill](https://github.com/bjgill)

View File

@ -5,6 +5,6 @@ If Not Exist %executable% (
) )
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -l ze-ph -o samples\server\petstore\ze-ph set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -l php-ze-ph -o samples\server\petstore\ze-ph
java %JAVA_OPTS% -jar %executable% %ags% java %JAVA_OPTS% -jar %executable% %ags%

View File

@ -174,24 +174,30 @@ public class PhpZendExpressivePathHandlerServerCodegen extends AbstractPhpCodege
continue; continue;
} }
List<String> requiredProperties = new ArrayList<>();
for (Parameter parameter : operation.getParameters()) { for (Parameter parameter : operation.getParameters()) {
Schema schema = convertParameterToSchema(parameter); Schema schema = convertParameterToSchema(parameter);
if (schema != null) { if (schema != null) {
schemas.put(schema.getName(), schema); schemas.put(schema.getName(), schema);
if (Boolean.TRUE.equals(parameter.getRequired())) {
requiredProperties.add(schema.getName());
}
} }
} }
if (!schemas.isEmpty()) { if (!schemas.isEmpty()) {
Schema model = new Schema(); ObjectSchema model = new ObjectSchema();
String operationId = getOrGenerateOperationId(operation, pathname, method.name()); String operationId = getOrGenerateOperationId(operation, pathname, method.name());
model.setDescription("Query parameters for " + operationId); model.setDescription("Query parameters for " + operationId);
model.setProperties(schemas); model.setProperties(schemas);
model.addExtension(VEN_FROM_QUERY, Boolean.TRUE); model.setRequired(requiredProperties);
//Add internal extension directly, because addExtension filters extension names
addInternalExtensionToSchema(model, VEN_FROM_QUERY, Boolean.TRUE);
String definitionName = generateUniqueDefinitionName(operationId + "QueryData", openAPI); String definitionName = generateUniqueDefinitionName(operationId + "QueryData", openAPI);
openAPI.getComponents().addSchemas(definitionName, model); openAPI.getComponents().addSchemas(definitionName, model);
String definitionModel = "\\" + modelPackage + "\\" + toModelName(definitionName); String definitionModel = "\\" + modelPackage + "\\" + toModelName(definitionName);
operation.addExtension(VEN_QUERY_DATA_TYPE, definitionModel); addInternalExtensionToOperation(operation, VEN_QUERY_DATA_TYPE, definitionModel);
operation.addExtension(VEN_HAS_QUERY_DATA, Boolean.TRUE); addInternalExtensionToOperation(operation, VEN_HAS_QUERY_DATA, Boolean.TRUE);
} }
} }
} }
@ -214,7 +220,7 @@ public class PhpZendExpressivePathHandlerServerCodegen extends AbstractPhpCodege
if (collectionFormat == null) { if (collectionFormat == null) {
collectionFormat = "csv"; collectionFormat = "csv";
} }
arraySchema.addExtension(VEN_COLLECTION_FORMAT, collectionFormat); addInternalExtensionToSchema(arraySchema, VEN_COLLECTION_FORMAT, collectionFormat);
property = arraySchema; property = arraySchema;
} else { // non-array e.g. string, integer } else { // non-array e.g. string, integer
switch (queryParameter.getSchema().getType()) { switch (queryParameter.getSchema().getType()) {
@ -252,17 +258,28 @@ public class PhpZendExpressivePathHandlerServerCodegen extends AbstractPhpCodege
if (property != null) { if (property != null) {
property.setName(queryParameter.getName()); property.setName(queryParameter.getName());
property.setDescription(queryParameter.getDescription()); property.setDescription(queryParameter.getDescription());
if (Boolean.TRUE.equals(queryParameter.getRequired())) { addInternalExtensionToSchema(property, VEN_FROM_QUERY, Boolean.TRUE);
List<String> required = new ArrayList<String>();
required.add(queryParameter.getName());
}
property.addExtension(VEN_FROM_QUERY, Boolean.TRUE);
} }
} }
return property; return property;
} }
protected void addInternalExtensionToSchema(Schema schema, String name, Object value) {
//Add internal extension directly, because addExtension filters extension names
if (schema.getExtensions() == null) {
schema.setExtensions(new HashMap<>());
}
schema.getExtensions().put(name, value);
}
protected void addInternalExtensionToOperation(Operation operation, String name, Object value) {
//Add internal extension directly, because addExtension filters extension names
if (operation.getExtensions() == null) {
operation.setExtensions(new HashMap<>());
}
operation.getExtensions().put(name, value);
}
protected String generateUniqueDefinitionName(String name, OpenAPI openAPI) { protected String generateUniqueDefinitionName(String name, OpenAPI openAPI) {
String result = name; String result = name;
if (openAPI.getComponents().getSchemas() != null) { if (openAPI.getComponents().getSchemas() != null) {

View File

@ -13,7 +13,7 @@ This server stub aims to provide light, yet comprehensive structure for your API
All you have to do to start development is: All you have to do to start development is:
- install dependencies via [Composer](https://getcomposer.org/) - install dependencies via [Composer](https://getcomposer.org/)
- create cache folder: `mkdir -p ./data/cache/ZendCache` (you will need it later for configuration and metadata caches - check comments in `./application/conig.yml`) - create cache folder: `mkdir -p ./data/cache/ZendCache` (you will need it later for configuration and metadata caches - check comments in `./application/config.yml`)
- start PHP development server: `php -S 0.0.0.0:8080 -t ./public` (or any other SAPI you prefer, just make sure that you configure webroot to `./public` and rewrites to `./public/index.php`) - start PHP development server: `php -S 0.0.0.0:8080 -t ./public` (or any other SAPI you prefer, just make sure that you configure webroot to `./public` and rewrites to `./public/index.php`)
After that you should be able to call all methods from your API spec. Most of the negative scenarios should be handled: After that you should be able to call all methods from your API spec. Most of the negative scenarios should be handled:

View File

@ -1,6 +1,6 @@
# Swagger generated server # OpenAPI generated server
Generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. Generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
## Overview ## Overview
This server stub aims to provide light, yet comprehensive structure for your API project using: This server stub aims to provide light, yet comprehensive structure for your API project using:
@ -13,7 +13,7 @@ This server stub aims to provide light, yet comprehensive structure for your API
All you have to do to start development is: All you have to do to start development is:
- install dependencies via [Composer](https://getcomposer.org/) - install dependencies via [Composer](https://getcomposer.org/)
- create cache folder: `mkdir -p ./data/cache/ZendCache` (you will need it later for configuration and metadata caches - check comments in `./application/conig.yml`) - create cache folder: `mkdir -p ./data/cache/ZendCache` (you will need it later for configuration and metadata caches - check comments in `./application/config.yml`)
- start PHP development server: `php -S 0.0.0.0:8080 -t ./public` (or any other SAPI you prefer, just make sure that you configure webroot to `./public` and rewrites to `./public/index.php`) - start PHP development server: `php -S 0.0.0.0:8080 -t ./public` (or any other SAPI you prefer, just make sure that you configure webroot to `./public` and rewrites to `./public/index.php`)
After that you should be able to call all methods from your API spec. Most of the negative scenarios should be handled: After that you should be able to call all methods from your API spec. Most of the negative scenarios should be handled:
@ -31,4 +31,4 @@ But for obvious reason you will not get any `200 OK`, only `500 Not implemented`
- implement your handlers - the most tricky part :) - implement your handlers - the most tricky part :)
## Enjoy! ## Enjoy!
Hopefully this stub will reduce the amount of boilerplate code you have to write manually. If you have any suggestions or questions about `ze-ph` generator, feel free to create issue either in [Path Handler repository](https://github.com/Articus/PathHandler/issues) or in [Swagger Codegen repository](https://github.com/swagger-api/swagger-codegen/issues). Hopefully this stub will reduce the amount of boilerplate code you have to write manually. If you have any suggestions or questions about `ze-ph` generator, feel free to create issue either in [Path Handler repository](https://github.com/Articus/PathHandler/issues) or in [OpenAPI Generator repository](https://openapi-generator.tech/issues).