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){
for(ModelField operationParam : operation.getParameters()){
//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())){
//generate enum
template = templateGroup.getInstanceOf(ENUM_OBJECT_TEMPLATE);
@ -261,7 +261,7 @@ public class LibraryCodeGenerator {
template.setAttribute("className", enumName);
template.setAttribute("description", operationParam.getDescription());
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")){
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",
"description",
"name",
"enum",
"allowableValues",
"properties",
"required",
"notes",
"access",
"type"
})
public class ApiPropertyDefn implements Serializable
{
public class ApiPropertyDefn implements Serializable {
@JsonProperty("id")
private String id;
@ -55,8 +54,8 @@ public class ApiPropertyDefn implements Serializable
private String description;
@JsonProperty("name")
private String name;
@JsonProperty("enum")
private List<Object> possibleValues = new ArrayList<Object>();
@JsonProperty("allowableValues")
private AllowableValues allowableValues = null;
@JsonProperty("properties")
private ApiPropertyListWrapper properties;
@JsonProperty("required")
@ -119,14 +118,14 @@ public class ApiPropertyDefn implements Serializable
this.name = name;
}
@JsonProperty("enum")
public List<Object> getPossibleValues() {
return possibleValues;
@JsonProperty("allowableValues")
public AllowableValues getAllowableValues() {
return allowableValues;
}
@JsonProperty("enum")
public void setEnum(List<Object> possibleValues) {
this.possibleValues = possibleValues;
@JsonProperty("allowableValues")
public void setAllowableValues(AllowableValues possibleValues) {
this.allowableValues = possibleValues;
}
@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.NamingPolicyProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* User: ramesh
* Date: 3/31/11
@ -37,7 +33,7 @@ public class ModelField {
private String defaultValue;
private boolean required = false;
private boolean allowMultiple = false;
private List<String> allowableValues = null;
private AllowableValues allowableValues = null;
private String paramType;
private String dataType;
private String internalDescription;
@ -84,7 +80,7 @@ public class ModelField {
this.required = required;
}
public List<String> getAllowableValues() {
public AllowableValues getAllowableValues() {
return allowableValues;
}
@ -96,32 +92,20 @@ public class ModelField {
this.allowMultiple = allowMultiple;
}
public void setAllowableValues(List<String> allowableValues) {
public void setAllowableValues(AllowableValues allowableValues) {
this.allowableValues = allowableValues;
}
public String getAllowedValuesString() {
String result = "";
if (this.allowableValues != null) {
for(String allowedValue: this.allowableValues){
result += (allowedValue +",");
}
}
if(result.length() == 0)
if(this.allowableValues != null){
return this.allowableValues.toString();
}else{
return null;
else
return result.substring(0, result.length() - 1);
}
}
public void setAllowedValues(String csvAlowedValue) {
List<String> allowedValues = new ArrayList<String>();
if (csvAlowedValue != null) {
StringTokenizer tokenizer = new StringTokenizer( csvAlowedValue, "," );
while(tokenizer.hasMoreTokens()){
tokenizer.nextToken(",");
}
}
this.setAllowableValues(allowedValues);
this.setAllowableValues(AllowableValues.ConvertAllowableValuesStringToObject(csvAlowedValue));
}
public String getParamType() {