Skip to content

Commit

Permalink
Allow to set values with setApiPackage(..) and setModelPackage(..) (#…
Browse files Browse the repository at this point in the history
…8013)

* Add getInvokerPackage() getter

* Add test cases

* Handle values set with setModelPackage(..) and setApiPackage(..)
  • Loading branch information
jmini authored and wing328 committed Apr 13, 2018
1 parent 2034f61 commit 006f084
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ public void processOpts() {
additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
}

if (!additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
}

if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);
}

if (additionalProperties.containsKey(CodegenConstants.GROUP_ID)) {
this.setGroupId((String) additionalProperties.get(CodegenConstants.GROUP_ID));
} else {
Expand Down Expand Up @@ -1125,6 +1133,10 @@ private static String sanitizePackageName(String packageName) {
return packageName;
}

public String getInvokerPackage() {
return invokerPackage;
}

public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ public String getSwaggerType(Property p) {
return toModelName(type);
}

public String getInvokerPackage() {
return invokerPackage;
}

public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ public void processOpts() {
additionalProperties.put("authPackage", authPackage);
}

if (!additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
}

if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);
}

if (additionalProperties.containsKey(CodegenConstants.GROUP_ID)) {
this.setGroupId((String) additionalProperties.get(CodegenConstants.GROUP_ID));
} else {
Expand Down Expand Up @@ -549,6 +557,10 @@ public void setAndroidBuildToolsVersion(String androidBuildToolsVersion) {
this.androidBuildToolsVersion = androidBuildToolsVersion;
}

public String getInvokerPackage() {
return invokerPackage;
}

public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.SupportingFile;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.ArrayModel;
import io.swagger.models.Info;
import io.swagger.models.License;
Expand Down Expand Up @@ -391,6 +391,10 @@ public String modelFileFolder() {
return createPath(outputFolder, sourceFolder, invokerPackage, modelPackage());
}

public String getInvokerPackage() {
return invokerPackage;
}

public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ public String getSwaggerType(Property p) {
return toModelName(type);
}

public String getInvokerPackage() {
return invokerPackage;
}

public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.swagger.codegen.android;

import org.testng.Assert;
import org.testng.annotations.Test;

import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.languages.AndroidClientCodegen;

public class AndroidClientCodegenTest {

@Test
public void testInitialConfigValues() throws Exception {
final AndroidClientCodegen codegen = new AndroidClientCodegen();
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
Assert.assertEquals(codegen.modelPackage(), "io.swagger.client.model");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "io.swagger.client.model");
Assert.assertEquals(codegen.apiPackage(), "io.swagger.client.api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "io.swagger.client.api");
Assert.assertEquals(codegen.getInvokerPackage(), "io.swagger.client");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "io.swagger.client");
}

@Test
public void testSettersForConfigValues() throws Exception {
final AndroidClientCodegen codegen = new AndroidClientCodegen();
codegen.setModelPackage("xx.yyyyyyyy.model");
codegen.setApiPackage("xx.yyyyyyyy.api");
codegen.setInvokerPackage("xx.yyyyyyyy.invoker");
codegen.processOpts();

Assert.assertEquals(codegen.modelPackage(), "xx.yyyyyyyy.model");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xx.yyyyyyyy.model");
Assert.assertEquals(codegen.apiPackage(), "xx.yyyyyyyy.api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xx.yyyyyyyy.api");
Assert.assertEquals(codegen.getInvokerPackage(), "xx.yyyyyyyy.invoker");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xx.yyyyyyyy.invoker");
}

@Test
public void testAdditionalPropertiesPutForConfigValues() throws Exception {
final AndroidClientCodegen codegen = new AndroidClientCodegen();
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.yyyyy.mmmmm.model");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.yyyyy.aaaaa.api");
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE,"xyz.yyyyy.iiii.invoker");
codegen.processOpts();

Assert.assertEquals(codegen.modelPackage(), "xyz.yyyyy.mmmmm.model");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xyz.yyyyy.mmmmm.model");
Assert.assertEquals(codegen.apiPackage(), "xyz.yyyyy.aaaaa.api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xyz.yyyyy.aaaaa.api");
Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.iiii.invoker");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xyz.yyyyy.iiii.invoker");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,50 @@ public void testInitialConfigValues() throws Exception {

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
Assert.assertEquals(codegen.modelPackage(), "invalidPackageName");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "invalidPackageName");
Assert.assertEquals(codegen.apiPackage(), "invalidPackageName");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "invalidPackageName");
Assert.assertEquals(codegen.getInvokerPackage(), "io.swagger");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "io.swagger");
}

@Test
public void testSettersForConfigValues() throws Exception {
final AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.setHideGenerationTimestamp(true);
codegen.setModelPackage("xyz.yyyyy.zzzzzzz.model");
codegen.setApiPackage("xyz.yyyyy.zzzzzzz.api");
codegen.setInvokerPackage("xyz.yyyyy.zzzzzzz.invoker");
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
Assert.assertEquals(codegen.modelPackage(), "xyz.yyyyy.zzzzzzz.model");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xyz.yyyyy.zzzzzzz.model");
Assert.assertEquals(codegen.apiPackage(), "xyz.yyyyy.zzzzzzz.api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xyz.yyyyy.zzzzzzz.api");
Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.zzzzzzz.invoker");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xyz.yyyyy.zzzzzzz.invoker");
}

@Test
public void testAdditionalPropertiesPutForConfigValues() throws Exception {
final AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.additionalProperties().put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, false);
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.yyyyy.model.oooooo");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.yyyyy.api.oooooo");
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE, "xyz.yyyyy.invoker.oooooo");
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
Assert.assertEquals(codegen.modelPackage(), "xyz.yyyyy.model.oooooo");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xyz.yyyyy.model.oooooo");
Assert.assertEquals(codegen.apiPackage(), "xyz.yyyyy.api.oooooo");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xyz.yyyyy.api.oooooo");
Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.invoker.oooooo");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xyz.yyyyy.invoker.oooooo");
}

private static class P_AbstractJavaCodegen extends AbstractJavaCodegen {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.swagger.codegen.java.jaxrs;

import org.testng.Assert;
import org.testng.annotations.Test;

import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.languages.AbstractJavaJAXRSServerCodegen;

public class AbstractJavaJAXRSServerCodegenTest {

private final AbstractJavaJAXRSServerCodegen fakeJavaJAXRSCodegen = new P_AbstractJavaJAXRSServerCodegen();

@Test
public void convertApiName() throws Exception {
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("name"), "NameApi");
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("$name"), "NameApi");
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("nam#e"), "NameApi");
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("$another-fake?"), "AnotherFakeApi");
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("fake_classname_tags 123#$%^"), "FakeClassnameTags123Api");
}

@Test
public void testInitialConfigValues() throws Exception {
final AbstractJavaJAXRSServerCodegen codegen = new P_AbstractJavaJAXRSServerCodegen();
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
Assert.assertEquals(codegen.modelPackage(), "io.swagger.model");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "io.swagger.model");
Assert.assertEquals(codegen.apiPackage(), "io.swagger.api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "io.swagger.api");
Assert.assertEquals(codegen.getInvokerPackage(), "io.swagger.api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "io.swagger.api");
}

@Test
public void testSettersForConfigValues() throws Exception {
final AbstractJavaJAXRSServerCodegen codegen = new P_AbstractJavaJAXRSServerCodegen();
codegen.setHideGenerationTimestamp(true);
codegen.setModelPackage("xx.yyyyyyyy.model");
codegen.setApiPackage("xx.yyyyyyyy.api");
codegen.setInvokerPackage("xx.yyyyyyyy.invoker");
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
Assert.assertEquals(codegen.modelPackage(), "xx.yyyyyyyy.model");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xx.yyyyyyyy.model");
Assert.assertEquals(codegen.apiPackage(), "xx.yyyyyyyy.api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xx.yyyyyyyy.api");
Assert.assertEquals(codegen.getInvokerPackage(), "xx.yyyyyyyy.invoker");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xx.yyyyyyyy.invoker");
}

@Test
public void testAdditionalPropertiesPutForConfigValues() throws Exception {
final AbstractJavaJAXRSServerCodegen codegen = new P_AbstractJavaJAXRSServerCodegen();
codegen.additionalProperties().put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true");
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.yyyyy.mmmmm.model");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.yyyyy.aaaaa.api");
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE,"xyz.yyyyy.iiii.invoker");
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
Assert.assertEquals(codegen.modelPackage(), "xyz.yyyyy.mmmmm.model");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xyz.yyyyy.mmmmm.model");
Assert.assertEquals(codegen.apiPackage(), "xyz.yyyyy.aaaaa.api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xyz.yyyyy.aaaaa.api");
Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.iiii.invoker");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xyz.yyyyy.iiii.invoker");
}

private static class P_AbstractJavaJAXRSServerCodegen extends AbstractJavaJAXRSServerCodegen {

@Override
public CodegenType getTag() {
return null;
}

@Override
public String getName() {
return null;
}

@Override
public String getHelp() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public void testInitialConfigValues() throws Exception {

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
Assert.assertEquals(codegen.modelPackage(), "model");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), null);
Assert.assertEquals(codegen.apiPackage(), "api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), null);
Assert.assertEquals(codegen.getInvokerPackage(), null);
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), null);
}

@Test
Expand Down
Loading

0 comments on commit 006f084

Please sign in to comment.