forked from loafle/openapi-generator-original
Merge branch 'master' of https://github.com/openapitools/openapi-generator
This commit is contained in:
commit
73605a0c0e
@ -25,7 +25,6 @@ import com.google.common.collect.ImmutableMap;
|
|||||||
import com.samskivert.mustache.Mustache;
|
import com.samskivert.mustache.Mustache;
|
||||||
import com.samskivert.mustache.Mustache.Compiler;
|
import com.samskivert.mustache.Mustache.Compiler;
|
||||||
import com.samskivert.mustache.Mustache.Lambda;
|
import com.samskivert.mustache.Mustache.Lambda;
|
||||||
|
|
||||||
import io.swagger.v3.core.util.Json;
|
import io.swagger.v3.core.util.Json;
|
||||||
import io.swagger.v3.oas.models.OpenAPI;
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
import io.swagger.v3.oas.models.Operation;
|
import io.swagger.v3.oas.models.Operation;
|
||||||
@ -57,16 +56,11 @@ import org.openapitools.codegen.meta.Stability;
|
|||||||
import org.openapitools.codegen.meta.features.*;
|
import org.openapitools.codegen.meta.features.*;
|
||||||
import org.openapitools.codegen.serializer.SerializerUtils;
|
import org.openapitools.codegen.serializer.SerializerUtils;
|
||||||
import org.openapitools.codegen.templating.MustacheEngineAdapter;
|
import org.openapitools.codegen.templating.MustacheEngineAdapter;
|
||||||
import org.openapitools.codegen.templating.mustache.CamelCaseLambda;
|
import org.openapitools.codegen.templating.mustache.*;
|
||||||
import org.openapitools.codegen.templating.mustache.IndentedLambda;
|
|
||||||
import org.openapitools.codegen.templating.mustache.LowercaseLambda;
|
|
||||||
import org.openapitools.codegen.templating.mustache.TitlecaseLambda;
|
|
||||||
import org.openapitools.codegen.templating.mustache.UppercaseLambda;
|
|
||||||
import org.openapitools.codegen.utils.ModelUtils;
|
import org.openapitools.codegen.utils.ModelUtils;
|
||||||
import org.openapitools.codegen.utils.OneOfImplementorAdditionalData;
|
import org.openapitools.codegen.utils.OneOfImplementorAdditionalData;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import static org.openapitools.codegen.utils.OnceLogger.once;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -78,6 +72,7 @@ import java.util.regex.Pattern;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.openapitools.codegen.utils.OnceLogger.once;
|
||||||
import static org.openapitools.codegen.utils.StringUtils.*;
|
import static org.openapitools.codegen.utils.StringUtils.*;
|
||||||
|
|
||||||
public class DefaultCodegen implements CodegenConfig {
|
public class DefaultCodegen implements CodegenConfig {
|
||||||
@ -504,34 +499,34 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
|
|
||||||
public void setCircularReferences(Map<String, CodegenModel> models) {
|
public void setCircularReferences(Map<String, CodegenModel> models) {
|
||||||
final Map<String, List<CodegenProperty>> dependencyMap = models.entrySet().stream()
|
final Map<String, List<CodegenProperty>> dependencyMap = models.entrySet().stream()
|
||||||
.collect(Collectors.toMap(Entry::getKey, entry -> getModelDependencies(entry.getValue())));
|
.collect(Collectors.toMap(Entry::getKey, entry -> getModelDependencies(entry.getValue())));
|
||||||
|
|
||||||
models.keySet().forEach(name -> setCircularReferencesOnProperties(name, dependencyMap));
|
models.keySet().forEach(name -> setCircularReferencesOnProperties(name, dependencyMap));
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<CodegenProperty> getModelDependencies(CodegenModel model) {
|
private List<CodegenProperty> getModelDependencies(CodegenModel model) {
|
||||||
return model.getAllVars().stream()
|
return model.getAllVars().stream()
|
||||||
.map(prop -> {
|
.map(prop -> {
|
||||||
if (prop.isContainer) {
|
if (prop.isContainer) {
|
||||||
return prop.items.dataType == null ? null : prop;
|
return prop.items.dataType == null ? null : prop;
|
||||||
}
|
}
|
||||||
return prop.dataType == null ? null : prop;
|
return prop.dataType == null ? null : prop;
|
||||||
})
|
})
|
||||||
.filter(prop -> prop != null)
|
.filter(prop -> prop != null)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setCircularReferencesOnProperties(final String root,
|
private void setCircularReferencesOnProperties(final String root,
|
||||||
final Map<String, List<CodegenProperty>> dependencyMap) {
|
final Map<String, List<CodegenProperty>> dependencyMap) {
|
||||||
dependencyMap.getOrDefault(root, new ArrayList<>()).stream()
|
dependencyMap.getOrDefault(root, new ArrayList<>()).stream()
|
||||||
.forEach(prop -> {
|
.forEach(prop -> {
|
||||||
final List<String> unvisited =
|
final List<String> unvisited =
|
||||||
Collections.singletonList(prop.isContainer ? prop.items.dataType : prop.dataType);
|
Collections.singletonList(prop.isContainer ? prop.items.dataType : prop.dataType);
|
||||||
prop.isCircularReference = isCircularReference(root,
|
prop.isCircularReference = isCircularReference(root,
|
||||||
new HashSet<>(),
|
new HashSet<>(),
|
||||||
new ArrayList<>(unvisited),
|
new ArrayList<>(unvisited),
|
||||||
dependencyMap);
|
dependencyMap);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isCircularReference(final String root,
|
private boolean isCircularReference(final String root,
|
||||||
@ -545,7 +540,7 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
dependencyMap.getOrDefault(next, new ArrayList<>())
|
dependencyMap.getOrDefault(next, new ArrayList<>())
|
||||||
.forEach(prop -> unvisited.add(prop.isContainer ? prop.items.dataType : prop.dataType));
|
.forEach(prop -> unvisited.add(prop.isContainer ? prop.items.dataType : prop.dataType));
|
||||||
visited.add(next);
|
visited.add(next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1122,7 +1117,9 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
this.allowUnicodeIdentifiers = allowUnicodeIdentifiers;
|
this.allowUnicodeIdentifiers = allowUnicodeIdentifiers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean getUseOneOfInterfaces() { return useOneOfInterfaces; }
|
public Boolean getUseOneOfInterfaces() {
|
||||||
|
return useOneOfInterfaces;
|
||||||
|
}
|
||||||
|
|
||||||
public void setUseOneOfInterfaces(Boolean useOneOfInterfaces) {
|
public void setUseOneOfInterfaces(Boolean useOneOfInterfaces) {
|
||||||
this.useOneOfInterfaces = useOneOfInterfaces;
|
this.useOneOfInterfaces = useOneOfInterfaces;
|
||||||
@ -1805,7 +1802,7 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the name of the 'allOf' composed schema.
|
* Return the name of the 'allOf' composed schema.
|
||||||
*
|
*
|
||||||
* @param names List of names
|
* @param names List of names
|
||||||
* @param composedSchema composed schema
|
* @param composedSchema composed schema
|
||||||
* @return name of the allOf schema
|
* @return name of the allOf schema
|
||||||
@ -1857,7 +1854,7 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a string representation of the schema type, resolving aliasing and references if necessary.
|
* Return a string representation of the schema type, resolving aliasing and references if necessary.
|
||||||
*
|
*
|
||||||
* @param schema
|
* @param schema
|
||||||
* @return the string representation of the schema type.
|
* @return the string representation of the schema type.
|
||||||
*/
|
*/
|
||||||
@ -1884,7 +1881,7 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
/**
|
/**
|
||||||
* Return the OAI type (e.g. integer, long, etc) corresponding to a schema.
|
* Return the OAI type (e.g. integer, long, etc) corresponding to a schema.
|
||||||
* <pre>$ref</pre> is not taken into account by this method.
|
* <pre>$ref</pre> is not taken into account by this method.
|
||||||
*
|
* <p>
|
||||||
* If the schema is free-form (i.e. 'type: object' with no properties) or inline
|
* If the schema is free-form (i.e. 'type: object' with no properties) or inline
|
||||||
* schema, the returned OAI type is 'object'.
|
* schema, the returned OAI type is 'object'.
|
||||||
*
|
*
|
||||||
@ -2117,7 +2114,7 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
m.getVendorExtensions().putAll(schema.getExtensions());
|
m.getVendorExtensions().putAll(schema.getExtensions());
|
||||||
}
|
}
|
||||||
m.isAlias = (typeAliases.containsKey(name)
|
m.isAlias = (typeAliases.containsKey(name)
|
||||||
|| isAliasOfSimpleTypes(schema)); // check if the unaliased schema is an alias of simple OAS types
|
|| isAliasOfSimpleTypes(schema)); // check if the unaliased schema is an alias of simple OAS types
|
||||||
m.discriminator = createDiscriminator(name, schema);
|
m.discriminator = createDiscriminator(name, schema);
|
||||||
|
|
||||||
if (schema.getXml() != null) {
|
if (schema.getXml() != null) {
|
||||||
@ -3303,16 +3300,28 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("default".equals(responseCode)) {
|
if ("default".equals(responseCode) || "defaultResponse".equals(responseCode)) {
|
||||||
r.code = "0";
|
r.code = "0";
|
||||||
} else {
|
} else {
|
||||||
r.code = responseCode;
|
r.code = responseCode;
|
||||||
switch(r.code.charAt(0)) {
|
switch (r.code.charAt(0)) {
|
||||||
case '1': r.is1xx = true; break;
|
case '1':
|
||||||
case '2': r.is2xx = true; break;
|
r.is1xx = true;
|
||||||
case '3': r.is3xx = true; break;
|
break;
|
||||||
case '4': r.is4xx = true; break;
|
case '2':
|
||||||
case '5': r.is5xx = true; break;
|
r.is2xx = true;
|
||||||
|
break;
|
||||||
|
case '3':
|
||||||
|
r.is3xx = true;
|
||||||
|
break;
|
||||||
|
case '4':
|
||||||
|
r.is4xx = true;
|
||||||
|
break;
|
||||||
|
case '5':
|
||||||
|
r.is5xx = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new RuntimeException("Invalid response code " + responseCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Schema responseSchema;
|
Schema responseSchema;
|
||||||
@ -5756,9 +5765,11 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//// Following methods are related to the "useOneOfInterfaces" feature
|
//// Following methods are related to the "useOneOfInterfaces" feature
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add "x-oneOf-name" extension to a given oneOf schema (assuming it has at least 1 oneOf elements)
|
* Add "x-oneOf-name" extension to a given oneOf schema (assuming it has at least 1 oneOf elements)
|
||||||
* @param s schema to add the extension to
|
*
|
||||||
|
* @param s schema to add the extension to
|
||||||
* @param name name of the parent oneOf schema
|
* @param name name of the parent oneOf schema
|
||||||
*/
|
*/
|
||||||
public void addOneOfNameExtension(ComposedSchema s, String name) {
|
public void addOneOfNameExtension(ComposedSchema s, String name) {
|
||||||
@ -5769,7 +5780,8 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a given ComposedSchema as an interface model to be generated, assuming it has `oneOf` defined
|
* Add a given ComposedSchema as an interface model to be generated, assuming it has `oneOf` defined
|
||||||
* @param cs ComposedSchema object to create as interface model
|
*
|
||||||
|
* @param cs ComposedSchema object to create as interface model
|
||||||
* @param type name to use for the generated interface model
|
* @param type name to use for the generated interface model
|
||||||
*/
|
*/
|
||||||
public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
|
public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
|
||||||
@ -5801,7 +5813,8 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
addOneOfInterfaces.add(cm);
|
addOneOfInterfaces.add(cm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addImportsToOneOfInterface(List<Map<String, String>> imports) {}
|
public void addImportsToOneOfInterface(List<Map<String, String>> imports) {
|
||||||
|
}
|
||||||
//// End of methods related to the "useOneOfInterfaces" feature
|
//// End of methods related to the "useOneOfInterfaces" feature
|
||||||
|
|
||||||
protected void modifyFeatureSet(Consumer<FeatureSet.Builder> processor) {
|
protected void modifyFeatureSet(Consumer<FeatureSet.Builder> processor) {
|
||||||
|
@ -18,11 +18,9 @@
|
|||||||
package org.openapitools.codegen.javascript;
|
package org.openapitools.codegen.javascript;
|
||||||
|
|
||||||
import io.swagger.v3.oas.models.OpenAPI;
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
|
import io.swagger.v3.oas.models.Operation;
|
||||||
import io.swagger.v3.oas.models.media.Schema;
|
import io.swagger.v3.oas.models.media.Schema;
|
||||||
import org.openapitools.codegen.CodegenConstants;
|
import org.openapitools.codegen.*;
|
||||||
import org.openapitools.codegen.CodegenModel;
|
|
||||||
import org.openapitools.codegen.CodegenProperty;
|
|
||||||
import org.openapitools.codegen.TestUtils;
|
|
||||||
import org.openapitools.codegen.languages.JavascriptClientCodegen;
|
import org.openapitools.codegen.languages.JavascriptClientCodegen;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
@ -98,4 +96,21 @@ public class JavascriptClientCodegenTest {
|
|||||||
Assert.assertFalse(property2.isContainer);
|
Assert.assertFalse(property2.isContainer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(description = "test isDefualt in the response")
|
||||||
|
public void testResponseIsDefault() throws Exception {
|
||||||
|
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/petstore.yaml");
|
||||||
|
final DefaultCodegen codegen = new DefaultCodegen();
|
||||||
|
codegen.setOpenAPI(openAPI);
|
||||||
|
|
||||||
|
Operation textOperation = openAPI.getPaths().get("/user").getPost();
|
||||||
|
CodegenOperation coText = codegen.fromOperation("/user", "post", textOperation, null);
|
||||||
|
|
||||||
|
for (CodegenResponse cr : coText.responses) {
|
||||||
|
Assert.assertTrue(cr.isDefault);
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(coText.responses.size(), 1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user