diff --git a/typescript-service-generator-core/src/main/java/com/palantir/code/ts/generator/ServiceEmitter.java b/typescript-service-generator-core/src/main/java/com/palantir/code/ts/generator/ServiceEmitter.java index 3448f3b..bc1a917 100644 --- a/typescript-service-generator-core/src/main/java/com/palantir/code/ts/generator/ServiceEmitter.java +++ b/typescript-service-generator-core/src/main/java/com/palantir/code/ts/generator/ServiceEmitter.java @@ -52,11 +52,12 @@ public ServiceEmitter(ServiceModel model, TypescriptServiceGeneratorConfiguratio this.writer = writer; } - public void emitTypescriptTypes(TypescriptServiceGeneratorConfiguration settings) { + public void emitTypescriptTypes(TypescriptServiceGeneratorConfiguration settings, List additionalTypesToOutput) { Settings settingsToUse = settings.getSettings(); TypeProcessor baseTypeProcessor = settingsToUse.customTypeProcessor; - Set referencedTypes = model.referencedTypes(); + Set referencedTypes = Sets.newHashSet(model.referencedTypes().iterator()); + referencedTypes.addAll(additionalTypesToOutput); Set> referencedClasses = getReferencedClasses(referencedTypes, settings); final Set discoveredTypes = Sets.newHashSet(referencedClasses.iterator()); referencedClasses = filterInputClasses(referencedClasses); diff --git a/typescript-service-generator-core/src/main/java/com/palantir/code/ts/generator/ServiceGenerator.java b/typescript-service-generator-core/src/main/java/com/palantir/code/ts/generator/ServiceGenerator.java index 9cb2019..78113a2 100644 --- a/typescript-service-generator-core/src/main/java/com/palantir/code/ts/generator/ServiceGenerator.java +++ b/typescript-service-generator-core/src/main/java/com/palantir/code/ts/generator/ServiceGenerator.java @@ -9,6 +9,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.lang.reflect.Type; import java.util.List; import org.apache.commons.io.IOUtils; @@ -51,6 +52,10 @@ public ServiceGenerator(TypescriptServiceGeneratorConfiguration settings) { } public void generateTypescriptService(Class clazz) { + this.generateTypescriptService(clazz, Lists.newArrayList()); + } + + public void generateTypescriptService(Class clazz, List additionalClassesToOutput) { OutputStream output = null; try { output = new FileOutputStream(new File(settings.generatedFolderLocation(), Character.toLowerCase(clazz.getSimpleName().charAt(0)) + clazz.getSimpleName().substring(1) + ".ts")); @@ -62,7 +67,7 @@ public void generateTypescriptService(Class clazz) { ServiceModel serviceModel = new ServiceClassParser().parseServiceClass(clazz, settings); ServiceEmitter serviceEndpointEmitter = new ServiceEmitter(serviceModel, settings, writer); - serviceEndpointEmitter.emitTypescriptTypes(settings); + serviceEndpointEmitter.emitTypescriptTypes(settings, additionalClassesToOutput); serviceEndpointEmitter.emitTypescriptInterface(); serviceEndpointEmitter.emitTypescriptClass(); diff --git a/typescript-service-generator-core/src/test/java/com/palantir/code/ts/generator/ServiceEmitterTest.java b/typescript-service-generator-core/src/test/java/com/palantir/code/ts/generator/ServiceEmitterTest.java index fe3b785..e671020 100644 --- a/typescript-service-generator-core/src/test/java/com/palantir/code/ts/generator/ServiceEmitterTest.java +++ b/typescript-service-generator-core/src/test/java/com/palantir/code/ts/generator/ServiceEmitterTest.java @@ -12,10 +12,13 @@ import org.junit.Before; import org.junit.Test; +import com.google.common.collect.Lists; import com.palantir.code.ts.generator.model.ServiceModel; import com.palantir.code.ts.generator.utils.TestUtils.ConcreteObjectService; import com.palantir.code.ts.generator.utils.TestUtils.DuplicateMethodNamesService; +import com.palantir.code.ts.generator.utils.TestUtils.MyObject; import com.palantir.code.ts.generator.utils.TestUtils.TestComplexServiceClass; +import com.palantir.code.ts.generator.utils.TestUtils.TestServiceClass; public class ServiceEmitterTest { @@ -45,7 +48,7 @@ public void before() { public void testComplexServiceClassEmitTypes() { ServiceModel model = serviceClassParser.parseServiceClass(TestComplexServiceClass.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); - serviceEmitter.emitTypescriptTypes(settings); + serviceEmitter.emitTypescriptTypes(settings, Lists.newArrayList()); writer.close(); String expectedOutput = "" + "\n" + @@ -84,7 +87,7 @@ public void testComplexServiceClassEmitInterface() { } @Test - public void testComplexServicClassEmitClass() { + public void testComplexServiceClassEmitClass() { ServiceModel model = serviceClassParser.parseServiceClass(TestComplexServiceClass.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptClass(); @@ -187,29 +190,42 @@ public void testConcreteObjectService() { serviceEmitter.emitTypescriptClass(); writer.close(); String expectedOutput = "\n" + -"export class ConcreteObjectServiceImpl implements ConcreteObjectService {\n" + -"\n" + -" private httpApiBridge: HttpApiBridge;\n" + -" constructor(httpApiBridge: HttpApiBridge) {\n" + -" this.httpApiBridge = httpApiBridge;\n" + -" }\n" + -"\n" + -" public noPathGetter() {\n" + -" var httpCallData = {\n" + -" serviceIdentifier: \"concreteObjectService\",\n" + -" endpointPath: \"concreteObject\",\n" + -" endpointName: \"noPathGetter\",\n" + -" method: \"GET\",\n" + -" mediaType: \"application/json\",\n" + -" requiredHeaders: [],\n" + -" pathArguments: [],\n" + -" queryArguments: {\n" + -" },\n" + -" data: null\n" + -" };\n" + -" return this.httpApiBridge.callEndpoint(httpCallData);\n" + -" }\n" + +"export class ConcreteObjectServiceImpl implements ConcreteObjectService {\n" + +"\n" + +" private httpApiBridge: HttpApiBridge;\n" + +" constructor(httpApiBridge: HttpApiBridge) {\n" + +" this.httpApiBridge = httpApiBridge;\n" + +" }\n" + +"\n" + +" public noPathGetter() {\n" + +" var httpCallData = {\n" + +" serviceIdentifier: \"concreteObjectService\",\n" + +" endpointPath: \"concreteObject\",\n" + +" endpointName: \"noPathGetter\",\n" + +" method: \"GET\",\n" + +" mediaType: \"application/json\",\n" + +" requiredHeaders: [],\n" + +" pathArguments: [],\n" + +" queryArguments: {\n" + +" },\n" + +" data: null\n" + +" };\n" + +" return this.httpApiBridge.callEndpoint(httpCallData);\n" + +" }\n" + "}\n"; assertEquals(expectedOutput, new String(stream.toByteArray())); } + + @Test + public void testAdditionalClassesToOutput() { + ServiceModel model = serviceClassParser.parseServiceClass(TestServiceClass.class, settings); + ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); + serviceEmitter.emitTypescriptTypes(settings, Lists.newArrayList(MyObject.class)); + writer.close(); + String expectedOutput = "\n" + +" export interface MyObject {\n" + +" y: MyObject;\n" + +" }\n"; + assertEquals(expectedOutput, new String(stream.toByteArray())); + } }