Fix selection of unnecessary form parameter models (#16418)

* fix idea

* add unit test

* adjust warnings about form parameters
This commit is contained in:
martin-mfg 2023-09-06 09:23:04 +02:00 committed by GitHub
parent acb798b58b
commit 75ac4a2f58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 5 deletions

View File

@ -6928,6 +6928,13 @@ public class DefaultCodegen implements CodegenConfig {
LOGGER.debug("debugging fromRequestBodyToFormParameters= {}", body); LOGGER.debug("debugging fromRequestBodyToFormParameters= {}", body);
Schema schema = ModelUtils.getSchemaFromRequestBody(body); Schema schema = ModelUtils.getSchemaFromRequestBody(body);
schema = ModelUtils.getReferencedSchema(this.openAPI, schema); schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
if(ModelUtils.isMapSchema(schema)) {
LOGGER.error("Form parameters with additionalProperties are not supported by OpenAPI Generator. Please report the issue to https://github.com/openapitools/openapi-generator if you need help.");
}
if(ModelUtils.isArraySchema(schema)) {
LOGGER.error("Array form parameters are not supported by OpenAPI Generator. Please report the issue to https://github.com/openapitools/openapi-generator if you need help.");
}
List<String> allRequired = new ArrayList<>(); List<String> allRequired = new ArrayList<>();
Map<String, Schema> properties = new LinkedHashMap<>(); Map<String, Schema> properties = new LinkedHashMap<>();
// this traverses a composed schema and extracts all properties in each schema into properties // this traverses a composed schema and extracts all properties in each schema into properties
@ -6943,10 +6950,6 @@ public class DefaultCodegen implements CodegenConfig {
// value => property schema // value => property schema
String propertyName = entry.getKey(); String propertyName = entry.getKey();
Schema propertySchema = entry.getValue(); Schema propertySchema = entry.getValue();
if (ModelUtils.isMapSchema(propertySchema)) {
LOGGER.error("Map of form parameters not supported. Please report the issue to https://github.com/openapitools/openapi-generator if you need help.");
continue;
}
codegenParameter = fromFormProperty(propertyName, propertySchema, imports); codegenParameter = fromFormProperty(propertyName, propertySchema, imports);
// Set 'required' flag defined in the schema element // Set 'required' flag defined in the schema element

View File

@ -367,7 +367,7 @@ public class ModelUtils {
Map<String, Schema> properties = schema.getProperties(); Map<String, Schema> properties = schema.getProperties();
if (properties != null) { if (properties != null) {
for (Schema property : properties.values()) { for (Schema property : properties.values()) {
visitSchema(openAPI, property, mimeType, visitedSchemas, visitor); visitSchema(openAPI, property, null, visitedSchemas, visitor);
} }
} }
} }

View File

@ -113,6 +113,14 @@ public class ModelUtilsTest {
Assert.assertTrue(unusedSchemas.contains("SomeObj7"), "contains 'SomeObj7'"); Assert.assertTrue(unusedSchemas.contains("SomeObj7"), "contains 'SomeObj7'");
} }
@Test
public void testNestedFormParameter() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/nestedFormParameter.yaml");
List<String> unusedSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
Assert.assertEquals(unusedSchemas.size(), 1);
Assert.assertTrue(unusedSchemas.contains("OuterObject"), "contains 'OuterObject'");
}
@Test @Test
public void testNoComponentsSection() { public void testNoComponentsSection() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/ping.yaml"); final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/ping.yaml");

View File

@ -0,0 +1,28 @@
swagger: "2.0"
info:
title: dummy
version: dummy
paths:
/property:
post:
consumes:
- application/x-www-form-urlencoded
parameters:
- in: body
name: body
schema:
$ref: '#/definitions/OuterObject'
responses:
'200':
description: dummy
definitions:
InnerObject:
type: object
properties:
innerProperty:
type: string
OuterObject:
type: object
properties:
outerProperty:
$ref: '#/definitions/InnerObject'