Skip to content

Commit f29dc08

Browse files
committed
load mapping by url
1 parent 9e996d6 commit f29dc08

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ class MappingReader {
3737
return null
3838
}
3939

40-
String mapping = typeMappings
41-
if (isFileName (typeMappings)) {
40+
def mapping
41+
if (isUrl (typeMappings)) {
42+
mapping = new URL (typeMappings).text
43+
} else if (isFileName (typeMappings)) {
4244
mapping = new File (typeMappings).text
45+
} else {
46+
mapping = typeMappings
4347
}
4448

4549
def mapper = createYamlParser ()
46-
4750
mapper.readValue (mapping, Mapping)
4851
}
4952

@@ -61,4 +64,12 @@ class MappingReader {
6164
name.endsWith ('.yaml') || name.endsWith ('.yml')
6265
}
6366

67+
private boolean isUrl (String source) {
68+
try {
69+
new URL (source)
70+
} catch (MalformedURLException ignore) {
71+
false
72+
}
73+
}
74+
6475
}

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@
1616

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

19+
import com.google.common.jimfs.Configuration
20+
import com.google.common.jimfs.Jimfs
1921
import org.junit.Rule
2022
import org.junit.rules.TemporaryFolder
23+
import spock.lang.Shared
2124
import spock.lang.Specification
2225

26+
import java.nio.file.FileSystem
27+
2328
class MappingReaderSpec extends Specification {
2429

30+
@Shared
31+
FileSystem fs = Jimfs.newFileSystem (Configuration.unix ())
32+
2533
@Rule
2634
TemporaryFolder folder
2735

@@ -37,30 +45,51 @@ class MappingReaderSpec extends Specification {
3745
yaml << [null, ""]
3846
}
3947

40-
void "reads mapping from file" () {
48+
void "reads mapping from url" () {
49+
def yaml = """\
50+
openapi-processor-spring: v1.0
51+
52+
map:
53+
types:
54+
- from: array
55+
to: java.util.Collection
56+
"""
57+
58+
def yamlFile = fs.getPath ('mapping.yaml')
59+
yamlFile.text = yaml
60+
61+
when:
62+
def reader = new MappingReader()
63+
def mapping = reader.read (yamlFile.toUri ().toURL ().toString ())
64+
65+
then:
66+
mapping
67+
}
68+
69+
void "reads mapping from local file if the scheme is missing" () {
4170
def yaml = """\
42-
openapi-generatr-spring: v1.0
71+
openapi-processor-spring: v1.0
4372
4473
map:
4574
types:
4675
- from: array
4776
to: java.util.Collection
4877
"""
4978

50-
def yamlFile = folder.newFile ("openapi-generatr-spring.yaml")
79+
def yamlFile = folder.newFile ('mapping.yaml')
5180
yamlFile.text = yaml
5281

5382
when:
5483
def reader = new MappingReader()
55-
def mapping = reader.read (yamlFile.absolutePath)
84+
def mapping = reader.read (yamlFile.toString ())
5685

5786
then:
5887
mapping
5988
}
6089

6190
void "reads mapping from string" () {
6291
def yaml = """\
63-
openapi-generatr-spring: v1.0
92+
openapi-processor-spring: v1.0
6493
6594
map:
6695
types:

0 commit comments

Comments
 (0)