Skip to content

Commit

Permalink
Bugfix generic extends map / list
Browse files Browse the repository at this point in the history
  • Loading branch information
kbuntrock committed Nov 10, 2024
1 parent cdd7b43 commit 53853ce
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void exploreDataObject(final DataObject dataObject) {
if(!exploredSignatures.add(dataObject.getSignature())) {
return;
}
// Generically typed objects are almost never written in the schema section (only when a recursive
// Generically typed objects are almost never written in the schema section (only when a recursive loop is detected)
if(dataObject.isReferenceObject()) {
if(schemaObjects.add(dataObject)) {
if(!dataObject.isMap()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public DataObject(final DataObject dataObject) {
this.classRequired = dataObject.classRequired;
}


public DataObject(final Type originalType) {
Type type = originalType;

Expand All @@ -131,17 +130,15 @@ public DataObject(final Type originalType) {
pt.getActualTypeArguments()[i]);
}

if(Collection.class.isAssignableFrom(javaClass)) {
arrayItemDataObject = new DataObject(pt.getActualTypeArguments()[0]);
} else if(Map.class.isAssignableFrom(javaClass)) {
mapKeyValueDataObjects[0] = new DataObject(pt.getActualTypeArguments()[0]);
mapKeyValueDataObjects[1] = new DataObject(pt.getActualTypeArguments()[1]);
if(Map.class.isAssignableFrom(javaClass)) {
computeMapTypes();
} else if(Collection.class.isAssignableFrom(javaClass)) {
computeCollectionType();
}

} else if(type instanceof GenericArrayType) {

// Parameterized array

this.genericallyTyped = true;
// See https://stackoverflow.com/questions/15450356/how-to-make-class-forname-return-array-type
final GenericArrayType gat = (GenericArrayType) type;
Expand All @@ -167,21 +164,11 @@ public DataObject(final Type originalType) {
"A GenericArrayType with a " + gat.getGenericComponentType().getClass().toString() + " is not and handled case.");
}
} else if(type instanceof Class) {
javaClass = (Class<?>) type;
if(Map.class.isAssignableFrom((Class<?>) type)) {
javaClass = (Class<?>) type;
TypeToken token = TypeToken.of(javaType);
TypeToken<Map> superType = token.getSupertype(Map.class);
Type[] resolvedArguments = ((ParameterizedType) superType.getType()).getActualTypeArguments();
mapKeyValueDataObjects[0] = new DataObject(resolvedArguments[0]);
mapKeyValueDataObjects[1] = new DataObject(resolvedArguments[1]);
computeMapTypes();
} else if(Collection.class.isAssignableFrom((Class<?>) type)) {
javaClass = (Class<?>) type;
TypeToken token = TypeToken.of(javaType);
TypeToken<Map> superType = token.getSupertype(Collection.class);
Type[] resolvedArguments = ((ParameterizedType) superType.getType()).getActualTypeArguments();
arrayItemDataObject = new DataObject(resolvedArguments[0]);
} else {
javaClass = (Class<?>) type;
computeCollectionType();
}
} else {
throw new RuntimeException(
Expand Down Expand Up @@ -228,6 +215,21 @@ public DataObject(final Type originalType) {

}

private void computeMapTypes() {
TypeToken token = TypeToken.of(javaType);
TypeToken<Map> superType = token.getSupertype(Map.class);
Type[] resolvedArguments = ((ParameterizedType) superType.getType()).getActualTypeArguments();
mapKeyValueDataObjects[0] = new DataObject(resolvedArguments[0]);
mapKeyValueDataObjects[1] = new DataObject(resolvedArguments[1]);
}

private void computeCollectionType() {
TypeToken token = TypeToken.of(javaType);
TypeToken<Map> superType = token.getSupertype(Collection.class);
Type[] resolvedArguments = ((ParameterizedType) superType.getType()).getActualTypeArguments();
arrayItemDataObject = new DataObject(resolvedArguments[0]);
}

/**
* @return true if this DataObject is a map
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import io.github.kbuntrock.resources.endpoint.error.SameOperationController;
import io.github.kbuntrock.resources.endpoint.file.FileUploadController;
import io.github.kbuntrock.resources.endpoint.file.StreamResponseController;
import io.github.kbuntrock.resources.endpoint.generic.ExtendsGenericObjectMap;
import io.github.kbuntrock.resources.endpoint.generic.ExtendsList;
import io.github.kbuntrock.resources.endpoint.generic.ExtendsListV2;
import io.github.kbuntrock.resources.endpoint.generic.ExtendsMap;
import io.github.kbuntrock.resources.endpoint.generic.GenericDataController;
import io.github.kbuntrock.resources.endpoint.generic.GenericMappingObject;
Expand Down Expand Up @@ -898,6 +900,13 @@ public void extends_list() throws MojoFailureException, IOException, MojoExecuti
checkGenerationResult(mojo.documentProject());
}

@Test
public void extends_list2() throws MojoFailureException, IOException, MojoExecutionException {

final DocumentationMojo mojo = createBasicMojo(ExtendsListV2.class.getCanonicalName());
checkGenerationResult(mojo.documentProject());
}

@Test
public void object_mapping() throws MojoFailureException, IOException, MojoExecutionException {

Expand All @@ -922,6 +931,13 @@ public void generic_object_mapping() throws MojoFailureException, IOException, M
checkGenerationResult(mojo.documentProject());
}

@Test
public void generic_object_mapping2() throws MojoFailureException, IOException, MojoExecutionException {

final DocumentationMojo mojo = createBasicMojo(ExtendsGenericObjectMap.class.getCanonicalName());
checkGenerationResult(mojo.documentProject());
}

@Test
public void nested_dtos() throws MojoFailureException, IOException, MojoExecutionException {
final DocumentationMojo mojo = createBasicMojo(NestedDtosController.class.getCanonicalName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.kbuntrock.resources.dto.genericity.extendsMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
*
*/
public class GenericExtendsList<T> extends ArrayList<UUID> {

public GenericExtendsList<T> doSomething(List<T> rows) {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.kbuntrock.resources.dto.genericity.extendsMap;

import java.util.HashMap;
import java.util.List;


public class GenericExtendsObjectMap<T> extends HashMap<String, Object> {

public GenericExtendsObjectMap<T> rows(List<T> rows) {
this.put("rows", rows);
return this;
}

public <G> GenericExtendsObjectMap<T> lines(List<G> lines) {
this.put("lines", lines);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.kbuntrock.resources.endpoint.generic;

import io.github.kbuntrock.resources.dto.genericity.extendsMap.GenericExtendsObjectMap;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@RequestMapping("/extends-generic-object-map")
public class ExtendsGenericObjectMap {

@GetMapping(path = "/map")
public GenericExtendsObjectMap<UUID> getMap() {
GenericExtendsObjectMap<UUID> map = new GenericExtendsObjectMap();
List<UUID> uuidList = new ArrayList<>();
map.rows(uuidList);
List<Long> longList = new ArrayList<>();
map.lines(longList);
return map;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.kbuntrock.resources.endpoint.generic;

import io.github.kbuntrock.resources.dto.genericity.extendsMap.GenericExtendsList;
import io.github.kbuntrock.resources.dto.genericity.extendsMap.GenericExtendsObjectMap;
import java.util.UUID;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/extends-list-v2")
public interface ExtendsListV2 {


@GetMapping(path = "/")
GenericExtendsList<Long> getList();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
openapi: 3.0.3
info:
title: My Project
version: 10.5.36
servers:
- url: ""
tags:
- name: ExtendsListV2
paths:
/extends-list-v2/:
get:
tags:
- ExtendsListV2
operationId: getList
responses:
200:
description: successful operation
content:
'*/*':
schema:
type: array
items:
type: string
format: uuid
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
openapi: 3.0.3
info:
title: My Project
version: 10.5.36
servers:
- url: ""
tags:
- name: ExtendsGenericObjectMap
paths:
/extends-generic-object-map/map:
get:
tags:
- ExtendsGenericObjectMap
operationId: getMap
responses:
200:
description: successful operation
content:
'*/*':
schema:
type: object
additionalProperties: {}

0 comments on commit 53853ce

Please sign in to comment.