From fc18c1c94e58967cb92bfab95f13265a3c40fc60 Mon Sep 17 00:00:00 2001 From: rpidikiti Date: Wed, 19 Oct 2011 16:03:25 -0700 Subject: [PATCH] Added support for range and list allowable values --- .../swagger/codegen/LibraryCodeGenerator.java | 4 +- .../codegen/resource/AllowableListValues.java | 59 +++++++++++++++ .../resource/AllowableRangeValues.java | 75 +++++++++++++++++++ .../codegen/resource/AllowableValues.java | 73 ++++++++++++++++++ .../codegen/resource/ApiPropertyDefn.java | 21 +++--- .../swagger/codegen/resource/ModelField.java | 32 ++------ 6 files changed, 227 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/wordnik/swagger/codegen/resource/AllowableListValues.java create mode 100644 src/main/java/com/wordnik/swagger/codegen/resource/AllowableRangeValues.java create mode 100644 src/main/java/com/wordnik/swagger/codegen/resource/AllowableValues.java diff --git a/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java b/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java index 4587c7dc16a..c2fe63f0acf 100644 --- a/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java +++ b/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java @@ -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 = "\""; } diff --git a/src/main/java/com/wordnik/swagger/codegen/resource/AllowableListValues.java b/src/main/java/com/wordnik/swagger/codegen/resource/AllowableListValues.java new file mode 100644 index 00000000000..657f489fc84 --- /dev/null +++ b/src/main/java/com/wordnik/swagger/codegen/resource/AllowableListValues.java @@ -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 values; + + public String getValueType() { + return valueType; + } + + public void setValueType(String valueType) { + this.valueType = valueType; + } + + public List getValues() { + return values; + } + + public void setValues(List 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); + } +} diff --git a/src/main/java/com/wordnik/swagger/codegen/resource/AllowableRangeValues.java b/src/main/java/com/wordnik/swagger/codegen/resource/AllowableRangeValues.java new file mode 100644 index 00000000000..fda744939b3 --- /dev/null +++ b/src/main/java/com/wordnik/swagger/codegen/resource/AllowableRangeValues.java @@ -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 + ")"; + } + } + +} diff --git a/src/main/java/com/wordnik/swagger/codegen/resource/AllowableValues.java b/src/main/java/com/wordnik/swagger/codegen/resource/AllowableValues.java new file mode 100644 index 00000000000..867aed4884b --- /dev/null +++ b/src/main/java/com/wordnik/swagger/codegen/resource/AllowableValues.java @@ -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 allowedValues = new ArrayList(); + 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; + } +} diff --git a/src/main/java/com/wordnik/swagger/codegen/resource/ApiPropertyDefn.java b/src/main/java/com/wordnik/swagger/codegen/resource/ApiPropertyDefn.java index b0fe41c2c2a..e7c00b09752 100644 --- a/src/main/java/com/wordnik/swagger/codegen/resource/ApiPropertyDefn.java +++ b/src/main/java/com/wordnik/swagger/codegen/resource/ApiPropertyDefn.java @@ -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 possibleValues = new ArrayList(); + @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 getPossibleValues() { - return possibleValues; + @JsonProperty("allowableValues") + public AllowableValues getAllowableValues() { + return allowableValues; } - @JsonProperty("enum") - public void setEnum(List possibleValues) { - this.possibleValues = possibleValues; + @JsonProperty("allowableValues") + public void setAllowableValues(AllowableValues possibleValues) { + this.allowableValues = possibleValues; } @JsonProperty("properties") diff --git a/src/main/java/com/wordnik/swagger/codegen/resource/ModelField.java b/src/main/java/com/wordnik/swagger/codegen/resource/ModelField.java index ea5393b8c06..ea9e61f6dd5 100644 --- a/src/main/java/com/wordnik/swagger/codegen/resource/ModelField.java +++ b/src/main/java/com/wordnik/swagger/codegen/resource/ModelField.java @@ -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 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 getAllowableValues() { + public AllowableValues getAllowableValues() { return allowableValues; } @@ -96,32 +92,20 @@ public class ModelField { this.allowMultiple = allowMultiple; } - public void setAllowableValues(List 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 allowedValues = new ArrayList(); - if (csvAlowedValue != null) { - StringTokenizer tokenizer = new StringTokenizer( csvAlowedValue, "," ); - while(tokenizer.hasMoreTokens()){ - tokenizer.nextToken(","); - } - } - this.setAllowableValues(allowedValues); + this.setAllowableValues(AllowableValues.ConvertAllowableValuesStringToObject(csvAlowedValue)); } public String getParamType() {