forked from loafle/openapi-generator-original
fix, tests for #1822
This commit is contained in:
parent
8923a541c0
commit
19c81bf7bd
@ -1792,6 +1792,20 @@ public class DefaultCodegen {
|
|||||||
opList = new ArrayList<CodegenOperation>();
|
opList = new ArrayList<CodegenOperation>();
|
||||||
operations.put(tag, opList);
|
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);
|
opList.add(co);
|
||||||
co.baseName = tag;
|
co.baseName = tag;
|
||||||
}
|
}
|
||||||
|
@ -9,22 +9,16 @@ import org.testng.annotations.AfterMethod;
|
|||||||
import org.testng.annotations.BeforeMethod;
|
import org.testng.annotations.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.*;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.*;
|
||||||
import static org.testng.Assert.assertTrue;
|
|
||||||
import static org.testng.Assert.assertNull;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for DefaultGenerator logic
|
* Tests for DefaultGenerator logic
|
||||||
@ -182,6 +176,30 @@ public class DefaultGeneratorTest {
|
|||||||
assertTrue(pom.exists());
|
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 {
|
private void changeContent(File file) throws IOException {
|
||||||
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), UTF_8));
|
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), UTF_8));
|
||||||
out.write(TEST_SKIP_OVERWRITE);
|
out.write(TEST_SKIP_OVERWRITE);
|
||||||
@ -198,5 +216,4 @@ public class DefaultGeneratorTest {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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"
|
Loading…
x
Reference in New Issue
Block a user