better operation id handling in scala generators (#2490)

This commit is contained in:
William Cheng 2019-03-25 13:13:09 +08:00 committed by GitHub
parent 5074f4d9c5
commit 3e065db2c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 32 deletions

View File

@ -333,4 +333,29 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen {
} }
} }
@Override
public String toOperationId(String operationId) {
// throw exception if method name is empty
if (StringUtils.isEmpty(operationId)) {
throw new RuntimeException("Empty method/operation name (operationId) not allowed");
}
operationId = camelize(sanitizeName(operationId), true);
// method name cannot use reserved keyword, e.g. return
if (isReservedWord(operationId)) {
String newOperationId = camelize("call_" + operationId, true);
LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + newOperationId);
return newOperationId;
}
// operationId starts with a number
if (operationId.matches("^\\d.*")) {
LOGGER.warn(operationId + " (starting with a number) cannot be used as method sname. Renamed to " + camelize("call_" + operationId), true);
operationId = camelize("call_" + operationId, true);
}
return operationId;
}
} }

View File

@ -235,16 +235,6 @@ public class ScalaAkkaClientCodegen extends AbstractScalaCodegen implements Code
return codegenSecurities; return codegenSecurities;
} }
@Override
public String toOperationId(String operationId) {
// throw exception if method name is empty
if (StringUtils.isEmpty(operationId)) {
throw new RuntimeException("Empty method name (operationId) not allowed");
}
return super.toOperationId(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, operationId));
}
@Override @Override
public String toParamName(String name) { public String toParamName(String name) {
return formatIdentifier(name, false); return formatIdentifier(name, false);

View File

@ -33,11 +33,15 @@ class {{classname}}(baseUrl: String) {
{{/dataType}}{{^dataType}}.with{{>responseState}}Response[Unit]({{code}}) {{/dataType}}{{^dataType}}.with{{>responseState}}Response[Unit]({{code}})
{{/dataType}}{{/isWildcard}}{{/responses}}{{#responses}}{{#isWildcard}}{{#dataType}}.withDefault{{>responseState}}Response[{{dataType}}] {{/dataType}}{{/isWildcard}}{{/responses}}{{#responses}}{{#isWildcard}}{{#dataType}}.withDefault{{>responseState}}Response[{{dataType}}]
{{/dataType}}{{^dataType}}.withDefault{{>responseState}}Response[Unit] {{/dataType}}{{^dataType}}.withDefault{{>responseState}}Response[Unit]
{{/dataType}}{{/isWildcard}}{{/responses}}{{^responseHeaders.isEmpty}} {{/dataType}}{{/isWildcard}}{{/responses}}
object {{#fnCapitalize}}{{operationId}}{{/fnCapitalize}}Headers { {{#responseHeaders}} {{^responseHeaders.isEmpty}}
def {{{name}}}(r: ApiReturnWithHeaders) = r.get{{^isContainer}}{{baseType}}{{/isContainer}}{{#isContainer}}String{{/isContainer}}Header("{{baseName}}"){{/responseHeaders}} object {{#fnCapitalize}}{{operationId}}{{/fnCapitalize}}Headers {
{{#responseHeaders}}
def {{{name}}}(r: ApiReturnWithHeaders) = r.get{{^isContainer}}{{baseType}}{{/isContainer}}{{#isContainer}}String{{/isContainer}}Header("{{baseName}}")
{{/responseHeaders}}
} }
{{/responseHeaders.isEmpty}} {{/responseHeaders.isEmpty}}
{{/operation}} {{/operation}}
{{#unknownStatusCodes}} {{#unknownStatusCodes}}

View File

@ -35,6 +35,8 @@ class PetApi(baseUrl: String) {
ApiRequest[Unit](ApiMethods.POST, "http://petstore.swagger.io/v2", "/pet", "application/json") ApiRequest[Unit](ApiMethods.POST, "http://petstore.swagger.io/v2", "/pet", "application/json")
.withBody(body) .withBody(body)
.withErrorResponse[Unit](405) .withErrorResponse[Unit](405)
/** /**
* Expected answers: * Expected answers:
* code 400 : (Invalid pet value) * code 400 : (Invalid pet value)
@ -47,6 +49,8 @@ class PetApi(baseUrl: String) {
.withPathParam("petId", petId) .withPathParam("petId", petId)
.withHeaderParam("api_key", apiKey) .withHeaderParam("api_key", apiKey)
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
/** /**
* Multiple status values can be provided with comma separated strings * Multiple status values can be provided with comma separated strings
* *
@ -61,6 +65,8 @@ class PetApi(baseUrl: String) {
.withQueryParam("status", ArrayValues(status, CSV)) .withQueryParam("status", ArrayValues(status, CSV))
.withSuccessResponse[Seq[Pet]](200) .withSuccessResponse[Seq[Pet]](200)
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
/** /**
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* *
@ -75,6 +81,8 @@ class PetApi(baseUrl: String) {
.withQueryParam("tags", ArrayValues(tags, CSV)) .withQueryParam("tags", ArrayValues(tags, CSV))
.withSuccessResponse[Seq[Pet]](200) .withSuccessResponse[Seq[Pet]](200)
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
/** /**
* Returns a single pet * Returns a single pet
* *
@ -95,6 +103,8 @@ class PetApi(baseUrl: String) {
.withSuccessResponse[Pet](200) .withSuccessResponse[Pet](200)
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
.withErrorResponse[Unit](404) .withErrorResponse[Unit](404)
/** /**
* Expected answers: * Expected answers:
* code 400 : (Invalid ID supplied) * code 400 : (Invalid ID supplied)
@ -109,6 +119,8 @@ class PetApi(baseUrl: String) {
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
.withErrorResponse[Unit](404) .withErrorResponse[Unit](404)
.withErrorResponse[Unit](405) .withErrorResponse[Unit](405)
/** /**
* Expected answers: * Expected answers:
* code 405 : (Invalid input) * code 405 : (Invalid input)
@ -123,6 +135,8 @@ class PetApi(baseUrl: String) {
.withFormParam("status", status) .withFormParam("status", status)
.withPathParam("petId", petId) .withPathParam("petId", petId)
.withErrorResponse[Unit](405) .withErrorResponse[Unit](405)
/** /**
* Expected answers: * Expected answers:
* code 200 : ApiResponse (successful operation) * code 200 : ApiResponse (successful operation)
@ -139,5 +153,7 @@ class PetApi(baseUrl: String) {
.withSuccessResponse[ApiResponse](200) .withSuccessResponse[ApiResponse](200)
} }

View File

@ -37,6 +37,8 @@ class StoreApi(baseUrl: String) {
.withPathParam("orderId", orderId) .withPathParam("orderId", orderId)
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
.withErrorResponse[Unit](404) .withErrorResponse[Unit](404)
/** /**
* Returns a map of status codes to quantities * Returns a map of status codes to quantities
* *
@ -50,6 +52,8 @@ class StoreApi(baseUrl: String) {
ApiRequest[Map[String, Int]](ApiMethods.GET, "http://petstore.swagger.io/v2", "/store/inventory", "application/json") ApiRequest[Map[String, Int]](ApiMethods.GET, "http://petstore.swagger.io/v2", "/store/inventory", "application/json")
.withApiKey(apiKey, "api_key", HEADER) .withApiKey(apiKey, "api_key", HEADER)
.withSuccessResponse[Map[String, Int]](200) .withSuccessResponse[Map[String, Int]](200)
/** /**
* For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
* *
@ -66,6 +70,8 @@ class StoreApi(baseUrl: String) {
.withSuccessResponse[Order](200) .withSuccessResponse[Order](200)
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
.withErrorResponse[Unit](404) .withErrorResponse[Unit](404)
/** /**
* Expected answers: * Expected answers:
* code 200 : Order (successful operation) * code 200 : Order (successful operation)
@ -80,5 +86,7 @@ class StoreApi(baseUrl: String) {
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
} }

View File

@ -35,6 +35,8 @@ class UserApi(baseUrl: String) {
ApiRequest[Unit](ApiMethods.POST, "http://petstore.swagger.io/v2", "/user", "application/json") ApiRequest[Unit](ApiMethods.POST, "http://petstore.swagger.io/v2", "/user", "application/json")
.withBody(body) .withBody(body)
.withDefaultSuccessResponse[Unit] .withDefaultSuccessResponse[Unit]
/** /**
* Expected answers: * Expected answers:
* code 0 : (successful operation) * code 0 : (successful operation)
@ -45,6 +47,8 @@ class UserApi(baseUrl: String) {
ApiRequest[Unit](ApiMethods.POST, "http://petstore.swagger.io/v2", "/user/createWithArray", "application/json") ApiRequest[Unit](ApiMethods.POST, "http://petstore.swagger.io/v2", "/user/createWithArray", "application/json")
.withBody(body) .withBody(body)
.withDefaultSuccessResponse[Unit] .withDefaultSuccessResponse[Unit]
/** /**
* Expected answers: * Expected answers:
* code 0 : (successful operation) * code 0 : (successful operation)
@ -55,6 +59,8 @@ class UserApi(baseUrl: String) {
ApiRequest[Unit](ApiMethods.POST, "http://petstore.swagger.io/v2", "/user/createWithList", "application/json") ApiRequest[Unit](ApiMethods.POST, "http://petstore.swagger.io/v2", "/user/createWithList", "application/json")
.withBody(body) .withBody(body)
.withDefaultSuccessResponse[Unit] .withDefaultSuccessResponse[Unit]
/** /**
* This can only be done by the logged in user. * This can only be done by the logged in user.
* *
@ -69,6 +75,8 @@ class UserApi(baseUrl: String) {
.withPathParam("username", username) .withPathParam("username", username)
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
.withErrorResponse[Unit](404) .withErrorResponse[Unit](404)
/** /**
* Expected answers: * Expected answers:
* code 200 : User (successful operation) * code 200 : User (successful operation)
@ -83,6 +91,8 @@ class UserApi(baseUrl: String) {
.withSuccessResponse[User](200) .withSuccessResponse[User](200)
.withErrorResponse[Unit](400) .withErrorResponse[Unit](400)
.withErrorResponse[Unit](404) .withErrorResponse[Unit](404)
/** /**
* Expected answers: * Expected answers:
* code 200 : String (successful operation) * code 200 : String (successful operation)
@ -105,6 +115,7 @@ class UserApi(baseUrl: String) {
def xRateLimit(r: ApiReturnWithHeaders) = r.getIntHeader("X-Rate-Limit") def xRateLimit(r: ApiReturnWithHeaders) = r.getIntHeader("X-Rate-Limit")
def xExpiresAfter(r: ApiReturnWithHeaders) = r.getDateTimeHeader("X-Expires-After") def xExpiresAfter(r: ApiReturnWithHeaders) = r.getDateTimeHeader("X-Expires-After")
} }
/** /**
* Expected answers: * Expected answers:
* code 0 : (successful operation) * code 0 : (successful operation)
@ -112,6 +123,8 @@ class UserApi(baseUrl: String) {
def logoutUser(): ApiRequest[Unit] = def logoutUser(): ApiRequest[Unit] =
ApiRequest[Unit](ApiMethods.GET, "http://petstore.swagger.io/v2", "/user/logout", "application/json") ApiRequest[Unit](ApiMethods.GET, "http://petstore.swagger.io/v2", "/user/logout", "application/json")
.withDefaultSuccessResponse[Unit] .withDefaultSuccessResponse[Unit]
/** /**
* This can only be done by the logged in user. * This can only be done by the logged in user.
* *
@ -130,5 +143,7 @@ class UserApi(baseUrl: String) {
.withErrorResponse[Unit](404) .withErrorResponse[Unit](404)
} }