Merge pull request #1823 from swagger-api/issue-1822

Added duplicate operation ID generation
This commit is contained in:
Tony Tam 2016-01-06 06:25:44 -08:00
commit 55cdf68192
3 changed files with 63 additions and 11 deletions

View File

@ -1792,6 +1792,20 @@ public class DefaultCodegen {
opList = new ArrayList<CodegenOperation>();
operations.put(tag, opList);
}
// check for operationId uniqueness
String uniqueName = co.operationId;
int counter = 0;
for(CodegenOperation op : opList) {
if(uniqueName.equals(op.operationId)) {
uniqueName = co.operationId + "_" + counter;
counter ++;
}
}
if(!co.operationId.equals(uniqueName)) {
LOGGER.warn("generated unique operationId `" + uniqueName + "`");
}
co.operationId = uniqueName;
opList.add(co);
co.baseName = tag;
}

View File

@ -9,22 +9,16 @@ import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.fail;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.*;
/**
* Tests for DefaultGenerator logic
@ -182,6 +176,30 @@ public class DefaultGeneratorTest {
assertTrue(pom.exists());
}
@Test
public void testGenerateUniqueOperationIds() {
final File output = folder.getRoot();
final Swagger swagger = new SwaggerParser().read("src/test/resources/2_0/duplicateOperationIds.yaml");
CodegenConfig codegenConfig = new JavaClientCodegen();
codegenConfig.setOutputDir(output.getAbsolutePath());
ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger).config(codegenConfig);
DefaultGenerator generator = new DefaultGenerator();
generator.opts(clientOptInput);
Map<String, List<CodegenOperation>> paths = generator.processPaths(swagger.getPaths());
Set<String> opIds = new HashSet<String>();
for(String path : paths.keySet()) {
List<CodegenOperation> ops = paths.get(path);
for(CodegenOperation op : ops) {
assertFalse(opIds.contains(op.operationId));
opIds.add(op.operationId);
}
}
}
private void changeContent(File file) throws IOException {
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), UTF_8));
out.write(TEST_SKIP_OVERWRITE);
@ -198,5 +216,4 @@ public class DefaultGeneratorTest {
}
return null;
}
}

View File

@ -0,0 +1,21 @@
---
swagger: "2.0"
info:
version: "1.0.1"
title: "fun!"
basePath: "/v1"
paths:
/one:
get:
operationId: "duplicate"
parameters: []
responses:
200:
description: "success"
/two:
get:
operationId: "duplicate"
parameters: []
responses:
200:
description: "success"