forked from loafle/openapi-generator-original
Merge pull request #467 from cor3ntin/security_upstream
Expose Security schemes of operations.
This commit is contained in:
commit
dce9a81536
@ -1,6 +1,7 @@
|
||||
package com.wordnik.swagger.codegen;
|
||||
|
||||
import com.wordnik.swagger.models.*;
|
||||
import com.wordnik.swagger.models.auth.SecuritySchemeDefinition;
|
||||
import com.wordnik.swagger.models.properties.*;
|
||||
|
||||
import java.util.*;
|
||||
@ -35,6 +36,8 @@ public interface CodegenConfig {
|
||||
|
||||
CodegenModel fromModel(String name, Model model);
|
||||
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation);
|
||||
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);
|
||||
|
||||
Set<String> defaultIncludes();
|
||||
Map<String, String> typeMapping();
|
||||
Map<String, String> instantiationTypes();
|
||||
|
@ -6,7 +6,8 @@ public enum CodegenModelType {
|
||||
OPERATION(CodegenOperation.class),
|
||||
PARAMETER(CodegenParameter.class),
|
||||
PROPERTY(CodegenProperty.class),
|
||||
RESPONSE(CodegenResponse.class);
|
||||
RESPONSE(CodegenResponse.class),
|
||||
SECURITY(CodegenSecurity.class);
|
||||
|
||||
private final Class<?> defaultImplementation;
|
||||
|
||||
|
@ -18,6 +18,7 @@ public class CodegenOperation {
|
||||
public List<CodegenParameter> queryParams = new ArrayList<CodegenParameter>();
|
||||
public List<CodegenParameter> headerParams = new ArrayList<CodegenParameter>();
|
||||
public List<CodegenParameter> formParams = new ArrayList<CodegenParameter>();
|
||||
public List<CodegenSecurity> authMethods;
|
||||
public List<String> tags;
|
||||
public List<CodegenResponse> responses = new ArrayList<CodegenResponse>();
|
||||
public final List<CodegenProperty> responseHeaders = new ArrayList<CodegenProperty>();
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.wordnik.swagger.codegen;
|
||||
|
||||
public class CodegenSecurity {
|
||||
String name;
|
||||
String type;
|
||||
Boolean hasMore;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.wordnik.swagger.codegen;
|
||||
|
||||
import com.wordnik.swagger.models.*;
|
||||
import com.wordnik.swagger.models.auth.SecuritySchemeDefinition;
|
||||
import com.wordnik.swagger.models.parameters.*;
|
||||
import com.wordnik.swagger.models.properties.*;
|
||||
import com.wordnik.swagger.util.Json;
|
||||
@ -880,6 +881,23 @@ public class DefaultCodegen {
|
||||
return p;
|
||||
}
|
||||
|
||||
public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes) {
|
||||
if(schemes == null)
|
||||
return null;
|
||||
|
||||
List<CodegenSecurity> secs = new ArrayList<CodegenSecurity>();
|
||||
for(Iterator entries = schemes.entrySet().iterator(); entries.hasNext(); ) {
|
||||
Map.Entry<String, SecuritySchemeDefinition> entry = (Map.Entry<String, SecuritySchemeDefinition>) entries.next();
|
||||
|
||||
CodegenSecurity sec = CodegenModelFactory.newInstance(CodegenModelType.SECURITY);
|
||||
sec.name = entry.getKey();
|
||||
sec.type = entry.getValue().getType();
|
||||
sec.hasMore = entries.hasNext();
|
||||
secs.add(sec);
|
||||
}
|
||||
return secs;
|
||||
}
|
||||
|
||||
protected List<Map<String, String>> toExamples(Map<String, String> examples) {
|
||||
if(examples == null)
|
||||
return null;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.wordnik.swagger.codegen;
|
||||
|
||||
import com.wordnik.swagger.models.*;
|
||||
import com.wordnik.swagger.models.auth.SecuritySchemeDefinition;
|
||||
import com.wordnik.swagger.util.*;
|
||||
import com.samskivert.mustache.*;
|
||||
|
||||
@ -227,6 +228,18 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
return ops;
|
||||
}
|
||||
|
||||
public SecuritySchemeDefinition fromSecurity(String name) {
|
||||
Map<String, SecuritySchemeDefinition> map = swagger.getSecurityDefinitions();
|
||||
if(map == null)
|
||||
return null;
|
||||
SecuritySchemeDefinition scheme = map.get(name);
|
||||
if(scheme == null)
|
||||
return null;
|
||||
return scheme;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void processOperation(String resourcePath, String httpMethod, Operation operation, Map<String, List<CodegenOperation>> operations) {
|
||||
if(operation != null) {
|
||||
List<String> tags = operation.getTags();
|
||||
@ -239,8 +252,25 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
CodegenOperation co = config.fromOperation(resourcePath, httpMethod, operation);
|
||||
co.tags = new ArrayList<String>();
|
||||
co.tags.add(sanitizeTag(tag));
|
||||
|
||||
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);
|
||||
|
||||
List<Map<String, List<String>>> securities = operation.getSecurity();
|
||||
if(securities == null)
|
||||
continue;
|
||||
Map<String, SecuritySchemeDefinition> authMethods = new HashMap<String, SecuritySchemeDefinition>();
|
||||
for (Map<String, List<String>> security : securities) {
|
||||
if (security.size() != 1) {
|
||||
//Not sure what to do
|
||||
continue;
|
||||
}
|
||||
String securityName = security.keySet().iterator().next();
|
||||
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
|
||||
if(securityDefinition != null)
|
||||
authMethods.put(securityName, securityDefinition);
|
||||
}
|
||||
if(!authMethods.isEmpty()) {
|
||||
co.authMethods = config.fromSecurity(authMethods);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user