[finch] Allow finch server to compile for CI checks (#7)

Previous error handling implementation had types returning
Either[CommonError, UserType], but implemented with the scala shortcut
??? which throws an exception instead. This causes compilation to fail
with a message that the expected CommonError is of type Any. This is
often fixable with generic upper bounds constraints, but this is
overkill for a placeholder implementation. Returning a temporary 'TODO'
type solves the compile error, and should allow CI to check for valid
compilation on changes.

Included in this is also a fix to support optional query parameter
types. The spec used to generate the finch server has optional query
parameters, but the version of finch in the template doesn't support
options on query parameters. Finch does, however, aggregate everything
(headers, query string, path parameters, etc) under "param" with
"paramOption" for those which are optional types.
This commit is contained in:
Jim Schubert 2018-05-12 23:07:14 -04:00 committed by William Cheng
parent d80e295852
commit 52322c47c9
9 changed files with 68 additions and 69 deletions

View File

@ -204,20 +204,7 @@ public class FinchServerCodegen extends DefaultCodegen implements CodegenConfig
return outputFolder + File.separator + sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar); return outputFolder + File.separator + sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar);
} }
/** @SuppressWarnings("unchecked")
* Convert OpenAPI Model object to Codegen Model object
*
* @param name the name of the model
* @param model OpenAPI Model object
* @param allDefinitions a map of all OpenAPI models from the spec
* @return Codegen Model object
*/
@Override
public CodegenModel fromModel(String name, Schema model, Map<String, Schema> allDefinitions) {
CodegenModel codegenModel = super.fromModel(name, model, allDefinitions);
return codegenModel;
}
@Override @Override
public Map<String, Object> postProcessOperations(Map<String, Object> objs) { public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations"); Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
@ -244,6 +231,7 @@ public class FinchServerCodegen extends DefaultCodegen implements CodegenConfig
} }
@SuppressWarnings("Duplicates")
@Override @Override
public String getTypeDeclaration(Schema p) { public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) { if (ModelUtils.isArraySchema(p)) {
@ -261,7 +249,7 @@ public class FinchServerCodegen extends DefaultCodegen implements CodegenConfig
@Override @Override
public String getSchemaType(Schema p) { public String getSchemaType(Schema p) {
String schemaType = super.getSchemaType(p); String schemaType = super.getSchemaType(p);
String type = null; String type;
if (typeMapping.containsKey(schemaType)) { if (typeMapping.containsKey(schemaType)) {
type = typeMapping.get(schemaType); type = typeMapping.get(schemaType);
if (languageSpecificPrimitives.contains(type)) { if (languageSpecificPrimitives.contains(type)) {
@ -371,9 +359,9 @@ public class FinchServerCodegen extends DefaultCodegen implements CodegenConfig
String scalaPath = ""; String scalaPath = "";
Integer pathParamIndex = 0; Integer pathParamIndex = 0;
for (int i = 0; i < items.length; ++i) { for (String item : items) {
if (items[i].matches("^\\{(.*)\\}$")) { // wrap in {} if (item.matches("^\\{(.*)}$")) { // wrap in {}
// find the datatype of the parameter // find the datatype of the parameter
final CodegenParameter cp = op.pathParams.get(pathParamIndex); final CodegenParameter cp = op.pathParams.get(pathParamIndex);
@ -382,7 +370,7 @@ public class FinchServerCodegen extends DefaultCodegen implements CodegenConfig
pathParamIndex++; pathParamIndex++;
} else { } else {
scalaPath = colConcat(scalaPath, "\"" + items[i] + "\""); scalaPath = colConcat(scalaPath, "\"" + item + "\"");
} }
} }
@ -431,7 +419,14 @@ public class FinchServerCodegen extends DefaultCodegen implements CodegenConfig
p.vendorExtensions.put("x-codegen-normalized-path-type", "fileUpload(\"" + p.paramName + "\")"); p.vendorExtensions.put("x-codegen-normalized-path-type", "fileUpload(\"" + p.paramName + "\")");
p.vendorExtensions.put("x-codegen-normalized-input-type", "FileUpload"); p.vendorExtensions.put("x-codegen-normalized-input-type", "FileUpload");
} else if (p.isPrimitiveType && !p.isPathParam) { } else if (p.isPrimitiveType && !p.isPathParam) {
p.vendorExtensions.put("x-codegen-normalized-path-type", p.dataType.toLowerCase()); if (!p.required) {
// Generator's current version of Finch doesn't support something like stringOption, but finch aggregates all
// parameter types under "param", so optional params can be grabbed by "paramOption".
p.vendorExtensions.put("x-codegen-normalized-path-type", toPathParameter(p, "param", true));
} else {
// If parameter is primitive and required, we can rely on data types like "string" or "long"
p.vendorExtensions.put("x-codegen-normalized-path-type", p.dataType.toLowerCase());
}
p.vendorExtensions.put("x-codegen-normalized-input-type", toInputParameter(p)); p.vendorExtensions.put("x-codegen-normalized-input-type", toInputParameter(p));
} else { } else {
//Path paremeters are handled in generateScalaPath() //Path paremeters are handled in generateScalaPath()

View File

@ -4,13 +4,15 @@ package {{packageName}}
import java.io._ import java.io._
import java.util.UUID import java.util.UUID
import java.time._ import java.time._
import com.twitter.finagle.http.exp.Multipart.{FileUpload, InMemoryFileUpload, OnDiskFileUpload}
import {{modelPackage}}._ import {{modelPackage}}._
trait DataAccessor { trait DataAccessor {
// TODO: apiInfo -> apis -> operations = ??? // TODO: apiInfo -> apis -> operations = TODO error
// NOTE: ??? throws a not implemented exception private object TODO extends CommonError("Not implemented") {
def message = "Not implemented"
}
{{#apiInfo}} {{#apiInfo}}
{{#apis}} {{#apis}}
@ -20,7 +22,7 @@ trait DataAccessor {
* {{{description}}} * {{{description}}}
* @return A {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}} * @return A {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
*/ */
def {{baseName}}_{{operationId}}({{{vendorExtensions.x-codegen-typedParams}}}): Either[CommonError,{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}] = ??? def {{baseName}}_{{operationId}}({{{vendorExtensions.x-codegen-typedParams}}}): Either[CommonError,{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}] = Left(TODO)
{{/operation}} {{/operation}}
{{/operations}} {{/operations}}

View File

@ -58,7 +58,7 @@ object {{classname}} {
* @return An endpoint representing a {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}} * @return An endpoint representing a {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
*/ */
private def {{operationId}}(da: DataAccessor): Endpoint[{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}] = private def {{operationId}}(da: DataAccessor): Endpoint[{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}] =
{{httpMethod}}({{{vendorExtensions.x-codegen-paths}}}) { {{#hasParams}}({{{vendorExtensions.x-codegen-typedParams}}}) => {{/hasParams}} {{httpMethod}}({{{vendorExtensions.x-codegen-paths}}}) { {{#vendorExtensions.x-codegen-typedParams}}({{{vendorExtensions.x-codegen-typedParams}}}) =>{{/vendorExtensions.x-codegen-typedParams}}
da.{{baseName}}_{{operationId}}({{{vendorExtensions.x-codegen-params}}}) match { da.{{baseName}}_{{operationId}}({{{vendorExtensions.x-codegen-params}}}) match {
case Left(error) => checkError(error) case Left(error) => checkError(error)
case Right(data) => Ok(data) case Right(data) => Ok(data)

View File

@ -6,7 +6,7 @@ name := "finch-sample"
version := "0.1.0-SNAPSHOT" version := "0.1.0-SNAPSHOT"
scalaVersion := "2.11.12" scalaVersion := "2.12.3"
resolvers += Resolver.sonatypeRepo("snapshots") resolvers += Resolver.sonatypeRepo("snapshots")

View File

@ -6,7 +6,7 @@ name := "finch-sample"
version := "0.1.0-SNAPSHOT" version := "0.1.0-SNAPSHOT"
scalaVersion := "2.11.12" scalaVersion := "2.12.3"
resolvers += Resolver.sonatypeRepo("snapshots") resolvers += Resolver.sonatypeRepo("snapshots")

View File

@ -4,132 +4,134 @@ package org.openapitools
import java.io._ import java.io._
import java.util.UUID import java.util.UUID
import java.time._ import java.time._
import com.twitter.finagle.http.exp.Multipart.{FileUpload, InMemoryFileUpload, OnDiskFileUpload}
import org.openapitools.models._ import org.openapitools.models._
trait DataAccessor { trait DataAccessor {
// TODO: apiInfo -> apis -> operations = ??? // TODO: apiInfo -> apis -> operations = TODO error
// NOTE: ??? throws a not implemented exception private object TODO extends CommonError("Not implemented") {
def message = "Not implemented"
}
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def Pet_addPet(pet: Pet): Either[CommonError,Unit] = ??? def Pet_addPet(pet: Pet): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def Pet_deletePet(petId: Long, apiKey: Option[String]): Either[CommonError,Unit] = ??? def Pet_deletePet(petId: Long, apiKey: Option[String]): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A Seq[Pet] * @return A Seq[Pet]
*/ */
def Pet_findPetsByStatus(status: Seq[String]): Either[CommonError,Seq[Pet]] = ??? def Pet_findPetsByStatus(status: Seq[String]): Either[CommonError,Seq[Pet]] = Left(TODO)
/** /**
* *
* @return A Seq[Pet] * @return A Seq[Pet]
*/ */
def Pet_findPetsByTags(tags: Seq[String]): Either[CommonError,Seq[Pet]] = ??? def Pet_findPetsByTags(tags: Seq[String]): Either[CommonError,Seq[Pet]] = Left(TODO)
/** /**
* *
* @return A Pet * @return A Pet
*/ */
def Pet_getPetById(petId: Long, authParamapi_key: String): Either[CommonError,Pet] = ??? def Pet_getPetById(petId: Long, authParamapi_key: String): Either[CommonError,Pet] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def Pet_updatePet(pet: Pet): Either[CommonError,Unit] = ??? def Pet_updatePet(pet: Pet): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def Pet_updatePetWithForm(petId: Long, name: Option[String], status: Option[String]): Either[CommonError,Unit] = ??? def Pet_updatePetWithForm(petId: Long, name: Option[String], status: Option[String]): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A ApiResponse * @return A ApiResponse
*/ */
def Pet_uploadFile(petId: Long, additionalMetadata: Option[String], file: FileUpload): Either[CommonError,ApiResponse] = ??? def Pet_uploadFile(petId: Long, additionalMetadata: Option[String], file: FileUpload): Either[CommonError,ApiResponse] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def Store_deleteOrder(orderId: String): Either[CommonError,Unit] = ??? def Store_deleteOrder(orderId: String): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A Map[String, Int] * @return A Map[String, Int]
*/ */
def Store_getInventory(authParamapi_key: String): Either[CommonError,Map[String, Int]] = ??? def Store_getInventory(authParamapi_key: String): Either[CommonError,Map[String, Int]] = Left(TODO)
/** /**
* *
* @return A Order * @return A Order
*/ */
def Store_getOrderById(orderId: Long): Either[CommonError,Order] = ??? def Store_getOrderById(orderId: Long): Either[CommonError,Order] = Left(TODO)
/** /**
* *
* @return A Order * @return A Order
*/ */
def Store_placeOrder(order: Order): Either[CommonError,Order] = ??? def Store_placeOrder(order: Order): Either[CommonError,Order] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def User_createUser(user: User): Either[CommonError,Unit] = ??? def User_createUser(user: User): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def User_createUsersWithArrayInput(user: Seq[User]): Either[CommonError,Unit] = ??? def User_createUsersWithArrayInput(user: Seq[User]): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def User_createUsersWithListInput(user: Seq[User]): Either[CommonError,Unit] = ??? def User_createUsersWithListInput(user: Seq[User]): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def User_deleteUser(username: String): Either[CommonError,Unit] = ??? def User_deleteUser(username: String): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A User * @return A User
*/ */
def User_getUserByName(username: String): Either[CommonError,User] = ??? def User_getUserByName(username: String): Either[CommonError,User] = Left(TODO)
/** /**
* *
* @return A String * @return A String
*/ */
def User_loginUser(username: String, password: String): Either[CommonError,String] = ??? def User_loginUser(username: String, password: String): Either[CommonError,String] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def User_logoutUser(): Either[CommonError,Unit] = ??? def User_logoutUser(): Either[CommonError,Unit] = Left(TODO)
/** /**
* *
* @return A Unit * @return A Unit
*/ */
def User_updateUser(username: String, user: User): Either[CommonError,Unit] = ??? def User_updateUser(username: String, user: User): Either[CommonError,Unit] = Left(TODO)
} }

View File

@ -144,7 +144,7 @@ object PetApi {
* @return An endpoint representing a Unit * @return An endpoint representing a Unit
*/ */
private def updatePetWithForm(da: DataAccessor): Endpoint[Unit] = private def updatePetWithForm(da: DataAccessor): Endpoint[Unit] =
post("pet" :: long :: string :: string) { (petId: Long, name: Option[String], status: Option[String]) => post("pet" :: long :: paramOption("name") :: paramOption("status")) { (petId: Long, name: Option[String], status: Option[String]) =>
da.Pet_updatePetWithForm(petId, name, status) match { da.Pet_updatePetWithForm(petId, name, status) match {
case Left(error) => checkError(error) case Left(error) => checkError(error)
case Right(data) => Ok(data) case Right(data) => Ok(data)
@ -158,7 +158,7 @@ object PetApi {
* @return An endpoint representing a ApiResponse * @return An endpoint representing a ApiResponse
*/ */
private def uploadFile(da: DataAccessor): Endpoint[ApiResponse] = private def uploadFile(da: DataAccessor): Endpoint[ApiResponse] =
post("pet" :: long :: "uploadImage" :: string :: fileUpload("file")) { (petId: Long, additionalMetadata: Option[String], file: FileUpload) => post("pet" :: long :: "uploadImage" :: paramOption("additionalMetadata") :: fileUpload("file")) { (petId: Long, additionalMetadata: Option[String], file: FileUpload) =>
da.Pet_uploadFile(petId, additionalMetadata, file) match { da.Pet_uploadFile(petId, additionalMetadata, file) match {
case Left(error) => checkError(error) case Left(error) => checkError(error)
case Right(data) => Ok(data) case Right(data) => Ok(data)

View File

@ -68,7 +68,7 @@ object StoreApi {
* @return An endpoint representing a Map[String, Int] * @return An endpoint representing a Map[String, Int]
*/ */
private def getInventory(da: DataAccessor): Endpoint[Map[String, Int]] = private def getInventory(da: DataAccessor): Endpoint[Map[String, Int]] =
get("store" :: "inventory" :: header("api_key")) { get("store" :: "inventory" :: header("api_key")) { (authParamapi_key: String) =>
da.Store_getInventory(authParamapi_key) match { da.Store_getInventory(authParamapi_key) match {
case Left(error) => checkError(error) case Left(error) => checkError(error)
case Right(data) => Ok(data) case Right(data) => Ok(data)

View File

@ -143,7 +143,7 @@ object UserApi {
* @return An endpoint representing a Unit * @return An endpoint representing a Unit
*/ */
private def logoutUser(da: DataAccessor): Endpoint[Unit] = private def logoutUser(da: DataAccessor): Endpoint[Unit] =
get("user" :: "logout") { get("user" :: "logout") { () =>
da.User_logoutUser() match { da.User_logoutUser() match {
case Left(error) => checkError(error) case Left(error) => checkError(error)
case Right(data) => Ok(data) case Right(data) => Ok(data)