Skip to content

Commit

Permalink
Merge branch 'fix_fastapi' of https://github.com/diorcety/openapi-gen…
Browse files Browse the repository at this point in the history
…erator into diorcety-fix_fastapi
  • Loading branch information
wing328 committed Oct 9, 2024
2 parents 627c0f4 + 1a31b05 commit 82aead0
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenResponse;
import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.GeneratorLanguage;
import org.openapitools.codegen.IJsonSchemaValidationProperties;
Expand Down Expand Up @@ -1264,6 +1265,20 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
);
}

// update typing import for operation responses type
for (CodegenResponse response : operation.responses) {
// Not interested in the result, only in the update of the imports
getPydanticType(
response.returnProperty,
modelImports,
exampleImports,
postponedModelImports,
postponedExampleImports,
moduleImports,
null
);
}

// add import for code samples
// import models one by one
if (!exampleImports.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public String getTypeDeclaration(Schema p) {

@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
super.postProcessOperationsWithModels(objs, allModels);

OperationMap operations = objs.getOperations();
// Set will make sure that no duplicated items are used.
Set<String> securityImports = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#isString}}str{{/isString}}{{#isInteger}}int{{/isInteger}}{{#isLong}}int{{/isLong}}{{#isFloat}}float{{/isFloat}}{{#isDouble}}float{{/isDouble}}{{#isByteArray}}str{{/isByteArray}}{{#isBinary}}str{{/isBinary}}{{#isBoolean}}bool{{/isBoolean}}{{#isDate}}str{{/isDate}}{{#isDateTime}}str{{/isDateTime}}{{#isModel}}{{dataType}}{{/isModel}}{{#isContainer}}{{dataType}}{{/isContainer}}
{{{vendorExtensions.x-py-typing}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.openapitools.codegen.python;

import com.google.common.collect.Sets;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.PythonClientCodegen;
import org.openapitools.codegen.languages.PythonFastAPIServerCodegen;
import org.openapitools.codegen.languages.features.CXFServerFeatures;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import static org.openapitools.codegen.TestUtils.assertFileContains;
import static org.openapitools.codegen.TestUtils.assertFileExists;

public class PythonFastAPIServerCodegenTest {

// Helper function, intended to reduce boilerplate
static private String generateFiles(DefaultCodegen codegen, String filePath) throws IOException {
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
final String outputPath = output.getAbsolutePath().replace('\\', '/');

codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");

final ClientOptInput input = new ClientOptInput();
final OpenAPI openAPI = new OpenAPIParser().readLocation(filePath, null, new ParseOptions()).getOpenAPI();
input.openAPI(openAPI);
input.config(codegen);

final DefaultGenerator generator = new DefaultGenerator();
final List<File> files = generator.opts(input).generate();

Assert.assertTrue(files.size() > 0);
return outputPath + "/";
}


@Test(description = "test containerType in parameters")
public void testContainerType() throws IOException {
final DefaultCodegen codegen = new PythonFastAPIServerCodegen();
final String outputPath = generateFiles(codegen, "src/test/resources/bugs/pr_18691.json");
final Path p = Paths.get(outputPath + "src/openapi_server/apis/default_api.py");

assertFileExists(p);
assertFileContains(p, "body: Optional[Dict[str, Any]] = Body(None, description=\"\"),");
}
}
24 changes: 24 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/pr_18691.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"paths": {
"/licensing/token/renew": {
"post": {
"description": "Manually ask license issuer for a new token. Available in Grafana Enterprise v7.4+.\n\nYou need to have a permission with action `licensing:update`.",
"operationId": "postRenewLicenseToken",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
}
}
}
}

0 comments on commit 82aead0

Please sign in to comment.