diff --git a/openapi-maven-plugin/src/main/java/io/github/kbuntrock/TagLibrary.java b/openapi-maven-plugin/src/main/java/io/github/kbuntrock/TagLibrary.java index 338557b..8642cfa 100644 --- a/openapi-maven-plugin/src/main/java/io/github/kbuntrock/TagLibrary.java +++ b/openapi-maven-plugin/src/main/java/io/github/kbuntrock/TagLibrary.java @@ -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()) { diff --git a/openapi-maven-plugin/src/main/java/io/github/kbuntrock/model/DataObject.java b/openapi-maven-plugin/src/main/java/io/github/kbuntrock/model/DataObject.java index c779d4c..9449937 100644 --- a/openapi-maven-plugin/src/main/java/io/github/kbuntrock/model/DataObject.java +++ b/openapi-maven-plugin/src/main/java/io/github/kbuntrock/model/DataObject.java @@ -104,7 +104,6 @@ public DataObject(final DataObject dataObject) { this.classRequired = dataObject.classRequired; } - public DataObject(final Type originalType) { Type type = originalType; @@ -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; @@ -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 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 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( @@ -228,6 +215,21 @@ public DataObject(final Type originalType) { } + private void computeMapTypes() { + TypeToken token = TypeToken.of(javaType); + TypeToken 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 superType = token.getSupertype(Collection.class); + Type[] resolvedArguments = ((ParameterizedType) superType.getType()).getActualTypeArguments(); + arrayItemDataObject = new DataObject(resolvedArguments[0]); + } + /** * @return true if this DataObject is a map */ diff --git a/openapi-maven-plugin/src/test/java/io/github/kbuntrock/SpringClassAnalyserTest.java b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/SpringClassAnalyserTest.java index de61ea0..ae52def 100644 --- a/openapi-maven-plugin/src/test/java/io/github/kbuntrock/SpringClassAnalyserTest.java +++ b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/SpringClassAnalyserTest.java @@ -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; @@ -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 { @@ -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()); diff --git a/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/dto/genericity/extendsMap/GenericExtendsList.java b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/dto/genericity/extendsMap/GenericExtendsList.java new file mode 100644 index 0000000..d76101c --- /dev/null +++ b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/dto/genericity/extendsMap/GenericExtendsList.java @@ -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 extends ArrayList { + + public GenericExtendsList doSomething(List rows) { + return this; + } +} diff --git a/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/dto/genericity/extendsMap/GenericExtendsObjectMap.java b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/dto/genericity/extendsMap/GenericExtendsObjectMap.java new file mode 100644 index 0000000..a68ed46 --- /dev/null +++ b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/dto/genericity/extendsMap/GenericExtendsObjectMap.java @@ -0,0 +1,18 @@ +package io.github.kbuntrock.resources.dto.genericity.extendsMap; + +import java.util.HashMap; +import java.util.List; + + +public class GenericExtendsObjectMap extends HashMap { + + public GenericExtendsObjectMap rows(List rows) { + this.put("rows", rows); + return this; + } + + public GenericExtendsObjectMap lines(List lines) { + this.put("lines", lines); + return this; + } +} diff --git a/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/endpoint/generic/ExtendsGenericObjectMap.java b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/endpoint/generic/ExtendsGenericObjectMap.java new file mode 100644 index 0000000..2c6f81d --- /dev/null +++ b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/endpoint/generic/ExtendsGenericObjectMap.java @@ -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 getMap() { + GenericExtendsObjectMap map = new GenericExtendsObjectMap(); + List uuidList = new ArrayList<>(); + map.rows(uuidList); + List longList = new ArrayList<>(); + map.lines(longList); + return map; + } + +} diff --git a/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/endpoint/generic/ExtendsListV2.java b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/endpoint/generic/ExtendsListV2.java new file mode 100644 index 0000000..bb22b05 --- /dev/null +++ b/openapi-maven-plugin/src/test/java/io/github/kbuntrock/resources/endpoint/generic/ExtendsListV2.java @@ -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 getList(); + +} diff --git a/openapi-maven-plugin/src/test/resources/io/github/kbuntrock/SpringClassAnalyserTest.extends_list2.approved.txt b/openapi-maven-plugin/src/test/resources/io/github/kbuntrock/SpringClassAnalyserTest.extends_list2.approved.txt new file mode 100644 index 0000000..8111992 --- /dev/null +++ b/openapi-maven-plugin/src/test/resources/io/github/kbuntrock/SpringClassAnalyserTest.extends_list2.approved.txt @@ -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 diff --git a/openapi-maven-plugin/src/test/resources/io/github/kbuntrock/SpringClassAnalyserTest.generic_object_mapping2.approved.txt b/openapi-maven-plugin/src/test/resources/io/github/kbuntrock/SpringClassAnalyserTest.generic_object_mapping2.approved.txt new file mode 100644 index 0000000..1eb4a37 --- /dev/null +++ b/openapi-maven-plugin/src/test/resources/io/github/kbuntrock/SpringClassAnalyserTest.generic_object_mapping2.approved.txt @@ -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: {}