Permalink
Browse files

Optional support in query parameters

  • Loading branch information...
1 parent 04f8123 commit 7dbe8d9dc261a8b32889b40ea16d5fea9cb79702 @rucek rucek committed Sep 11, 2014
View
Binary file not shown.
@@ -1,7 +1,4 @@
package com._8x8.cloud.swagger2raml.model
-
-import java.util.regex.Pattern
-
/**
* @author Jacek Kunicki
*/
@@ -22,13 +19,10 @@ class DirectModelProperty extends ModelPropertyType {
class ReferenceModelProperty extends ModelPropertyType {
- private static final Pattern OPTIONAL_PATTERN = ~/Optional«(\w+)»/
-
@Override
SchemaPropertyType toSchemaProperty(Map<String, Model> models) {
- if (name.matches(OPTIONAL_PATTERN)) {
- String actualType = name.replaceAll(OPTIONAL_PATTERN, '$1')
- return SchemaPropertyType.optionalPrimitive(actualType)
+ if (OptionalSupport.isOptional(name)) {
+ return SchemaPropertyType.optionalPrimitive(OptionalSupport.actualType(name))
} else {
return new ObjectSchemaProperty(
name: name,
@@ -0,0 +1,23 @@
+package com._8x8.cloud.swagger2raml.model
+
+import java.util.regex.Pattern
+
+/**
+ * @author Jacek Kunicki
+ */
+class OptionalSupport {
+
+ private static final Pattern OPTIONAL_PATTERN = ~/Optional«(\w+)»/
+
+ static String actualTypeIfOptional(String originalType) {
+ return isOptional(originalType) ? actualType(originalType) : originalType
+ }
+
+ static boolean isOptional(String name) {
+ return name.matches(OPTIONAL_PATTERN)
+ }
+
+ static String actualType(String originalType) {
+ return originalType.replaceAll(OPTIONAL_PATTERN, '$1')
+ }
+}
@@ -2,15 +2,61 @@ package com._8x8.cloud.swagger2raml.model
import groovy.transform.Canonical
import groovy.transform.ToString
+import groovy.util.logging.Log
/**
* @author Jacek Kunicki
*/
@Canonical
@ToString(includePackage = false)
+@Log
class QueryParameter {
+ private static final String[] SUPPORTED_TYPES = ['string', 'number', 'integer', 'file', 'date', 'boolean', 'array']
+
String name, displayName, type, description, example
boolean required
Boolean repeat
+
+ static QueryParameter create(parameter) {
+ String parameterType = OptionalSupport.actualTypeIfOptional(parameter.type)
+
+ parameterType = longToInteger(parameterType)
+ parameterType = unsupportedTypeToString(parameterType)
+
+ QueryParameter queryParameter = new QueryParameter(
+ name: parameter.name,
+ displayName: parameter.name,
+ description: parameter.description,
+ type: parameterType,
+ required: parameter.required
+ )
+
+ if (parameterType == 'array') {
+ queryParameter.with {
+ type = 'string'
+ repeat = true
+ }
+ }
+
+ return queryParameter
+ }
+
+ private static String unsupportedTypeToString(String parameterType) {
+ if (!(parameterType in SUPPORTED_TYPES)) {
+ log.warning("Parameter type is ${parameterType} " +
+ "but has to be one of: ${SUPPORTED_TYPES.join(', ')}. Setting string instead.")
+ parameterType = 'string'
+ }
+
+ return parameterType
+ }
+
+ private static String longToInteger(String parameterType) {
+ if (parameterType == 'long') {
+ parameterType = 'integer'
+ }
+
+ return parameterType
+ }
}
@@ -8,26 +8,17 @@ import com._8x8.cloud.swagger2raml.model.EnumModelProperty
import com._8x8.cloud.swagger2raml.model.Method
import com._8x8.cloud.swagger2raml.model.Model
import com._8x8.cloud.swagger2raml.model.ModelPropertyType
-import com._8x8.cloud.swagger2raml.model.ObjectSchemaProperty
import com._8x8.cloud.swagger2raml.model.Path
import com._8x8.cloud.swagger2raml.model.QueryParameter
import com._8x8.cloud.swagger2raml.model.ReferenceModelProperty
import com._8x8.cloud.swagger2raml.model.Resource
import com._8x8.cloud.swagger2raml.model.SchemaProperty
-import com._8x8.cloud.swagger2raml.model.SchemaPropertyType
-
-import java.util.regex.Pattern
-import groovy.util.logging.Log
/**
* @author Jacek Kunicki
*/
-@Log
class SwaggerResourceReader extends SwaggerReader<Resource> {
- private static final String[] SUPPORTED_PARAMETER_TYPES = ['string', 'number', 'integer', 'file', 'date', 'boolean', 'array']
-
-
@Override
Resource readFromUrl(String url) {
def json = jsonSlurper.parse(new URL(url))
@@ -109,29 +100,7 @@ class SwaggerResourceReader extends SwaggerReader<Resource> {
}
private static Collection<QueryParameter> extractQueryParameters(operation) {
- return operation.parameters.findAll { it.paramType == 'query' }.collect { parameter ->
- if (!(parameter.type in SUPPORTED_PARAMETER_TYPES)) {
- log.warning("Parameter type is ${parameter.type} " +
- "but has to be one of: ${SUPPORTED_PARAMETER_TYPES.join(', ')}. Setting string instead.")
- parameter.type = 'string'
- }
- QueryParameter queryParameter = new QueryParameter(
- name: parameter.name,
- displayName: parameter.name,
- description: parameter.description,
- type: parameter.type,
- required: parameter.required
- )
-
- if (parameter.type == 'array') {
- queryParameter.with {
- type = 'string'
- repeat = true
- }
- }
-
- return queryParameter
- }
+ return operation.parameters.findAll { it.paramType == 'query' }.collect { QueryParameter.create(it) }
}
private static Collection<Body> extractResponses(operation) {
@@ -0,0 +1,33 @@
+package com._8x8.cloud.swagger2raml.model
+
+import spock.lang.Specification
+import spock.lang.Unroll
+
+/**
+ * @author Jacek Kunicki
+ */
+class QueryParameterSpec extends Specification {
+
+ private static final PARAMETER_BASE = [
+ name : 'foo',
+ displayName: 'bar',
+ description: 'baz',
+ required : false
+ ]
+
+ @Unroll
+ def 'should correctly determine parameter type for #originalType'() {
+ expect:
+ QueryParameter.create(PARAMETER_BASE + [type: originalType]).type == expectedType
+
+ where:
+ originalType || expectedType
+ 'string' || 'string'
+ 'integer' || 'integer'
+ 'Optional«string»' || 'string'
+ 'long' || 'integer'
+ 'Optional«long»' || 'integer'
+ 'foo' || 'string'
+ 'Optional«bar»' || 'string'
+ }
+}

0 comments on commit 7dbe8d9

Please sign in to comment.