Introduced a factory for codegen data containers

This enables users to extend the Codegen* classes
and add their own methods there (which then can
be accessed from the mustache templates)
This commit is contained in:
Martin Ehrnhoefer 2014-12-06 16:07:45 +01:00
parent f3ac9a579f
commit f4424a5eae
3 changed files with 57 additions and 4 deletions

View File

@ -0,0 +1,37 @@
package com.wordnik.swagger.codegen;
import java.util.HashMap;
import java.util.Map;
public final class CodegenModelFactory {
private static final Map<CodegenModelType, Class<?>> typeMapping = new HashMap<CodegenModelType, Class<?>>();
/**
* Configure a different implementation class.
* @param type the type that shall be replaced
* @param implementation the implementation class must extend the default class and must provide a public no-arg constructor
*/
public static void setTypeMapping(CodegenModelType type, Class<?> implementation) {
if (!type.getDefaultImplementation().isAssignableFrom(implementation)) {
throw new IllegalArgumentException(implementation.getSimpleName() + " doesn't extend " + type.getDefaultImplementation().getSimpleName());
}
try {
implementation.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
typeMapping.put(type, implementation);
}
public static <T> T newInstance(CodegenModelType type) {
@SuppressWarnings("unchecked") Class<T> classType = (Class<T>) typeMapping.get(type);
try {
return classType.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,17 @@
package com.wordnik.swagger.codegen;
public enum CodegenModelType {
PARAMETER(CodegenParameter.class),
OPERATION(CodegenOperation.class);
private final Class<?> defaultImplementation;
private CodegenModelType(Class<?> defaultImplementation) {
this.defaultImplementation = defaultImplementation;
}
public Class<?> getDefaultImplementation() {
return defaultImplementation;
}
}

View File

@ -1,13 +1,12 @@
package com.wordnik.swagger.codegen;
import com.wordnik.swagger.util.Json;
import com.wordnik.swagger.models.*;
import com.wordnik.swagger.models.parameters.*;
import com.wordnik.swagger.models.properties.*;
import com.wordnik.swagger.util.Json;
import org.apache.commons.lang.StringUtils;
import java.util.*;
import java.io.File;
public class DefaultCodegen {
protected String outputFolder = "";
@ -513,7 +512,7 @@ public class DefaultCodegen {
}
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation){
CodegenOperation op = new CodegenOperation();
CodegenOperation op = CodegenModelFactory.newInstance(CodegenModelType.OPERATION);
Set<String> imports = new HashSet<String>();
String operationId = operation.getOperationId();
@ -656,7 +655,7 @@ public class DefaultCodegen {
if(parameters != null) {
for(Parameter param : parameters) {
CodegenParameter p = new CodegenParameter();
CodegenParameter p = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
p.baseName = param.getName();
p.description = param.getDescription();
p.required = param.getRequired();