forked from loafle/openapi-generator-original
added support for headers when generating per #102
This commit is contained in:
parent
785cc4e29a
commit
dd971f19e7
@ -19,10 +19,16 @@ package com.wordnik.swagger.codegen;
|
||||
import com.wordnik.swagger.codegen.ClientOpts;
|
||||
import com.wordnik.swagger.annotations.*;
|
||||
import com.wordnik.swagger.models.Swagger;
|
||||
import com.wordnik.swagger.models.auth.AuthorizationValue;
|
||||
|
||||
import java.util.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
public class ClientOptInput {
|
||||
private ClientOpts opts;
|
||||
private Swagger swagger;
|
||||
private List<AuthorizationValue> auths;
|
||||
protected CodegenConfig config;
|
||||
|
||||
public ClientOptInput swagger(Swagger swagger) {
|
||||
@ -34,6 +40,44 @@ public class ClientOptInput {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setAuth(String urlEncodedAuthString) {
|
||||
List<AuthorizationValue> auths = new ArrayList<AuthorizationValue>();
|
||||
if(urlEncodedAuthString != null && !"".equals(urlEncodedAuthString)) {
|
||||
String[] parts = urlEncodedAuthString.split(",");
|
||||
for(String part : parts) {
|
||||
String[] kvPair = part.split(":");
|
||||
if(kvPair.length == 2) {
|
||||
auths.add(new AuthorizationValue(URLDecoder.decode(kvPair[0]), URLDecoder.decode(kvPair[1]), "header"));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.auths = auths;
|
||||
}
|
||||
public String getAuth() {
|
||||
if(auths != null) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
for(AuthorizationValue v : auths) {
|
||||
try {
|
||||
if(b.toString().length() > 0)
|
||||
b.append(",");
|
||||
b.append(URLEncoder.encode(v.getKeyName(), "UTF-8"))
|
||||
.append(":")
|
||||
.append(URLEncoder.encode(v.getValue(), "UTF-8"));
|
||||
}
|
||||
catch (Exception e) {
|
||||
// continue
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return b.toString();
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
public List<AuthorizationValue> getAuthorizationValues() {
|
||||
return auths;
|
||||
}
|
||||
|
||||
public CodegenConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.wordnik.swagger.codegen;
|
||||
|
||||
import com.wordnik.swagger.codegen.languages.*;
|
||||
import com.wordnik.swagger.models.Swagger;
|
||||
import com.wordnik.swagger.models.auth.AuthorizationValue;
|
||||
import com.wordnik.swagger.util.*;
|
||||
|
||||
import io.swagger.parser.SwaggerParser;
|
||||
@ -43,6 +44,7 @@ public class Codegen extends DefaultGenerator {
|
||||
options.addOption("i", "input-spec", true, "location of the swagger spec, as URL or file");
|
||||
options.addOption("t", "template-dir", true, "folder containing the template files");
|
||||
options.addOption("d", "debug-info", false, "prints additional info for debugging");
|
||||
options.addOption("a", "auth", false, "addes authorization headers when fetching the swagger definitions remotely. Pass in a URL-encoded string of name:header with a comma separating multiple values");
|
||||
|
||||
ClientOptInput clientOptInput = new ClientOptInput();
|
||||
ClientOpts clientOpts = new ClientOpts();
|
||||
@ -59,6 +61,8 @@ public class Codegen extends DefaultGenerator {
|
||||
System.out.println(debugInfoOptions);
|
||||
return;
|
||||
}
|
||||
if (cmd.hasOption("a"))
|
||||
clientOptInput.setAuth(cmd.getOptionValue("a"));
|
||||
if (cmd.hasOption("l"))
|
||||
clientOptInput.setConfig(getConfig(cmd.getOptionValue("l")));
|
||||
if (cmd.hasOption("o"))
|
||||
@ -76,7 +80,7 @@ public class Codegen extends DefaultGenerator {
|
||||
return;
|
||||
}
|
||||
if (cmd.hasOption("i"))
|
||||
swagger = new SwaggerParser().read(cmd.getOptionValue("i"));
|
||||
swagger = new SwaggerParser().read(cmd.getOptionValue("i"), clientOptInput.getAuthorizationValues());
|
||||
if (cmd.hasOption("t"))
|
||||
clientOpts.getProperties().put("templateDir", String.valueOf(cmd.getOptionValue("t")));
|
||||
}
|
||||
|
@ -333,7 +333,6 @@ public class DefaultCodegen {
|
||||
|
||||
public CodegenModel fromModel(String name, Model model) {
|
||||
CodegenModel m = CodegenModelFactory.newInstance(CodegenModelType.MODEL);
|
||||
System.out.println("created " + m.getClass());
|
||||
if(reservedWords.contains(name))
|
||||
m.name = escapeReservedWord(name);
|
||||
else
|
||||
|
@ -0,0 +1,44 @@
|
||||
import com.wordnik.swagger.codegen._
|
||||
|
||||
import com.wordnik.swagger.models.auth.AuthorizationValue
|
||||
|
||||
import org.junit.runner.RunWith
|
||||
import org.scalatest.junit.JUnitRunner
|
||||
import org.scalatest.FlatSpec
|
||||
import org.scalatest.Matchers
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
@RunWith(classOf[JUnitRunner])
|
||||
class ClientAuthInputTest extends FlatSpec with Matchers {
|
||||
behavior of "ClientAuthInput"
|
||||
|
||||
it should "read a file upload param from a 2.0 spec" in {
|
||||
val input = new ClientOptInput()
|
||||
|
||||
val header = "api_key:special-key,api_key:hello,X-AUTHORIZATION:0e6c11d79a,Authorization:Basic 1jz0"
|
||||
input.setAuth(header)
|
||||
val authValues = input.getAuthorizationValues()
|
||||
authValues.size() should be (4)
|
||||
|
||||
val a1 = authValues.get(0)
|
||||
a1.getKeyName should be ("api_key")
|
||||
a1.getValue should be ("special-key")
|
||||
a1.getType should be ("header")
|
||||
|
||||
val a2 = authValues.get(1)
|
||||
a2.getKeyName should be ("api_key")
|
||||
a2.getValue should be ("hello")
|
||||
a2.getType should be ("header")
|
||||
|
||||
val a3 = authValues.get(2)
|
||||
a3.getKeyName should be ("X-AUTHORIZATION")
|
||||
a3.getValue should be ("0e6c11d79a")
|
||||
a3.getType should be ("header")
|
||||
|
||||
val a4 = authValues.get(3)
|
||||
a4.getKeyName should be ("Authorization")
|
||||
a4.getValue should be ("Basic 1jz0")
|
||||
a4.getType should be ("header")
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user