Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException Multi-level inheritance fixes #41 #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,21 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.maven.surefire>2.22.2</version.maven.surefire>
<version.swagger-parser>1.0.33</version.swagger-parser>
<version.mustache>0.8.18</version.mustache>
<version.junit>3.8.1</version.junit>
<version.net.sourceforge.plantuml>7999</version.net.sourceforge.plantuml>
<junit.jupiter.version>5.4.2</junit.jupiter.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -117,6 +118,10 @@
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.maven.surefire}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
Expand Down
5 changes: 2 additions & 3 deletions swagger2puml-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<version>0.0.1-SNAPSHOT</version>
</parent>

<groupId>io.github.kicksolutions</groupId>
<artifactId>swagger-plantuml-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Core POM to generate Plant UML from Swagger</description>
Expand All @@ -18,8 +17,8 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -727,31 +729,69 @@ private List<ClassMembers> getClassMembers(ArrayModel arrayModel, Map<String, Mo
* @return
*/
private List<ClassMembers> getClassMembers(ComposedModel composedModel, Map<String, Model> modelsMap) {
LOGGER.entering(LOGGER.getName(), "getClassMembers-ComposedModel");
return getClassMembers(composedModel, modelsMap, new HashSet<Model>());
}

List<ClassMembers> classMembers = new ArrayList<ClassMembers>();
/**
* New Overloaded getClassMembers Implementation to handle deeply nested class hierarchies
* @param composedModel
* @param modelsMap
* @param visited
* @return
*/
private List<ClassMembers> getClassMembers(ComposedModel composedModel, Map<String, Model> modelsMap, Set<Model> visited) {
LOGGER.entering(LOGGER.getName(), "getClassMembers-ComposedModel-DeepNest");

List<ClassMembers> classMembers = new ArrayList<ClassMembers>();
Map<String, Property> childProperties = new HashMap<String, Property>();

if (null != composedModel.getChild()) {
childProperties = composedModel.getChild().getProperties();
}

List<ClassMembers> ancestorMembers = new ArrayList<ClassMembers>();
List<Model> allOf = composedModel.getAllOf();
for (Model currentModel : allOf) {

if (currentModel instanceof RefModel) {
RefModel refModel = (RefModel) currentModel;
childProperties.putAll(modelsMap.get(refModel.getSimpleRef()).getProperties());
// This line throws an NPE when encountering deeply nested class hierarchies because it assumes any child
// classes are RefModel and not ComposedModel
// childProperties.putAll(modelsMap.get(refModel.getSimpleRef()).getProperties());

Model parentRefModel = modelsMap.get(refModel.getSimpleRef());

if (parentRefModel.getProperties() != null) {
childProperties.putAll(parentRefModel.getProperties());
}

classMembers = convertModelPropertiesToClassMembers(childProperties,
modelsMap.get(refModel.getSimpleRef()), modelsMap);

// If the parent model also has AllOf references -- meaning it's a child of some other superclass
// then we need to recurse to get the grandparent's properties and add them to our current classes
// derived property list
if (parentRefModel instanceof ComposedModel) {
ComposedModel parentRefComposedModel = (ComposedModel) parentRefModel;
// Use visited to mark which classes we've processed -- this is just to avoid
// an infinite loop in case there's a circular reference in the class hierarchy.
if (!visited.contains(parentRefComposedModel)) {
ancestorMembers = getClassMembers(parentRefComposedModel, modelsMap, visited);
classMembers.addAll(ancestorMembers);
}
}


}
}

LOGGER.exiting(LOGGER.getName(), "getClassMembers-ComposedModel");
visited.add(composedModel);
LOGGER.exiting(LOGGER.getName(), "getClassMembers-ComposedModel-DeepNest");
return classMembers;
}
}




/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.kicksolutions.swagger.plantuml;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.File;

import static org.junit.jupiter.api.Assertions.assertTrue;

class PlantUMLGeneratorTest {

private PlantUMLGenerator generator = new PlantUMLGenerator();

private static final String DEFAULT_PLANT_UML_FILENAME = "swagger.puml";

// TODO - setup - remove the generated files if they exist


@Test
@DisplayName("Basic Petstore test")
void test_generatePetStorePlantUml() {
String specFile = "src/test/resources/petstore/swagger.yaml";
String outputPath = "src/test/resources/petstore";
boolean generateDefinitionModelOnly = false;
boolean includeCardinality = true;
boolean generateSvg = false; // TODO - this is to generate the svg image file but it doesn't work correctly as a
// test

generator.transformSwagger2Puml(specFile, outputPath, generateDefinitionModelOnly, includeCardinality, generateSvg);

assertTrue(new File(outputPath + "/" + DEFAULT_PLANT_UML_FILENAME).exists(), "Expect PlantUML file to be generated");
}


@Test
@DisplayName("Petstore test with inheritance")
void test_generatePetStorePlantUml_withInheritance() {
String specFile = "src/test/resources/petstore_with_inheritance/swagger.yaml";
String outputPath = "src/test/resources/petstore_with_inheritance";
boolean generateDefinitionModelOnly = false;
boolean includeCardinality = true;
boolean generateSvg = false; // TODO - this is to generate the svg image file but it doesn't work correctly as a
// test

generator.transformSwagger2Puml(specFile, outputPath, generateDefinitionModelOnly, includeCardinality, generateSvg);

assertTrue(new File(outputPath + "/" + DEFAULT_PLANT_UML_FILENAME).exists(), "Expect PlantUML file to be generated");
}

}
Loading