Implement your own object binder for Route parameter of some object type in Play scala - scala

Well, I want to replace my String param from the following Play scala Route into my own object, say "MyObject"
From GET /api/:id controllers.MyController.get(id: String)
To GET /api/:id controllers.MyController.get(id: MyOwnObject)
Any idea on how to do this would be appreciated.

Well, I have written up my own "MyOwnObject" binder now. Another way of implementing PathBindable to bind an object.
object Binders {
implicit def pathBinder(implicit intBinder: PathBindable[String]) = new PathBindable[MyOwnObject] {
override def bind(key: String, value: String): Either[String, MyOwnObject] = {
for {
id <- intBinder.bind(key, value).right
} yield UniqueId(id)
}
override def unbind(key: String, id: UniqueId): String = {
intBinder.unbind(key, id.value)
}
}
}

Use PathBindable to bind parameters from path rather than from query. Sample implementation for binding ids from path separated by comma (no error handling):
public class CommaSeparatedIds implements PathBindable<CommaSeparatedIds> {
private List<Long> id;
#Override
public IdBinder bind(String key, String txt) {
if ("id".equals(key)) {
String[] split = txt.split(",");
id = new ArrayList<>(split.length + 1);
for (String s : split) {
long parseLong = Long.parseLong(s);
id.add(Long.valueOf(parseLong));
}
return this;
}
return null;
}
...
}
Sample path:
/data/entity/1,2,3,4
Sample routes entry:
GET /data/entity/:id controllers.EntityController.process(id: CommaSeparatedIds)

I'm not sure if it works for binding data in the path part of a URL, but you may want to read the docs on QueryStringBindable if you're able to accept your data as query params.

Related

Spring boot return dataclass

I have a problem returning data class and handling it in the frontend. I dont know if the problem relies in the backend or the frontend
#RestController
#CrossOrigin
#RequestMapping(path = ["/1/private/profile"])
class ProfileController {
...OTHER CODE...
#GetMapping(
path = ["/getUser"],
consumes = [MediaType.APPLICATION_JSON_VALUE],
produces = [MediaType.APPLICATION_JSON_VALUE])
fun getUser(#RequestHeader(AUTHORIZATION) token: String): User? {
val uid = token.getUid()
val user = userRepository?.findItemByUid(uid)
user?.let {
return user
}?: kotlin.run {
return null
}
}
}
This is the controller holding the endpoint called from the frontend. The endpoint is returning 200 when called from the frontend. But the body returned is completely wrong.
response body: {"id":{"timestamp":1672696689,"date…y":"Sweden","uid":"4yRhpe43hHUwDGkqMqVBqX3G60p2"}
But expected is json string from the data class below
#Document("users")
data class User(
val id: ObjectId = ObjectId.get(),
val fullName: String,
val email: String,
val country: String,
val uid: String
)
What could i be doing wrong? It works perfectly if the endpoint for some reason returns a list of objects.

ReactiveStreamCrudRepository not returning data from postgres DB

I'm new to reactive programming and micronaut. I'm basically working on simple CRUD APIs. I'm using Kotlin with micronaut. I'm not sure why the DB is not returning any Data and I'm stuck with this.
#JdbcRepository(dialect = Dialect.POSTGRES)
interface EmployeeCrudRepository: ReactiveStreamsCrudRepository<EmployeeMaster, Int>, EmployeeRepository {
}
interface EmployeeRepository {
fun findByEmployeeIdAndTcin(employeeId: UUID, tcin: String): Mono<EmployeeMaster>
}
#MappedEntity
#Table(name="employee")
data class EmployeeMaster (
#Id
#Column(name = "transaction_id")
val transactionId: Int,
#Column(name = "employee_id")
val employeeId: UUID,
#Column(name = "item_id")
val itemId: UUID
)
fun getEmployeeDetailsResponse(registryId: UUID, itemId: String) : Mono<EmployeeDetailsDTO> {
return getEmployeeDetails(employeeId, itemId)
.map {
employeeDetails -> EmployeeDetailsDTO(employeeDetails)
}
.switchIfEmpty {
logger.info("No records found")
Mono.just(ItemDetailsDTO())
}
}
fun getEmployeeDetails(employeeId: UUID, itemId: String) : Mono<EmployeeDetailsDTO> {
return employeeRepository.findByEmployeeIdAndTcin(registryId = registryId, tcin = itemId)
.map {
employeeDetails -> EmployeeDetailsDTO(employeeDetails)
}
.switchIfEmpty {
logger.info("No records found")
Mono.just(EmployeeDetailsDTO())
}
}
I'm confused as to how to debug this to find the issue. The credentials all seem to be fine and the record I'm searching for exists in the DB.
flyway {
// ./gradlew -Ppostgres_host=localhost -Ppostgres_ssl='' -Ppostgres_user=postgres -Ppostgres_pwd=postgres flywayMigrate -i
url = "jdbc:postgresql://${postgres_host}:5432/postgres${postgres_ssl}"
user = "${postgres_user}"
password = "${postgres_pwd}"
schemas = ['public']
}
Issue Found:
My Bad, I was sending some other value and didn't realise that the value was incorrect. The implementation was fine and returning the response as expected. I'm writing kotlin and micronaut code for the first time and at the back of my head it always feels like the implementation was wrong.
Take a look at Access a Database with Micronaut Data R2DBC.
There are a number of things in your example that look wrong:
#JdbcRepository(dialect = Dialect.POSTGRES) should be #R2dbcRepository.
#Table and #Column are JPA.
#MappedEntity("employee")
#MappedProperty("item_id")
You might not need #MappedProperty("item_id"), itemId should be mapped to item_id.

Kotlin Data Class: How generate an MongoDB ObjectId for an embedded document

I'm building an API with Spring Boot and Kotlin. I am trying to generate a structure the following way in MongoDB.
I understand that in MongoDb the concept of relationships between entities does not exist, so I will use the strategy of embedded documents. That is, to embed the Reunión in the Proyecto, and the Participante in the Reunión.
I have a main class called Proyecto and NewProyecto, that contains as property a list of reuniones of type NewReunion. I use two different classes to create and return data.
Proyecto.kt
#Document(collection = "proyectos")
#TypeAlias("proyecto")
data class Proyecto (
#Id
val id: String,
val nombre: String,
val area: String,
val fecha:String,
val reuniones: List<Reunion>?
){}
#Document(collection = "proyectos")
#TypeAlias("newproyecto")
data class NewProyecto (
#Id
val id: String?,//Es posiblemente nulo porqué se crea automáticamente
var nombre: String,
var area: String,
var fecha:String,
var reuniones: List<NewReunion>?
){}
Now, to create 'reuniones' I have two classes, Reunion and NewReunion. The class that corresponds to create a MongoDB embedded document is NewReunion.
NewReunion.kt
#Document
data class Reunion(
val objetivo: String,
val fecha: String,
val participantes: List<Participante>?
) {}
#Document
data class NewReunion(
var id: String? = ObjectId().toHexString(),
var fecha: String,
var participantes: List<NewParticipante>?
) {}
This is where I have the problem. I want to generate an ObjectId for this NewReunion class, so that each object embedded in it has an id. The problem is that ObjectId ().ToHexString() is not generating any value at the time that the object of type NewReunion is built, but the other data that are objetivo and fecha are filled with the data that comes from the request POST.
How I send the information.
The information I send via POST. This request is handled by a Controller named ProyectoController.kt
ProyectoController.kt
#PostMapping("/")
fun createProyecto(#RequestBody newProyecto: NewProyecto): NewProyecto = proyectoService.createProyecto(newProyecto)
ProyectoRepository.kt
interface ProyectoRepository : MongoRepository<Proyecto, String> {
fun findById(id: ObjectId): Proyecto
override fun findAll(): List<Proyecto>
fun insert(proyecto: NewProyecto): NewProyecto
fun save(proyect: Proyecto): Proyecto
fun deleteById(id: ObjectId)
}
ProyectoService.kt
#Service("proyectoService")
class ProyectoServiceImpl : ProyectoService {
#Autowired
lateinit var proyectoRepository: ProyectoRepository
//Obtener un proyecto
override fun findById(id: ObjectId): Proyecto = proyectoRepository.findById(id)
//Obtener todos los proyectos
override fun findAll(): List<Proyecto> = proyectoRepository.findAll()
//Crear un proyecto
override fun createProyecto(newProyecto: NewProyecto): NewProyecto = proyectoRepository.insert(newProyecto)
//Actualizar un proyecto
override fun updateProyecto(proyecto: Proyecto):Proyecto = proyectoRepository.save(proyecto)
//Eliminar un proyecto
override fun deleteProyecto(id:ObjectId) = proyectoRepository.deleteById(id)
}
POST using Postman:
To send the information I am using Postman, and I send the request in the following way.
At the time of creating the new Proyecto, I return it to see the result which returns a result with id=null, but all other fields do assign the corresponding value:
Now, I tried initializing all the constructor parameters of the NewReunion class to see what happened.
data class NewReunion(
#Id
var id: String? = ObjectId().toHexString(),
var objetivo: String = "",
var fecha: String = ""
) {}
the value for the id is generated correctly together with the other values. It is just this behavior that I do not understand why I need to initialize constructor parameters of the NewReunion class.
Result of POST with the parameters initialized.
build.gradle
buildscript {
ext.kotlin_version = '1.2.71'
ext {
kotlinVersion = '1.2.71'
springBootVersion = '2.0.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
group = 'com.gibranlara'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // Required for Kotlin integration
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "org.springframework.boot:spring-boot-starter-data-mongodb"
compile 'org.springframework.boot:spring-boot-starter-web'
}
The library you are using is probably not written with Kotlin in mind.
Kotlin generates a synthetic constructor that loads default values prior to calling the actual constructor, e.g.
// Java
public NewReunion(String var1, String var2, String var3, int var4, DefaultConstructorMarker var5) {
if ((var4 & 1) != 0) {
var1 = ObjectId().toHexString();
}
this(var1, var2, var3);
}
The library is likely doing one of the following:
Calling the default constructor, then calling set[Property] matching the annotations/convention.
Calling the closest match constructor: NewReunion(#Nullable String id, #NotNull String objetivo, #NotNull String fecha) with NewReunion(null, "objetivo", "fecha")
If you define your class as such:
data class NewReunion #JvmOverloads constructor(
var id: String? = "",
var objetivo: String,
var fecha: String
)
You will get additional constructors e.g.
// Java
public NewReunion(#NotNull String objetivo, #NotNull String fecha)
If your library is using the first option then you may need to lazy initialize the id field in a getter (also convert data class to normal class).
An Aside
Most of these kind of problems stem from devs using the same object model for communication and business logic. Whenever I see a nullable id on an entity it like a clarion call that bugs are afoot.
Any data you get from an outside source(even if it's from a server you control) should be treated as if it was put there by your most baleful enemy, but many developers just suck it in and use it as it comes.
If you don't have something along the lines of
val cleanData = validate(inputData)
before crossing from an input layer to a business layer then you are setting yourself up for future embarrassment.
Input layers are:
User interface
Web services
Anything coming from outside your immediate domain of control

Angular 5 models httpClient type casting

I declare a model in ingredient.model.ts
export class Ingredient {
constructor(private name: string, public amount: number) {}
getName() { return this.name }
}
In ingredients.service.ts, if I get them in this way:
httpClient.get<Ingredient>(url).subscribe(
(igredient) => {
console.log(igredient.getName());
});
It gives errors in console, such as "no method getName in property igredient".
Also, whenever I try to declare a property type Category[] it fails, but Array seems working fine.
Edit:
I want to provide more info.
Given the Igredient model and the following JSON structure:
{
name: "Apple",
amount: "5",
created_at: "date",
}
The Igredient constructor isn't even invoked, therefore the GET payload won't be parsed.
You'll need to use a property, not a method. The returned object is really a json object, and there is no such thing as "getName()" method (despite your effort to add a type information). Try something like this:
export interface Ingredient {
strin: string,
amount: number,
created_at: string
}
httpClient.get<Ingredient>(url).subscribe(
(igredient) => {
console.log(igredient.amount);
});
EDIT: You need to provide a type information based on the expected json object. If the returned json object has attributes, strin, amount, and created_at, then you need to define a type that is compatible with the expected json object.
In angular 5, You can do this:
export interface Deserializable<T> {
deserialize(input: any): T;
}
export class Ingredient implments Deserializable<Ingredient>{
constructor(private name: string, public amount: number) {}
deserialize(input: any): Project {
Object.assign(this, input);
// do nested thing here -pop arrays of nested objects and create them
}
return this;
}
now in your service:
httpClient.get<Ingredient>(url).pipe(map(elem=>this.foo(elem)))
.subscribe((igredient) => {console.log(igredient.getName());
});
foo(ingredient:Ingrdient){
var i = new Ingridiant().desrialize(ingredient)
}
after the map you will have the Ingradient class, not the object.

Implementing an indexer in a class in TypeScript

Is it currently possible to implement an indexer on a class in TypeScript?
class MyCollection {
[name: string]: MyType;
}
This doesn't compile. I can specify an indexer on an interface, of course, but I need methods on this type as well as the indexer, so an interface won't suffice.
Thanks.
You cannot implement a class with an indexer. You can create an interface, but that interface cannot be implemented by a class. It can be implemented in plain JavaScript, and you can specify functions as well as the indexer on the interface:
class MyType {
constructor(public someVal: string) {
}
}
interface MyCollection {
[name: string]: MyType;
}
var collection: MyCollection = {};
collection['First'] = new MyType('Val');
collection['Second'] = new MyType('Another');
var a = collection['First'];
alert(a.someVal);
This is an old question, for those looking for the answer: now it's possible to define a indexed property like:
let lookup : {[key:string]:AnyType};
the signature of the key must be either string or integer see:
Interfaces on www.typescriptlang.org
Is not possible to define an indexed property getter/setter in a class but you can "simulate" that in a way like this using Proxy:
class IndexedPropSample {
[name: string | symbol]: any;
private static indexedHandler: ProxyHandler<IndexedPropSample> = {
get(target, property) {
return target[property];
},
set(target, property, value): boolean {
target[property] = value;
return true;
}
};
constructor() {
return new Proxy(this, IndexedPropSample.indexedHandler);
}
readIndexedProp = (prop: string | symbol): any => {
return this[prop];
}
}
var test = new IndexedPropSample();
test["propCustom"] = "valueCustom";
console.log(test["propCustom"]); // "valueCustom"
console.log(test.readIndexedProp("propCustom")); // "valueCustom"
console.log(test instanceof IndexedPropSample); // true
console.log(Object.keys(test)); // ["propCustom", "readIndexedProp"]
you can try it in Typescript Playground