Skip to content

Commit

Permalink
Fixes for php-ze-ph generator (#340)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Articus authored and wing328 committed May 7, 2018
1 parent e45b378 commit d30fcba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ OpenAPI Generator is a fork of [Swagger Codegen](https://github.com/swagger-api/

- [Akihito Nakano](https://github.com/ackintosh)
- [Artem Ocheredko](https://github.com/galaxie)
- [Arthur Mogliev](https://github.com/Articus)
- [Bartek Kryza](https://github.com/bkryza)
- [Ben Wells](https://github.com/bvwells)
- [Benjamin Gill](https://github.com/bjgill)
Expand Down
2 changes: 1 addition & 1 deletion bin/windows/ze-ph-petstore.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ If Not Exist %executable% (
)

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%
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,30 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
continue;
}

List<String> requiredProperties = new ArrayList<>();
for (Parameter parameter : operation.getParameters()) {
Schema schema = convertParameterToSchema(parameter);
if (schema != null) {
schemas.put(schema.getName(), schema);
if (Boolean.TRUE.equals(parameter.getRequired())) {
requiredProperties.add(schema.getName());
}
}
}

if (!schemas.isEmpty()) {
Schema model = new Schema();
ObjectSchema model = new ObjectSchema();
String operationId = getOrGenerateOperationId(operation, pathname, method.name());
model.setDescription("Query parameters for " + operationId);
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);
openAPI.getComponents().addSchemas(definitionName, model);
String definitionModel = "\\" + modelPackage + "\\" + toModelName(definitionName);
operation.addExtension(VEN_QUERY_DATA_TYPE, definitionModel);
operation.addExtension(VEN_HAS_QUERY_DATA, Boolean.TRUE);
addInternalExtensionToOperation(operation, VEN_QUERY_DATA_TYPE, definitionModel);
addInternalExtensionToOperation(operation, VEN_HAS_QUERY_DATA, Boolean.TRUE);
}
}
}
Expand All @@ -214,7 +220,7 @@ protected Schema convertParameterToSchema(Parameter parameter) {
if (collectionFormat == null) {
collectionFormat = "csv";
}
arraySchema.addExtension(VEN_COLLECTION_FORMAT, collectionFormat);
addInternalExtensionToSchema(arraySchema, VEN_COLLECTION_FORMAT, collectionFormat);
property = arraySchema;
} else { // non-array e.g. string, integer
switch (queryParameter.getSchema().getType()) {
Expand Down Expand Up @@ -252,17 +258,28 @@ protected Schema convertParameterToSchema(Parameter parameter) {
if (property != null) {
property.setName(queryParameter.getName());
property.setDescription(queryParameter.getDescription());
if (Boolean.TRUE.equals(queryParameter.getRequired())) {
List<String> required = new ArrayList<String>();
required.add(queryParameter.getName());
}

property.addExtension(VEN_FROM_QUERY, Boolean.TRUE);
addInternalExtensionToSchema(property, VEN_FROM_QUERY, Boolean.TRUE);
}
}
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) {
String result = name;
if (openAPI.getComponents().getSchemas() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:

- 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`)

After that you should be able to call all methods from your API spec. Most of the negative scenarios should be handled:
Expand Down
8 changes: 4 additions & 4 deletions samples/server/petstore/ze-ph/README.md
Original file line number Diff line number Diff line change
@@ -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
This server stub aims to provide light, yet comprehensive structure for your API project using:
Expand All @@ -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:

- 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`)

After that you should be able to call all methods from your API spec. Most of the negative scenarios should be handled:
Expand All @@ -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 :)

## 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).

0 comments on commit d30fcba

Please # to comment.