Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 2ba22f9

Browse files
committed
improved obsolete mapping format/key check
1 parent 837ee32 commit 2ba22f9

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

src/main/groovy/com/github/hauner/openapi/core/processor/MappingConverter.groovy

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import com.github.hauner.openapi.core.processor.mapping.Mapping as MappingV1
2222
import com.github.hauner.openapi.core.processor.mapping.VersionedMapping
2323
import com.github.hauner.openapi.core.processor.mapping.v2.MappingConverter as MappingConverterV2
2424
import com.github.hauner.openapi.core.processor.mapping.v2.Mapping as MappingV2
25-
import org.slf4j.Logger
26-
import org.slf4j.LoggerFactory
2725

2826
/**
2927
* Converter for the type mapping from the mapping yaml. It converts the type mapping information
@@ -32,14 +30,12 @@ import org.slf4j.LoggerFactory
3230
* @author Martin Hauner
3331
*/
3432
class MappingConverter {
35-
private static final Logger LOG = LoggerFactory.getLogger (MappingConverter)
3633

3734
List<Mapping> convert (VersionedMapping source) {
3835
if (source?.isV2()) {
3936
def converter = new MappingConverterV2 ()
4037
converter.convert (source as MappingV2)
4138
} else {
42-
LOG.warn ("current mapping.yaml format is deprecated, upgrade to v2")
4339
def converter = new MappingConverterV1 ()
4440
converter.convert (source as MappingV1)
4541
}

src/main/groovy/com/github/hauner/openapi/core/processor/MappingReader.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ import com.github.hauner.openapi.core.processor.mapping.version.Mapping as Versi
3030
import com.github.hauner.openapi.core.processor.mapping.v2.Mapping as MappingV2
3131
import com.github.hauner.openapi.core.processor.mapping.v2.Parameter as ParameterV2
3232
import com.github.hauner.openapi.core.processor.mapping.v2.ParameterDeserializer as ParameterDeserializerV2
33+
import groovy.util.logging.Slf4j
3334

3435
/**
3536
* Reader for mapping yaml.
3637
*
3738
* @author Martin Hauner
3839
*/
40+
@Slf4j
3941
class MappingReader {
4042

4143
VersionedMapping read (String typeMappings) {
@@ -54,11 +56,19 @@ class MappingReader {
5456

5557
def versionMapper = createVersionParser ()
5658
VersionMapping version = versionMapper.readValue (mapping, VersionMapping)
59+
60+
if (version.isDeprecatedVersionKey ()) {
61+
log.warn ('the mapping version key "openapi-processor-spring" is deprecated, please use "openapi-processor-mapping"')
62+
}
63+
5764
if (version.v2) {
5865
def mapper = createV2Parser ()
5966
mapper.readValue (mapping, MappingV2)
6067
} else {
6168
// assume v1
69+
log.info ('please update the mapping to the latest format')
70+
log.info ('see https://hauner.github.io/openapi-processor/spring/mapping/structure.html')
71+
6272
def mapper = createYamlParser ()
6373
mapper.readValue (mapping, Mapping)
6474
}

src/main/kotlin/com/github/hauner/openapi/core/processor/mapping/version/Mapping.kt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,32 @@
1616

1717
package com.github.hauner.openapi.core.processor.mapping.version
1818

19-
import com.fasterxml.jackson.annotation.JsonAlias
2019
import com.fasterxml.jackson.annotation.JsonProperty
2120

2221
data class Mapping(
2322
@JsonProperty("openapi-processor-mapping")
24-
@JsonAlias("openapi-processor-spring") // deprecated
25-
val version: String?) {
23+
val version: String? = null,
24+
25+
@JsonProperty("openapi-processor-spring")
26+
val versionObsolete: String? = null
27+
) {
2628

2729
fun isV2(): Boolean {
28-
if (version == null) {
29-
return false
30-
}
30+
return getSafeVersion().startsWith("v2")
31+
}
32+
33+
fun isDeprecatedVersionKey (): Boolean {
34+
return versionObsolete != null
35+
}
36+
37+
private fun getSafeVersion(): String {
38+
if (version != null)
39+
return version
40+
41+
if (versionObsolete != null)
42+
return versionObsolete
3143

32-
return version.startsWith("v2")
44+
return "no version"
3345
}
3446

3547
}

src/test/groovy/com/github/hauner/openapi/core/processor/MappingReaderSpec.groovy

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.github.hauner.openapi.core.processor
1818

19+
import com.github.hauner.openapi.core.test.Sl4jMockRule
1920
import com.google.common.jimfs.Configuration
2021
import com.google.common.jimfs.Jimfs
2122
import org.junit.Rule
2223
import org.junit.rules.TemporaryFolder
24+
import org.slf4j.Logger
2325
import spock.lang.Shared
2426
import spock.lang.Specification
2527

@@ -33,6 +35,9 @@ class MappingReaderSpec extends Specification {
3335
@Rule
3436
TemporaryFolder folder
3537

38+
def log = Mock Logger
39+
@Rule Sl4jMockRule rule = new Sl4jMockRule(MappingReader, log)
40+
3641
void "ignores empty type mapping" () {
3742
when:
3843
def reader = new MappingReader()
@@ -105,4 +110,28 @@ map:
105110
mapping
106111
}
107112

113+
void "warns use of old mapping version key" () {
114+
def yaml = """\
115+
openapi-processor-spring: v2
116+
"""
117+
118+
when:
119+
new MappingReader().read (yaml)
120+
121+
then:
122+
1 * log.warn (*_)
123+
}
124+
125+
void "warns use of old mapping format" () {
126+
def yaml = """\
127+
openapi-processor-mapping: v1
128+
"""
129+
130+
when:
131+
new MappingReader().read (yaml)
132+
133+
then:
134+
2 * log.info (*_)
135+
}
136+
108137
}

0 commit comments

Comments
 (0)