Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a few issues with the C generator #14379

Closed
wants to merge 19 commits into from
Closed
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.servers.Server;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
Expand Down Expand Up @@ -326,6 +329,7 @@ public void processOpts() {
// root folder
supportingFiles.add(new SupportingFile("CMakeLists.txt.mustache", "", "CMakeLists.txt"));
supportingFiles.add(new SupportingFile("Packing.cmake.mustache", "", "Packing.cmake"));
supportingFiles.add(new SupportingFile("cmake-config.mustache", "", "Config.cmake.in"));
supportingFiles.add(new SupportingFile("libcurl.licence.mustache", "", "libcurl.licence"));
supportingFiles.add(new SupportingFile("uncrustify-rules.cfg.mustache", "", "uncrustify-rules.cfg"));
supportingFiles.add(new SupportingFile("README.md.mustache", "", "README.md"));
Expand All @@ -347,6 +351,7 @@ public void processOpts() {
// Object files in model folder
supportingFiles.add(new SupportingFile("object-body.mustache", "model", "object.c"));
supportingFiles.add(new SupportingFile("object-header.mustache", "model", "object.h"));
supportingFiles.add(new SupportingFile("any_type-header.mustache", "model", "any_type.h"));
}

@Override
Expand Down Expand Up @@ -908,6 +913,145 @@ public void postProcessFile(File file, String fileType) {
}
}

@Override
public ExtendedCodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
CodegenOperation superOp = super.fromOperation(path, httpMethod, operation, servers);
ExtendedCodegenOperation op = new ExtendedCodegenOperation(superOp);

ApiResponse methodResponse = findMethodResponse(operation.getResponses());
if (methodResponse != null) {
Map<String, Schema> schemas = ModelUtils.getSchemas(this.openAPI);
Schema schema = null;
if (schemas != null) {
schema = schemas.get(op.returnBaseType);
}

Schema responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(methodResponse));
if (responseSchema != null) {
CodegenProperty cp = fromProperty("response", responseSchema);
if (cp != null) {
op.returnTypeIsEnum = cp.isEnum;
op.returnTypeIsBinary = cp.isBinary;
op.returnTypeIsString = cp.isString;
op.returnTypeIsInteger = cp.isInteger;
}
}
}

return op;
}

class ExtendedCodegenOperation extends CodegenOperation {
wing328 marked this conversation as resolved.
Show resolved Hide resolved
boolean returnTypeIsEnum;
boolean returnTypeIsBinary;
boolean returnTypeIsString;
boolean returnTypeIsInteger;

public ExtendedCodegenOperation(CodegenOperation o) {
super();

this.responseHeaders.addAll(o.responseHeaders);
this.hasAuthMethods = o.hasAuthMethods;
this.hasConsumes = o.hasConsumes;
this.hasProduces = o.hasProduces;
this.hasParams = o.hasParams;
this.hasOptionalParams = o.hasOptionalParams;
this.hasRequiredParams = o.hasRequiredParams;
this.returnTypeIsPrimitive = o.returnTypeIsPrimitive;
this.returnSimpleType = o.returnSimpleType;
this.subresourceOperation = o.subresourceOperation;
this.isMap = o.isMap;
this.isArray = o.isArray;
this.isMultipart = o.isMultipart;
this.isResponseBinary = o.isResponseBinary;
this.isResponseFile = o.isResponseFile;
this.hasReference = o.hasReference;
this.isRestfulIndex = o.isRestfulIndex;
this.isRestfulShow = o.isRestfulShow;
this.isRestfulCreate = o.isRestfulCreate;
this.isRestfulUpdate = o.isRestfulUpdate;
this.isRestfulDestroy = o.isRestfulDestroy;
this.isRestful = o.isRestful;
this.isDeprecated = o.isDeprecated;
this.isCallbackRequest = o.isCallbackRequest;
this.uniqueItems = o.uniqueItems;
this.path = o.path;
this.operationId = o.operationId;
this.returnType = o.returnType;
this.returnFormat = o.returnFormat;
this.httpMethod = o.httpMethod;
this.returnBaseType = o.returnBaseType;
this.returnContainer = o.returnContainer;
this.summary = o.summary;
this.unescapedNotes = o.unescapedNotes;
this.notes = o.notes;
this.baseName = o.baseName;
this.defaultResponse = o.defaultResponse;
this.discriminator = o.discriminator;
this.consumes = o.consumes;
this.produces = o.produces;
this.prioritizedContentTypes = o.prioritizedContentTypes;
this.servers = o.servers;
this.bodyParam = o.bodyParam;
this.allParams = o.allParams;
this.bodyParams = o.bodyParams;
this.pathParams = o.pathParams;
this.queryParams = o.queryParams;
this.headerParams = o.headerParams;
this.formParams = o.formParams;
this.cookieParams = o.cookieParams;
this.requiredParams = o.requiredParams;
this.optionalParams = o.optionalParams;
this.authMethods = o.authMethods;
this.tags = o.tags;
this.responses = o.responses;
this.callbacks = o.callbacks;
this.imports = o.imports;
this.examples = o.examples;
this.requestBodyExamples = o.requestBodyExamples;
this.externalDocs = o.externalDocs;
this.vendorExtensions = o.vendorExtensions;
this.nickname = o.nickname;
this.operationIdOriginal = o.operationIdOriginal;
this.operationIdLowerCase = o.operationIdLowerCase;
this.operationIdCamelCase = o.operationIdCamelCase;
this.operationIdSnakeCase = o.operationIdSnakeCase;
}

@Override
public boolean equals(Object o) {
if (o == null)
return false;

if (this.getClass() != o.getClass())
return false;

boolean result = super.equals(o);
ExtendedCodegenOperation that = (ExtendedCodegenOperation) o;
return result && returnTypeIsEnum == that.returnTypeIsEnum &&
returnTypeIsBinary == that.returnTypeIsBinary &&
returnTypeIsString == that.returnTypeIsString &&
returnTypeIsInteger == that.returnTypeIsInteger;
}

@Override
public int hashCode() {
int superHash = super.hashCode();
return Objects.hash(superHash, returnTypeIsEnum, returnTypeIsBinary, returnTypeIsString, returnTypeIsInteger);
}

@Override
public String toString() {
String superString = super.toString();
final StringBuilder sb = new StringBuilder(superString);
sb.append(", returnTypeIsEnum=").append(returnTypeIsEnum);
sb.append(", returnTypeIsBinary=").append(returnTypeIsBinary);
sb.append(", returnTypeIsString=").append(returnTypeIsString);
sb.append(", returnTypeIsInteger=").append(returnTypeIsInteger);
return sb.toString();
}
}

@Override
public void postProcess() {
System.out.println("################################################################################");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
cmake_minimum_required (VERSION 2.6...3.10.2)
project (CGenerator)
project (CGenerator C)

cmake_policy(SET CMP0063 NEW)

set(CMAKE_C_VISIBILITY_PRESET default)
set(CMAKE_CXX_VISIBILITY_PRESET default)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some developers use a C++ compiler to compile this project. Will this change break it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can discuss this in your new PR. Just putting it here for your consideration.

set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

Expand Down Expand Up @@ -76,6 +75,7 @@ set(HDRS
include/keyValuePair.h
external/cJSON.h
model/object.h
model/any_type.h
{{#models}}
{{#model}}
model/{{classname}}.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* any_type.h
*
* A placeholder for now, this type isn't really needed.
*/
Loading