diff --git a/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy b/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy index 3b9dbe4c..b710483d 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy @@ -30,7 +30,7 @@ import com.github.hauner.openapi.spring.model.parameters.PathParameter import com.github.hauner.openapi.spring.model.parameters.QueryParameter import com.github.hauner.openapi.spring.model.Response import com.github.hauner.openapi.spring.model.datatypes.DataType -import com.github.hauner.openapi.support.StringUtil +import com.github.hauner.openapi.support.Identifier import groovy.util.logging.Slf4j import io.swagger.v3.oas.models.OpenAPI import io.swagger.v3.oas.models.PathItem @@ -162,11 +162,11 @@ class ApiConverter { } private String getInlineTypeName (String path) { - StringUtil.toCamelCase (path.substring (1)) + 'RequestBody' + Identifier.toClass (path) + 'RequestBody' } private String getInlineResponseName (String path, String httpStatus) { - StringUtil.toCamelCase (path.substring (1)) + 'Response' + httpStatus + Identifier.toClass (path) + 'Response' + httpStatus } private Response createEmptyResponse () { diff --git a/src/main/groovy/com/github/hauner/openapi/spring/writer/DataTypeWriter.groovy b/src/main/groovy/com/github/hauner/openapi/spring/writer/DataTypeWriter.groovy index cfbef6b4..5e5ab429 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/writer/DataTypeWriter.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/writer/DataTypeWriter.groovy @@ -44,14 +44,14 @@ class DataTypeWriter { def propertyNames = dataType.properties.keySet () propertyNames.each { - def javaPropertyName = Identifier.fromJson (it) + def javaPropertyName = Identifier.toCamelCase (it) def propDataType = dataType.getObjectProperty (it) target.write (" @JsonProperty(\"$it\")\n") target.write (" private ${propDataType.name} ${javaPropertyName};\n\n") } propertyNames.each { - def javaPropertyName = Identifier.fromJson (it) + def javaPropertyName = Identifier.toCamelCase (it) def propDataType = dataType.getObjectProperty (it) target.write (getGetter (javaPropertyName, propDataType)) target.write (getSetter (javaPropertyName, propDataType)) diff --git a/src/main/groovy/com/github/hauner/openapi/spring/writer/MethodWriter.groovy b/src/main/groovy/com/github/hauner/openapi/spring/writer/MethodWriter.groovy index f903a1fb..ffd40cc9 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/writer/MethodWriter.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/writer/MethodWriter.groovy @@ -92,7 +92,7 @@ class MethodWriter { private String createMethodName (Endpoint endpoint) { def tokens = endpoint.path.tokenize ('/') - tokens = tokens.collect { Identifier.fromJson (it).capitalize () } + tokens = tokens.collect { Identifier.toCamelCase (it).capitalize () } def name = tokens.join ('') "${endpoint.method.method}${name}" } @@ -101,9 +101,9 @@ class MethodWriter { def ps = endpoint.parameters.collect { if (it.withAnnotation ()) { - "${createParameterAnnotation (it)} ${it.dataType.name} ${Identifier.fromJson (it.name)}" + "${createParameterAnnotation (it)} ${it.dataType.name} ${Identifier.toCamelCase (it.name)}" } else { - "${it.dataType.name} ${Identifier.fromJson (it.name)}" + "${it.dataType.name} ${Identifier.toCamelCase (it.name)}" } } diff --git a/src/main/groovy/com/github/hauner/openapi/support/Identifier.groovy b/src/main/groovy/com/github/hauner/openapi/support/Identifier.groovy index 9ffa5e9b..a2575874 100644 --- a/src/main/groovy/com/github/hauner/openapi/support/Identifier.groovy +++ b/src/main/groovy/com/github/hauner/openapi/support/Identifier.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2019 the original authors + * Copyright 2019-2020 the original authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,31 +16,34 @@ package com.github.hauner.openapi.support +import groovy.transform.CompileStatic + /** * Identifier support to create valid java identifiers. * * @author Martin Hauner */ +@CompileStatic class Identifier { /** - * converts a Json string as defined by http://www.json.org/ to a valid (camel case) java - * identifier. One way, ie it is not reversible. + * converts a source string to a valid (camel case) java identifier. One way, ie it is not + * reversible. * * conversion rules: * characters that are not valid java identifiers will be removed. The characters " ", "_", * "-" (valid or not) are interpreted as word separators and the next character will be * converted to upper case. * - * @param json a valid json "string" + * @param src the source "string" * * @return a valid camel case java identifier */ - static String fromJson (String json) { + static String toCamelCase (String src) { def sb = new StringBuilder() def wordSplit = false - json.toCharArray ().eachWithIndex { char c, int idx -> + src.toCharArray ().eachWithIndex { char c, int idx -> if (idx == 0) { if (isValidStart (c)) { @@ -64,23 +67,40 @@ class Identifier { } /** - * converts a Json string as defined by http://www.json.org/ to a valid (upper case) java - * enum identifier. One way, ie it is not reversible. + * converts a source string to a valid (camel case) java class identifier. One way, ie it is + * not reversible. + * + * conversion rules: + * characters that are not valid java identifiers will be removed. The characters " ", "_", + * "-" (valid or not) are removed and interpreted as word separators. Each words first character + * will be converted to upper case. + * + * @param src the source string + * + * @return a valid camel case java class identifier + */ + static String toClass (String src) { + toCamelCase (src).capitalize () + } + + /** + * converts a source string to a valid (all upper case) java enum identifier. One way, ie it is + * not reversible. * * conversion rules: * characters that are not valid java identifiers will be removed. The characters " ", "_", * "-" (valid or not) are interpreted as word separators and are replaced by "_" and the words * are converted to upper case. * - * @param json a valid json "string" + * @param src the source "string" * * @return a valid upper case enum java identifier */ - static String toEnum (String json) { + static String toEnum (String src) { def sb = new StringBuilder() def wordSplit = false - json.toCharArray ().eachWithIndex { char c, int idx -> + src.toCharArray ().eachWithIndex { char c, int idx -> def cu = c.toUpperCase () if (idx == 0) { diff --git a/src/main/groovy/com/github/hauner/openapi/support/StringUtil.groovy b/src/main/groovy/com/github/hauner/openapi/support/StringUtil.groovy deleted file mode 100644 index 073c6d03..00000000 --- a/src/main/groovy/com/github/hauner/openapi/support/StringUtil.groovy +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2019 the original authors - * - * 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.github.hauner.openapi.support - -/** - * String support. - * - * @author Martin Hauner - */ -class StringUtil { - - /** - * converts kebap-cased input string to camel-cased string. - * - * @param source kebab-case string - * @return camel cased source - */ - static String toCamelCase(String source) { - source.replaceAll( "(-)([\\p{Alnum}])", { Object[] it -> it[2].toUpperCase() } ) - .capitalize () - } - -} diff --git a/src/test/groovy/com/github/hauner/openapi/support/IdentifierSpec.groovy b/src/test/groovy/com/github/hauner/openapi/support/IdentifierSpec.groovy index 4089db70..daba8aae 100644 --- a/src/test/groovy/com/github/hauner/openapi/support/IdentifierSpec.groovy +++ b/src/test/groovy/com/github/hauner/openapi/support/IdentifierSpec.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2019 the original authors + * Copyright 2019-2020 the original authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,30 +22,48 @@ import spock.lang.Unroll class IdentifierSpec extends Specification { @Unroll - void "convert json string '#json' to valid java identifier '#identifier'" () { + void "convert source string '#src' to valid java identifier '#identifier'" () { expect: - Identifier.fromJson (json) == identifier + Identifier.toCamelCase (src) == identifier where: - json | identifier - "a" | "a" - "a b" | "aB" // space is invalid - "a-b" | "aB" // dash is invalid - "_ab" | "ab" // underscore is valid but unwanted - "a_b" | "aB" // underscore is valid but unwanted + src | identifier + "a" | "a" + "a b" | "aB" // space is invalid + "a-b" | "aB" // dash is invalid + 'api/some/thing' | 'apiSomeThing' // slash is invalid + "_ab" | "ab" // underscore is valid but unwanted + "a_b" | "aB" // underscore is valid but unwanted } @Unroll - void "convert json string '#json' to valid java enum identifier '#identifier'" () { + void "convert source string '#src' to valid java enum identifier '#identifier'" () { expect: - Identifier.toEnum (json) == identifier + Identifier.toEnum (src) == identifier where: - json | identifier - "a" | "A" - "a b" | "A_B" // space is invalid - "a-b" | "A_B" // dash is invalid - "_ab" | "AB" // underscore is valid but unwanted - "a_b" | "A_B" // underscore is valid but unwanted + src | identifier + "a" | "A" + "a b" | "A_B" // space is invalid + "a-b" | "A_B" // dash is invalid + 'api/some/thing' | 'API_SOME_THING' // slash is invalid + "_ab" | "AB" // underscore is valid but unwanted + "a_b" | "A_B" // underscore is valid but unwanted } + + @Unroll + void "converts source string '#src' to valid java class identifier '#identifier'" () { + expect: + Identifier.toClass (src) == identifier + + where: + src | identifier + "a" | "A" + "a b" | "AB" // space is invalid + "a-b" | "AB" // dash is invalid + 'api/some/thing' | 'ApiSomeThing' // slash is invalid + "_ab" | "Ab" // underscore is valid but unwanted + "a_b" | "AB" // underscore is valid but unwanted + } + } diff --git a/src/test/groovy/com/github/hauner/openapi/support/StringUtilSpec.groovy b/src/test/groovy/com/github/hauner/openapi/support/StringUtilSpec.groovy deleted file mode 100644 index 027081bf..00000000 --- a/src/test/groovy/com/github/hauner/openapi/support/StringUtilSpec.groovy +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2019 the original authors - * - * 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.github.hauner.openapi.support - -import spock.lang.Specification - -class StringUtilSpec extends Specification { - - void "converts kebap-cased string to camel-cased string" () { - expect: - StringUtil.toCamelCase (source) == result - - where: - source | result - 'some-api-path' | 'SomeApiPath' - } -}