Skip to content

Commit

Permalink
Add default case to handle response code (OpenAPITools#5825)
Browse files Browse the repository at this point in the history
* add default case to handle response code

* fix default response code

* add test for isDefault
  • Loading branch information
wing328 authored and MikailBag committed May 31, 2020
1 parent 6bf0183 commit 7b6e32a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Mustache.Compiler;
import com.samskivert.mustache.Mustache.Lambda;

import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand Down Expand Up @@ -57,16 +56,11 @@
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.serializer.SerializerUtils;
import org.openapitools.codegen.templating.MustacheEngineAdapter;
import org.openapitools.codegen.templating.mustache.CamelCaseLambda;
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.templating.mustache.*;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.OneOfImplementorAdditionalData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.openapitools.codegen.utils.OnceLogger.once;

import java.io.File;
import java.util.*;
Expand All @@ -78,6 +72,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.openapitools.codegen.utils.OnceLogger.once;
import static org.openapitools.codegen.utils.StringUtils.*;

public class DefaultCodegen implements CodegenConfig {
Expand Down Expand Up @@ -504,34 +499,34 @@ public Map<String, Object> updateAllModels(Map<String, Object> objs) {

public void setCircularReferences(Map<String, CodegenModel> models) {
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));
}

private List<CodegenProperty> getModelDependencies(CodegenModel model) {
return model.getAllVars().stream()
.map(prop -> {
if (prop.isContainer) {
return prop.items.dataType == null ? null : prop;
}
return prop.dataType == null ? null : prop;
})
.filter(prop -> prop != null)
.collect(Collectors.toList());
.map(prop -> {
if (prop.isContainer) {
return prop.items.dataType == null ? null : prop;
}
return prop.dataType == null ? null : prop;
})
.filter(prop -> prop != null)
.collect(Collectors.toList());
}

private void setCircularReferencesOnProperties(final String root,
final Map<String, List<CodegenProperty>> dependencyMap) {
dependencyMap.getOrDefault(root, new ArrayList<>()).stream()
.forEach(prop -> {
final List<String> unvisited =
Collections.singletonList(prop.isContainer ? prop.items.dataType : prop.dataType);
prop.isCircularReference = isCircularReference(root,
new HashSet<>(),
new ArrayList<>(unvisited),
dependencyMap);
});
.forEach(prop -> {
final List<String> unvisited =
Collections.singletonList(prop.isContainer ? prop.items.dataType : prop.dataType);
prop.isCircularReference = isCircularReference(root,
new HashSet<>(),
new ArrayList<>(unvisited),
dependencyMap);
});
}

private boolean isCircularReference(final String root,
Expand All @@ -545,7 +540,7 @@ private boolean isCircularReference(final String root,
return true;
}
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);
}
}
Expand Down Expand Up @@ -1122,7 +1117,9 @@ public void setAllowUnicodeIdentifiers(Boolean allowUnicodeIdentifiers) {
this.allowUnicodeIdentifiers = allowUnicodeIdentifiers;
}

public Boolean getUseOneOfInterfaces() { return useOneOfInterfaces; }
public Boolean getUseOneOfInterfaces() {
return useOneOfInterfaces;
}

public void setUseOneOfInterfaces(Boolean useOneOfInterfaces) {
this.useOneOfInterfaces = useOneOfInterfaces;
Expand Down Expand Up @@ -1805,7 +1802,7 @@ protected Schema<?> getSchemaAdditionalProperties(Schema schema) {

/**
* Return the name of the 'allOf' composed schema.
*
*
* @param names List of names
* @param composedSchema composed schema
* @return name of the allOf schema
Expand Down Expand Up @@ -1857,7 +1854,7 @@ public String toOneOfName(List<String> names, ComposedSchema composedSchema) {

/**
* Return a string representation of the schema type, resolving aliasing and references if necessary.
*
*
* @param schema
* @return the string representation of the schema type.
*/
Expand All @@ -1884,7 +1881,7 @@ private String getSingleSchemaType(Schema 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.
*
* <p>
* If the schema is free-form (i.e. 'type: object' with no properties) or inline
* schema, the returned OAI type is 'object'.
*
Expand Down Expand Up @@ -2117,7 +2114,7 @@ public CodegenModel fromModel(String name, Schema schema) {
m.getVendorExtensions().putAll(schema.getExtensions());
}
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);

if (schema.getXml() != null) {
Expand Down Expand Up @@ -3303,16 +3300,28 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
}
}

if ("default".equals(responseCode)) {
if ("default".equals(responseCode) || "defaultResponse".equals(responseCode)) {
r.code = "0";
} else {
r.code = responseCode;
switch(r.code.charAt(0)) {
case '1': r.is1xx = true; break;
case '2': r.is2xx = true; break;
case '3': r.is3xx = true; break;
case '4': r.is4xx = true; break;
case '5': r.is5xx = true; break;
switch (r.code.charAt(0)) {
case '1':
r.is1xx = true;
break;
case '2':
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;
Expand Down Expand Up @@ -5756,9 +5765,11 @@ public void setRemoveEnumValuePrefix(final boolean removeEnumValuePrefix) {
}

//// 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)
* @param s schema to add the extension to
*
* @param s schema to add the extension to
* @param name name of the parent oneOf schema
*/
public void addOneOfNameExtension(ComposedSchema s, String name) {
Expand All @@ -5769,7 +5780,8 @@ public void addOneOfNameExtension(ComposedSchema s, String name) {

/**
* 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
*/
public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
Expand Down Expand Up @@ -5801,7 +5813,8 @@ public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
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

protected void modifyFeatureSet(Consumer<FeatureSet.Builder> processor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
package org.openapitools.codegen.javascript;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.JavascriptClientCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -98,4 +96,21 @@ public void bodyParameterTest() {
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);

}

}

0 comments on commit 7b6e32a

Please sign in to comment.