Added support for range and list allowable values

This commit is contained in:
rpidikiti 2011-10-19 16:03:25 -07:00
parent 60c577a283
commit fc18c1c94e
6 changed files with 227 additions and 37 deletions

View File

@ -251,7 +251,7 @@ public class LibraryCodeGenerator {
if(operation.getParameters() != null){ if(operation.getParameters() != null){
for(ModelField operationParam : operation.getParameters()){ for(ModelField operationParam : operation.getParameters()){
//skipping the case where there is just one item - TODO process case of allowableValue like '0 to 1000' //skipping the case where there is just one item - TODO process case of allowableValue like '0 to 1000'
if(operationParam.getAllowableValues() != null && operationParam.getAllowableValues().size() > 1) { if(operationParam.getAllowableValues() != null && operationParam.getAllowableValues().getClass().isAssignableFrom(AllowableListValues.class)) {
if(!generatedEnums.contains(operationParam.getName())){ if(!generatedEnums.contains(operationParam.getName())){
//generate enum //generate enum
template = templateGroup.getInstanceOf(ENUM_OBJECT_TEMPLATE); template = templateGroup.getInstanceOf(ENUM_OBJECT_TEMPLATE);
@ -261,7 +261,7 @@ public class LibraryCodeGenerator {
template.setAttribute("className", enumName); template.setAttribute("className", enumName);
template.setAttribute("description", operationParam.getDescription()); template.setAttribute("description", operationParam.getDescription());
template.setAttribute("enumValueType", this.getDataTypeMappingProvider().getClassType(operationParam.getDataType(), true)); template.setAttribute("enumValueType", this.getDataTypeMappingProvider().getClassType(operationParam.getDataType(), true));
for (String allowableValue : operationParam.getAllowableValues()) { for (String allowableValue : ((AllowableListValues)operationParam.getAllowableValues()).getValues()) {
if(operationParam.getDataType().equalsIgnoreCase("string")){ if(operationParam.getDataType().equalsIgnoreCase("string")){
valuePrefix = valueSuffix = "\""; valuePrefix = valueSuffix = "\"";
} }

View File

@ -0,0 +1,59 @@
/**
* Copyright 2011 Wordnik, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wordnik.swagger.codegen.resource;
import java.util.List;
/**
* User: ramesh
* Date: 10/18/11
* Time: 11:34 PM
*/
public class AllowableListValues extends AllowableValues {
private String valueType = "LIST";
private List<String> values;
public String getValueType() {
return valueType;
}
public void setValueType(String valueType) {
this.valueType = valueType;
}
public List<String> getValues() {
return values;
}
public void setValues(List<String> values) {
this.values = values;
}
@Override
public String toString() {
StringBuffer buffer = new StringBuffer();
if(this.getValues() != null){
for(String value : values ){
buffer.append(value);
buffer.append(",");
}
}
return buffer.toString().substring(0, buffer.toString().length()-1);
}
}

View File

@ -0,0 +1,75 @@
/**
* Copyright 2011 Wordnik, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wordnik.swagger.codegen.resource;
/**
* User: ramesh
* Date: 10/18/11
* Time: 11:35 PM
*/
public class AllowableRangeValues extends AllowableValues {
private String valueType = "RANGE";
private boolean inclusive = true;
private String min;
private String max;
public String getValueType() {
return valueType;
}
public void setValueType(String valueType) {
this.valueType = valueType;
}
public String getMin() {
return min;
}
public void setMin(String minValue) {
this.min = minValue;
}
public String getMax() {
return max;
}
public void setMax(String maxValue) {
this.max = maxValue;
}
public boolean isInclusive() {
return inclusive;
}
public void setInclusive(boolean inclusive) {
this.inclusive = inclusive;
}
@Override
public String toString() {
if(inclusive){
return "range(" + min + "," + max + ")";
}else{
return "rangeExclusive(" + min + "," + max + ")";
}
}
}

View File

@ -0,0 +1,73 @@
/**
* Copyright 2011 Wordnik, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wordnik.swagger.codegen.resource;
import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import java.util.ArrayList;
import java.util.List;
/**
* Common interface between range and list allowable values
* User: ramesh
* Date: 10/18/11
* Time: 11:34 PM
*/
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "valueType")
@JsonSubTypes({
@JsonSubTypes.Type(value = AllowableListValues.class, name = "LIST"),
@JsonSubTypes.Type(value = AllowableRangeValues.class, name = "RANGE")
})
public abstract class AllowableValues {
public abstract String getValueType();
public static AllowableValues ConvertAllowableValuesStringToObject(String data) {
if(data != null){
if(data.toLowerCase().startsWith("range")){
AllowableRangeValues av = new AllowableRangeValues();
String[] values = null;
if(data.toLowerCase().startsWith("rangeexclusive")){
values = data.substring(15, data.length()-1).split(",");
av.setInclusive(false);
}else{
values = data.substring(6, data.length()-1).split(",");
av.setInclusive(true);
}
av.setMin(values[0]);
av.setMin(values[1]);
return av;
}else{
List<String> allowedValues = new ArrayList<String>();
if (data != null) {
String[] tokens = data.split(",");
for(String token: tokens){
allowedValues.add(token);
}
}
AllowableListValues av = new AllowableListValues();
av.setValues(allowedValues);
return av;
}
}
return null;
}
}

View File

@ -35,15 +35,14 @@ import java.util.Map;
"items", "items",
"description", "description",
"name", "name",
"enum", "allowableValues",
"properties", "properties",
"required", "required",
"notes", "notes",
"access", "access",
"type" "type"
}) })
public class ApiPropertyDefn implements Serializable public class ApiPropertyDefn implements Serializable {
{
@JsonProperty("id") @JsonProperty("id")
private String id; private String id;
@ -55,8 +54,8 @@ public class ApiPropertyDefn implements Serializable
private String description; private String description;
@JsonProperty("name") @JsonProperty("name")
private String name; private String name;
@JsonProperty("enum") @JsonProperty("allowableValues")
private List<Object> possibleValues = new ArrayList<Object>(); private AllowableValues allowableValues = null;
@JsonProperty("properties") @JsonProperty("properties")
private ApiPropertyListWrapper properties; private ApiPropertyListWrapper properties;
@JsonProperty("required") @JsonProperty("required")
@ -119,14 +118,14 @@ public class ApiPropertyDefn implements Serializable
this.name = name; this.name = name;
} }
@JsonProperty("enum") @JsonProperty("allowableValues")
public List<Object> getPossibleValues() { public AllowableValues getAllowableValues() {
return possibleValues; return allowableValues;
} }
@JsonProperty("enum") @JsonProperty("allowableValues")
public void setEnum(List<Object> possibleValues) { public void setAllowableValues(AllowableValues possibleValues) {
this.possibleValues = possibleValues; this.allowableValues = possibleValues;
} }
@JsonProperty("properties") @JsonProperty("properties")

View File

@ -21,10 +21,6 @@ import com.wordnik.swagger.codegen.config.ApiConfiguration;
import com.wordnik.swagger.codegen.config.DataTypeMappingProvider; import com.wordnik.swagger.codegen.config.DataTypeMappingProvider;
import com.wordnik.swagger.codegen.config.NamingPolicyProvider; import com.wordnik.swagger.codegen.config.NamingPolicyProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/** /**
* User: ramesh * User: ramesh
* Date: 3/31/11 * Date: 3/31/11
@ -37,7 +33,7 @@ public class ModelField {
private String defaultValue; private String defaultValue;
private boolean required = false; private boolean required = false;
private boolean allowMultiple = false; private boolean allowMultiple = false;
private List<String> allowableValues = null; private AllowableValues allowableValues = null;
private String paramType; private String paramType;
private String dataType; private String dataType;
private String internalDescription; private String internalDescription;
@ -84,7 +80,7 @@ public class ModelField {
this.required = required; this.required = required;
} }
public List<String> getAllowableValues() { public AllowableValues getAllowableValues() {
return allowableValues; return allowableValues;
} }
@ -96,32 +92,20 @@ public class ModelField {
this.allowMultiple = allowMultiple; this.allowMultiple = allowMultiple;
} }
public void setAllowableValues(List<String> allowableValues) { public void setAllowableValues(AllowableValues allowableValues) {
this.allowableValues = allowableValues; this.allowableValues = allowableValues;
} }
public String getAllowedValuesString() { public String getAllowedValuesString() {
String result = ""; if(this.allowableValues != null){
if (this.allowableValues != null) { return this.allowableValues.toString();
for(String allowedValue: this.allowableValues){ }else{
result += (allowedValue +",");
}
}
if(result.length() == 0)
return null; return null;
else }
return result.substring(0, result.length() - 1);
} }
public void setAllowedValues(String csvAlowedValue) { public void setAllowedValues(String csvAlowedValue) {
List<String> allowedValues = new ArrayList<String>(); this.setAllowableValues(AllowableValues.ConvertAllowableValuesStringToObject(csvAlowedValue));
if (csvAlowedValue != null) {
StringTokenizer tokenizer = new StringTokenizer( csvAlowedValue, "," );
while(tokenizer.hasMoreTokens()){
tokenizer.nextToken(",");
}
}
this.setAllowableValues(allowedValues);
} }
public String getParamType() { public String getParamType() {