Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -4999,12 +4999,21 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
} else if (ModelUtils.isDoubleSchema(responseSchema)) { // double
r.isDouble = Boolean.TRUE;
}
} else if (ModelUtils.isFreeFormObject(responseSchema, openAPI)) {
r.isFreeFormObject = true;
r.simpleType = false;
} else if (ModelUtils.isMapSchema(responseSchema)) {
r.isMap = true;
r.simpleType = false;
r.isModel = false;
r.containerType = cp.containerType;
r.containerTypeMapped = cp.containerTypeMapped;
} else if (ModelUtils.isArraySchema(responseSchema)) {
r.isArray = true;
r.simpleType = false;
r.containerType = cp.containerType;
r.containerTypeMapped = cp.containerTypeMapped;
} else if (ModelUtils.isTypeObjectSchema(responseSchema)) {
if (ModelUtils.isFreeFormObject(responseSchema, openAPI)) {
r.isFreeFormObject = true;
} else {
r.isModel = true;
}
r.simpleType = false;
r.containerType = cp.containerType;
r.containerTypeMapped = cp.containerTypeMapped;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.samskivert.mustache.Mustache.Lambda;
import com.samskivert.mustache.Template;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.features.*;
Expand Down Expand Up @@ -286,6 +287,36 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
}
}

@Override
public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
// patch to work around the fix to set isArray, isMap in response objects
// ref: https://github.com/OpenAPITools/openapi-generator/pull/24566/
CodegenResponse cr = super.fromResponse(responseCode, response);

Schema responseSchema;
if (this.openAPI != null && this.openAPI.getComponents() != null) {
responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(openAPI, response));
} else { // no model/alias defined
responseSchema = ModelUtils.getSchemaFromResponse(openAPI, response);
}

if (ModelUtils.isTypeObjectSchema(responseSchema)) {
CodegenProperty cp = fromProperty("response", responseSchema, false);

if (ModelUtils.isFreeFormObject(responseSchema, openAPI)) {
cr.isFreeFormObject = true;
} else {
cr.isModel = true;
}
cr.simpleType = false;
cr.containerType = cp.containerType;
cr.containerTypeMapped = cp.containerTypeMapped;
addVarsRequiredVarsAdditionalProps(responseSchema, cr);
}

return cr;
}

@Override
@SuppressWarnings({"static-method", "unchecked"})
public Map<String, ModelsMap> postProcessAllModels(final Map<String, ModelsMap> orgObjs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
Expand Down Expand Up @@ -661,6 +662,36 @@ public String getNameFromDataType(CodegenProperty property) {
}
}

@Override
public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
// patch to work around the fix to set isArray, isMap in response objects
// ref: https://github.com/OpenAPITools/openapi-generator/pull/24566/
CodegenResponse cr = super.fromResponse(responseCode, response);

Schema responseSchema;
if (this.openAPI != null && this.openAPI.getComponents() != null) {
responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(openAPI, response));
} else { // no model/alias defined
responseSchema = ModelUtils.getSchemaFromResponse(openAPI, response);
}

if (ModelUtils.isTypeObjectSchema(responseSchema)) {
CodegenProperty cp = fromProperty("response", responseSchema, false);

if (ModelUtils.isFreeFormObject(responseSchema, openAPI)) {
cr.isFreeFormObject = true;
} else {
cr.isModel = true;
}
cr.simpleType = false;
cr.containerType = cp.containerType;
cr.containerTypeMapped = cp.containerTypeMapped;
addVarsRequiredVarsAdditionalProps(responseSchema, cr);
}

return cr;
}

/**
* Post-processes CodegenModel objects to apply protobuf-specific transformations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import lombok.Setter;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.utils.ModelUtils;

import java.io.File;
import java.text.Normalizer;
Expand Down Expand Up @@ -385,4 +388,34 @@ public String escapeUnsafeCharacters(String input) {
public GeneratorLanguage generatorLanguage() {
return GeneratorLanguage.WSDL;
}

@Override
public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
// patch to work around the fix to set isArray, isMap in response objects
// ref: https://github.com/OpenAPITools/openapi-generator/pull/24566/
CodegenResponse cr = super.fromResponse(responseCode, response);

Schema responseSchema;
if (this.openAPI != null && this.openAPI.getComponents() != null) {
responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(openAPI, response));
} else { // no model/alias defined
responseSchema = ModelUtils.getSchemaFromResponse(openAPI, response);
}

if (ModelUtils.isTypeObjectSchema(responseSchema)) {
CodegenProperty cp = fromProperty("response", responseSchema, false);

if (ModelUtils.isFreeFormObject(responseSchema, openAPI)) {
cr.isFreeFormObject = true;
} else {
cr.isModel = true;
}
cr.simpleType = false;
cr.containerType = cp.containerType;
cr.containerTypeMapped = cp.containerTypeMapped;
addVarsRequiredVarsAdditionalProps(responseSchema, cr);
}

return cr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonNull;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -540,7 +541,9 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
obj.addProperty(entry.getKey(), (Character) entry.getValue());
else {
JsonElement jsonElement = gson.toJsonTree(entry.getValue());
if (jsonElement.isJsonArray()) {
if (jsonElement.isJsonNull()) {
obj.add(entry.getKey(), JsonNull.INSTANCE);
} else if (jsonElement.isJsonArray()) {
obj.add(entry.getKey(), jsonElement.getAsJsonArray());
} else {
obj.add(entry.getKey(), jsonElement.getAsJsonObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@
return Objects.hash({{#allParams}}{{#vendorExtensions.x-is-jackson-optional-nullable}}hashCodeNullable({{paramName}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{^isByteArray}}{{paramName}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{paramName}}){{/isByteArray}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}}, {{/-last}}{{/allParams}});
{{/useReflectionEqualsHashCode}}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {\n");
{{#allParams}}
sb.append(" {{paramName}}: ").append({{#isPassword}}"*"{{/isPassword}}{{^isPassword}}toIndentedString({{paramName}}){{/isPassword}}).append("\n");
{{/allParams}}
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private{{#jsonb}} static{{/jsonb}} String toIndentedString(Object o) {
return o == null ? "null" : o.toString().replace("\n", "\n ");
}
}
{{/staticRequest}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@
return Objects.hash({{#allParams}}{{#vendorExtensions.x-is-jackson-optional-nullable}}hashCodeNullable({{paramName}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{^isByteArray}}{{paramName}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{paramName}}){{/isByteArray}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}}, {{/-last}}{{/allParams}});
{{/useReflectionEqualsHashCode}}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {\n");
{{#allParams}}
sb.append(" {{paramName}}: ").append({{#isPassword}}"*"{{/isPassword}}{{^isPassword}}toIndentedString({{paramName}}){{/isPassword}}).append("\n");
{{/allParams}}
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private{{#jsonb}} static{{/jsonb}} String toIndentedString(Object o) {
return o == null ? "null" : o.toString().replace("\n", "\n ");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace {{packageName}}.Test.{{apiPackage}}
{{#dataType}}
var response = await _instance.{{operationId}}Async({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
var model = response.{{vendorExtensions.x-http-status}}();
Assert.IsType<{{{.}}}>(model);
Assert.IsType<{{#isModel}}{{packageName}}.{{modelPackage}}.{{/isModel}}{{{.}}}>(model);
{{/dataType}}
{{^dataType}}
await _instance.{{operationId}}Async({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3169,7 +3169,7 @@ public void testAdditionalPropertiesPresentInParameters() {
}
}

@Test
@Test(enabled = false)
public void testAdditionalPropertiesPresentInResponses() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7613.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
package org.openapitools.codegen.csharpnetcore;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.CSharpClientCodegen;
import org.openapitools.codegen.languages.JavaCXFClientCodegen;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand All @@ -28,6 +34,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -506,4 +513,30 @@ public void testNumericEnumJsonConverterUsesNumericOperations() throws IOExcepti
"return -1.2d;"
);
}

@Test
public void testMapResponse() throws Exception {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
final CSharpClientCodegen codegen = new CSharpClientCodegen();
codegen.setOpenAPI(openAPI);
Operation operation = openAPI.getPaths().get("/store/inventory").getGet();
final CodegenOperation co = codegen.fromOperation("getInventory", "GET", operation, null);

OperationMap operationMap = new OperationMap();
operationMap.setOperation(co);

OperationsMap objs = new OperationsMap();
objs.setOperation(operationMap);
objs.setImports(Collections.emptyList());
codegen.postProcessOperationsWithModels(objs, Collections.emptyList());

Assert.assertEquals(co.responses.size(), 1);
CodegenResponse cr1 = co.responses.get(0);
Assert.assertEquals(cr1.code, "200");
Assert.assertEquals(cr1.baseType, "Integer");
Assert.assertEquals(cr1.dataType, "Map<string, Integer>");
Assert.assertFalse(cr1.isArray);
Assert.assertFalse(cr1.isModel);
Assert.assertTrue(cr1.isMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ public class JavaCXFClientCodegenTest {
@Test
public void responseWithoutContent() throws Exception {
final Schema listOfPets = new ArraySchema().items(new Schema<>().$ref("#/components/schemas/Pet"));
final Schema mapOfPets = new Schema<>().additionalProperties(new Schema<>().$ref("#/components/schemas/Pet"));
Operation operation = new Operation()
.responses(new ApiResponses()
.addApiResponse("200",
new ApiResponse().description("Return a list of pets")
.content(new Content().addMediaType("application/json",
new MediaType().schema(listOfPets))))
.addApiResponse("201",
new ApiResponse().description("Return a map of pets")
.content(new Content().addMediaType("application/json",
new MediaType().schema(mapOfPets))))
.addApiResponse("400", new ApiResponse().description("Error")));
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("Pet", new ObjectSchema());
final JavaCXFClientCodegen codegen = new JavaCXFClientCodegen();
Expand All @@ -71,18 +76,30 @@ public void responseWithoutContent() throws Exception {
objs.setImports(Collections.emptyList());
codegen.postProcessOperationsWithModels(objs, Collections.emptyList());

Assert.assertEquals(co.responses.size(), 2);
Assert.assertEquals(co.responses.size(), 3);
CodegenResponse cr1 = co.responses.get(0);
Assert.assertEquals(cr1.code, "200");
Assert.assertEquals(cr1.baseType, "Pet");
Assert.assertEquals(cr1.dataType, "List<Pet>");
Assert.assertTrue(cr1.isArray);
Assert.assertFalse(cr1.isModel);
Assert.assertFalse(cr1.isMap);
Assert.assertFalse(cr1.vendorExtensions.containsKey("x-java-is-response-void"));

CodegenResponse cr2 = co.responses.get(1);
Assert.assertEquals(cr2.code, "400");
Assert.assertEquals(cr2.baseType, "Void");
Assert.assertEquals(cr2.dataType, "void");
Assert.assertEquals(cr2.vendorExtensions.get("x-java-is-response-void"), Boolean.TRUE);
Assert.assertEquals(cr2.code, "201");
Assert.assertEquals(cr2.baseType, "Pet");
Assert.assertEquals(cr2.dataType, "Map<String, Pet>");
Assert.assertTrue(cr2.isMap);
Assert.assertFalse(cr2.isArray);
Assert.assertFalse(cr2.isModel);
Assert.assertFalse(cr2.vendorExtensions.containsKey("x-java-is-response-void"));

CodegenResponse cr3 = co.responses.get(2);
Assert.assertEquals(cr3.code, "400");
Assert.assertEquals(cr3.baseType, "Void");
Assert.assertEquals(cr3.dataType, "void");
Assert.assertEquals(cr3.vendorExtensions.get("x-java-is-response-void"), Boolean.TRUE);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,27 @@ public void testIsFreeFormObject() {
Assert.assertFalse(ModelUtils.isFreeFormObject(null, openAPI));
}

@Test(enabled = false)
public void testIsFreeFormObjectFromSpec() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7613.yaml");
// Create initial "empty" object schema.
Schema additionalPropertiesUnset = ModelUtils.getSchema(openAPI, "AdditionalPropertiesUnset");
Assert.assertTrue(ModelUtils.isFreeFormObject(additionalPropertiesUnset, openAPI));
Assert.assertFalse(ModelUtils.isMapSchema(additionalPropertiesUnset));

Schema additionalPropertiesTrue = ModelUtils.getSchema(openAPI, "AdditionalPropertiesTrue");
Assert.assertTrue(ModelUtils.isFreeFormObject(additionalPropertiesTrue, openAPI));
Assert.assertFalse(ModelUtils.isMapSchema(additionalPropertiesTrue));

Schema additionalPropertiesFalse = ModelUtils.getSchema(openAPI, "AdditionalPropertiesFalse");
Assert.assertTrue(ModelUtils.isFreeFormObject(additionalPropertiesFalse, openAPI));
Assert.assertFalse(ModelUtils.isMapSchema(additionalPropertiesFalse));

Schema asdditionalPropertiesSchema = ModelUtils.getSchema(openAPI, "AdditionalPropertiesSchema");
Assert.assertFalse(ModelUtils.isFreeFormObject(asdditionalPropertiesSchema, openAPI));
Assert.assertTrue(ModelUtils.isMapSchema(asdditionalPropertiesSchema));
}

@Test
public void testIsSetForValidSet() {
ArraySchema as = new ArraySchema()
Expand Down
Loading
Loading