Skip to content

#49 #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 2, 2020
Merged

#49 #50

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
Expand All @@ -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)}"
}

}
Expand Down
42 changes: 31 additions & 11 deletions src/main/groovy/com/github/hauner/openapi/support/Identifier.groovy
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)) {
Expand All @@ -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) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}

}

This file was deleted.