Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Commit 655e75f

Browse files
author
Ole Lensmar
committed
improved writing of 1.X specifications
1 parent a5e4eaa commit 655e75f

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

src/main/java/com/smartbear/swagger4j/FileSwaggerStore.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Writer createResource(String path) throws IOException {
4646
// make sure directory exists
4747
if (path.indexOf('/') > 0) {
4848
String nm = path.substring(0, path.lastIndexOf('/'));
49-
File f = new File(nm, this.path);
49+
File f = new File(this.path, nm);
5050
if (!f.exists() || !f.isDirectory()) {
5151
if (!f.mkdirs()) {
5252
throw new IOException("Failed to create path [" + path + "] for file storage");
@@ -55,7 +55,7 @@ public Writer createResource(String path) throws IOException {
5555
}
5656

5757
File file = new File(this.path, path);
58-
if (path.equals("/api-docs") || path.equals("api-docs")) {
58+
if (path.startsWith("/api-docs.") || path.startsWith("api-docs.")) {
5959
apiDocsPath = file.getAbsolutePath();
6060
}
6161

src/main/java/com/smartbear/swagger4j/URISwaggerSource.java

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public Reader readApiDeclaration(String basePath, String path) throws IOExceptio
8585

8686
if (basePath == null) {
8787
basePath = uri.toString();
88+
89+
int ix = basePath.lastIndexOf("/api-docs.");
90+
if (ix != -1) {
91+
basePath = basePath.substring(0, ix + 9);
92+
}
93+
8894
} else if (!basePath.toLowerCase().startsWith("file:") && !basePath.contains("://")) {
8995
String uriString = uri.toString();
9096
if (basePath.equals(".")) {

src/main/java/com/smartbear/swagger4j/impl/SwaggerWriterImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@ public SwaggerFormat getFormat() {
293293
}
294294

295295
public void writeSwagger(SwaggerStore store, ResourceListing resourceListing) throws IOException {
296-
Writer writer = store.createResource("api-docs");
296+
Writer writer = store.createResource("api-docs." + format.getExtension());
297297
writeResourceListing(resourceListing, writer);
298298

299299
for (ResourceListing.ResourceListingApi api : resourceListing.getApis()) {
300300
ApiDeclaration declaration = api.getDeclaration();
301-
String path = Utils.createFileNameFromPath(api.getPath(), format);
301+
String path = Utils.createFileNameFromPath("api-docs" + api.getPath(), format);
302302

303303
writer = store.createResource(path);
304304
writeApiDeclaration(declaration, writer);

src/test/java/com/smartbear/swagger4j/SwaggerWriterTestCase.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testJsonCreation() throws Exception {
5757
MapSwaggerStore store = new MapSwaggerStore();
5858
ResourceListing resourceListing = createResourceListingForCreationTesting(store, SwaggerFormat.json);
5959

60-
String json = store.getFileMap().get("api-docs").toString();
60+
String json = store.getFileMap().get("api-docs.json").toString();
6161
assertTrue(json.length() > 0);
6262

6363
JsonReader reader = Json.createReader(new StringReader(json));
@@ -66,7 +66,7 @@ public void testJsonCreation() throws Exception {
6666

6767
System.out.println( json );
6868

69-
json = store.getFileMap().get("/user.json").toString();
69+
json = store.getFileMap().get("api-docs/user.json").toString();
7070
System.out.println("Created: " + json);
7171
reader = Json.createReader(new StringReader(json));
7272
JsonObject object = reader.readObject();
@@ -118,7 +118,7 @@ public void testXmlCreation() throws Exception {
118118
MapSwaggerStore store = new MapSwaggerStore();
119119
ResourceListing resourceListing = createResourceListingForCreationTesting(store, SwaggerFormat.xml);
120120

121-
String xml = store.getFileMap().get("api-docs").toString();
121+
String xml = store.getFileMap().get("api-docs.xml").toString();
122122
assertTrue(xml.length() > 0);
123123

124124
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource( new StringReader(xml)));
@@ -128,7 +128,7 @@ public void testXmlCreation() throws Exception {
128128
SwaggerVersion swaggerVersion = resourceListing.getSwaggerVersion();
129129
assertEquals(swaggerVersion.getIdentifier(), elm.getTextContent());
130130

131-
xml = store.getFileMap().get("/user.xml").toString();
131+
xml = store.getFileMap().get("api-docs/user.xml").toString();
132132
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
133133
documentElement = document.getDocumentElement();
134134
assertEquals(Constants.API_DOCUMENTATION, documentElement.getNodeName());
@@ -154,13 +154,13 @@ private MapSwaggerStore testCreation(SwaggerFactory factory, ResourceListing res
154154
swaggerWriter.writeSwagger(store, resourceListing);
155155

156156
assertEquals(3, store.getFileMap().size());
157-
assertTrue(store.getFileMap().containsKey("api-docs"));
157+
assertTrue(store.getFileMap().containsKey("api-docs.xml"));
158158

159159
swaggerWriter = factory.createSwaggerWriter(SwaggerFormat.json);
160160
swaggerWriter.writeSwagger(store, resourceListing);
161161

162-
assertEquals(5, store.getFileMap().size());
163-
assertTrue(store.getFileMap().containsKey("api-docs"));
162+
assertEquals(6, store.getFileMap().size());
163+
assertTrue(store.getFileMap().containsKey("api-docs.json"));
164164

165165
return store;
166166
}

0 commit comments

Comments
 (0)