Skip to content

Commit

Permalink
OpenAPI routes mixin refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
gnuzzz authored and ihostage committed Oct 14, 2020
1 parent 3473585 commit 87918cd
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 27 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ def method: ServiceCall[_, _]
In conclusion, you must add the route for OpenAPI specification. You can do that with helper function `withOpenAPI` (default route is `/_<service_name>/openapi`)

```scala
override def descriptor: Descriptor = withOpenAPI(
named("service")
override def descriptor: Descriptor = named("service")
.withOpenAPI()
...
)
```

or use a custom route
Expand Down Expand Up @@ -446,7 +445,7 @@ Also, you can change filename using `openapi.file` configuration in `application
openapi.file = foobar.yml
```

In conclusion, you must add the route for OpenAPI specification. You can do that with helper function `withOpenAPI` (default route is `/_<service_name>/openapi`)
In conclusion, you must add the route for OpenAPI specification. You can do that with helper function `org.taymyr.lagom.javadsl.openapi.OpenAPIUtils#withOpenAPI` (default route is `/_<service_name>/openapi`)

```java
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@file:JvmName("OpenAPIUtils")
package org.taymyr.lagom.javadsl.openapi

import akka.NotUsed
Expand All @@ -21,7 +22,11 @@ interface OpenAPIService : Service {
fun openapi(format: Optional<String>): ServiceCall<NotUsed, String>

@JvmDefault
fun withOpenAPI(descriptor: Descriptor): Descriptor = descriptor.withCalls(
pathCall<NotUsed, String>("/_${descriptor.name()}/openapi?format", OpenAPIService::openapi.javaMethod)
)
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Use static function OpenAPIUtils.withOpenAPI(...) instead.")
fun withOpenAPI(descriptor: Descriptor): Descriptor = descriptor.withOpenAPI()
}

fun Descriptor.withOpenAPI(): Descriptor = this.withCalls(
pathCall<NotUsed, String>("/_${this.name()}/openapi?format", OpenAPIService::openapi.javaMethod)
)
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ void serviceWithDefaultYmlSpec() throws InterruptedException, ExecutionException
);
}

@Test
@DisplayName("Service without config should be return default yml spec")
void ktServiceWithDefaultYmlSpec() throws InterruptedException, ExecutionException, TimeoutException {
check(
new Test4ServiceImpl(ConfigFactory.load()),
yamlToJson(resourceAsString("test4.yml"))
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.lightbend.lagom.javadsl.api.Descriptor;
import com.lightbend.lagom.javadsl.api.Service;
import org.taymyr.lagom.javadsl.openapi.OpenAPIService;
import org.taymyr.lagom.javadsl.openapi.OpenAPIService;
import org.taymyr.lagom.javadsl.openapi.OpenAPIUtils;

public interface Test1Service extends OpenAPIService {
@Override
default Descriptor descriptor() {
return withOpenAPI(Service.named("test1"));
return OpenAPIUtils.withOpenAPI(Service.named("test1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.lightbend.lagom.javadsl.api.Descriptor;
import com.lightbend.lagom.javadsl.api.Service;
import org.taymyr.lagom.javadsl.openapi.OpenAPIService;
import org.taymyr.lagom.javadsl.openapi.OpenAPIService;
import org.taymyr.lagom.javadsl.openapi.OpenAPIUtils;

public interface Test2Service extends OpenAPIService {
@Override
default Descriptor descriptor() {
return withOpenAPI(Service.named("test2"));
return OpenAPIUtils.withOpenAPI(Service.named("test2"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.lightbend.lagom.javadsl.api.Descriptor;
import com.lightbend.lagom.javadsl.api.Service;
import org.taymyr.lagom.javadsl.openapi.OpenAPIService;
import org.taymyr.lagom.javadsl.openapi.OpenAPIService;
import org.taymyr.lagom.javadsl.openapi.OpenAPIUtils;

public interface Test3Service extends OpenAPIService {
@Override
default Descriptor descriptor() {
return withOpenAPI(Service.named("test3"));
return OpenAPIUtils.withOpenAPI(Service.named("test3"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.lightbend.lagom.javadsl.api.Descriptor;
import com.lightbend.lagom.javadsl.api.Service;
import org.taymyr.lagom.javadsl.openapi.OpenAPIService;
import org.taymyr.lagom.javadsl.openapi.OpenAPIUtils;

public interface EmptyService extends OpenAPIService {
@Override
default Descriptor descriptor() {
return withOpenAPI(Service.named("test"));
return OpenAPIUtils.withOpenAPI(Service.named("test"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.swagger.v3.oas.annotations.servers.Server;
import org.taymyr.lagom.javadsl.openapi.LagomError;
import org.taymyr.lagom.javadsl.openapi.OpenAPIService;
import org.taymyr.lagom.javadsl.openapi.OpenAPIUtils;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -177,7 +178,7 @@ public interface PetsService extends OpenAPIService {

@Override
default Descriptor descriptor() {
return withOpenAPI(
return OpenAPIUtils.withOpenAPI(
named("test").withCalls(
pathCall("/pets?tags&limit", this::find),
restCall(Method.POST, "/pets", this::create),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.taymyr.lagom.javadsl.openapi.file

import com.lightbend.lagom.javadsl.api.Descriptor
import com.lightbend.lagom.javadsl.api.Service
import org.taymyr.lagom.javadsl.openapi.OpenAPIService
import org.taymyr.lagom.javadsl.openapi.withOpenAPI

interface Test4Service : OpenAPIService {
@JvmDefault
override fun descriptor(): Descriptor {
return Service.named("test4").withOpenAPI()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.taymyr.lagom.javadsl.openapi.file

import com.typesafe.config.Config
import org.taymyr.lagom.javadsl.openapi.AbstractOpenAPIService

class Test4ServiceImpl(config: Config) : AbstractOpenAPIService(config), Test4Service
1 change: 1 addition & 0 deletions java/impl/src/test/resources/test4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: "test4"
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ trait OpenAPIService {
*/
def openapi(format: Option[String]): ServiceCall[NotUsed, String]

def withOpenAPI(descriptor: Descriptor): Descriptor = {
descriptor.addCalls(
pathCall(s"/_${descriptor.name}/openapi?format", openapi _)
)
}
/**
* @deprecated Use extension function withOpenAPI() instead.
*/
@Deprecated
def withOpenAPI(descriptor: Descriptor): Descriptor = descriptor.withOpenAPI()

implicit class DescriptorWithOpenAPI(descriptor: Descriptor) {
def withOpenAPI(): Descriptor = {
descriptor.addCalls(
pathCall(s"/_${descriptor.name}/openapi?format", openapi _)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import org.taymyr.lagom.scaladsl.openapi.OpenAPIService
import org.taymyr.lagom.scaladsl.openapi.OpenAPIServiceImpl

trait Test1Service extends OpenAPIService with Service {
override def descriptor: Descriptor = withOpenAPI(Service.named("test1"))
override def descriptor: Descriptor = Service.named("test1").withOpenAPI()
}
class Test1ServiceImpl(override val config: Config) extends Test1Service with OpenAPIServiceImpl {}

trait Test2Service extends OpenAPIService with Service {
override def descriptor: Descriptor = withOpenAPI(Service.named("test2"))
override def descriptor: Descriptor = Service.named("test2").withOpenAPI()
}
class Test2ServiceImpl(override val config: Config) extends Test2Service with OpenAPIServiceImpl {}

trait Test3Service extends OpenAPIService with Service {
override def descriptor: Descriptor = withOpenAPI(Service.named("test3"))
override def descriptor: Descriptor = Service.named("test3").withOpenAPI()
}
class Test3ServiceImpl(override val config: Config) extends Test3Service with OpenAPIServiceImpl {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.taymyr.lagom.scaladsl.openapi.OpenAPIService
import org.taymyr.lagom.scaladsl.openapi.OpenAPIServiceImpl

trait EmptyService extends OpenAPIService with Service {
override def descriptor: Descriptor = withOpenAPI(Service.named("test"))
override def descriptor: Descriptor = Service.named("test").withOpenAPI()
}

class EmptyServiceImpl(override val config: Config) extends EmptyService with OpenAPIServiceImpl {}
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ trait PetsService extends OpenAPIService with Service {
def delete(id: Long): ServiceCall[NotUsed, NotUsed]

override def descriptor: Descriptor =
withOpenAPI(
named("test").withCalls(
named("test")
.withOpenAPI()
.withCalls(
pathCall("/pets?tags&limit", find _),
restCall(Method.POST, "/pets", create),
pathCall("/pets/:id", findBy _),
restCall(Method.DELETE, "/pets/:id", delete _)
)
)

}

0 comments on commit 87918cd

Please sign in to comment.