DefaultGenerator: ignore only form param schemas (#74)

* Rewrite ModelUtils.getUnusedSchemas(OpenAPI)
* Add ModelUtils.getAllUnusedSchemas(OpenAPI)
* Add ModelUtils.getSchemasUsedOnlyInFormParam(OpenAPI)
This commit is contained in:
Jérémie Bresson 2018-05-24 17:07:41 +02:00 committed by GitHub
parent a3aabd3908
commit 76b7307a6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 553 additions and 33 deletions

View File

@ -778,9 +778,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
// models // models
List<String> unusedSchemas = ModelUtils.getUnusedSchemas(openAPI); List<String> filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
List<Object> allModels = new ArrayList<Object>(); List<Object> allModels = new ArrayList<Object>();
generateModels(files, allModels, unusedSchemas); generateModels(files, allModels, filteredSchemas);
// apis // apis
List<Object> allOperations = new ArrayList<Object>(); List<Object> allOperations = new ArrayList<Object>();
generateApis(files, allOperations, allModels); generateApis(files, allOperations, allModels);

View File

@ -39,6 +39,7 @@ import io.swagger.v3.oas.models.media.PasswordSchema;
import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema; import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.media.UUIDSchema; import io.swagger.v3.oas.models.media.UUIDSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody; import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.parser.util.SchemaTypeUtil; import io.swagger.v3.parser.util.SchemaTypeUtil;
@ -52,6 +53,8 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
public class ModelUtils { public class ModelUtils {
@ -85,40 +88,113 @@ public class ModelUtils {
return null; return null;
} }
/**
* Return the list of all schemas in the 'components/schemas' section used in the openAPI specification
* @param openAPI specification
* @return schemas a list of used schemas
*/
public static List<String> getAllUsedSchemas(OpenAPI openAPI) {
List<String> allUsedSchemas = new ArrayList<String>();
visitOpenAPI(openAPI, (s, t) -> {
if(s.get$ref() != null) {
String ref = getSimpleRef(s.get$ref());
if(!allUsedSchemas.contains(ref)) {
allUsedSchemas.add(ref);
}
}
});
return allUsedSchemas;
}
/**
* Return the list of unused schemas in the 'components/schemas' section of an openAPI specification
* @param openAPI specification
* @return schemas a list of unused schemas
*/
public static List<String> getUnusedSchemas(OpenAPI openAPI) { public static List<String> getUnusedSchemas(OpenAPI openAPI) {
List<String> unusedSchemas = new ArrayList<String>(); List<String> unusedSchemas = new ArrayList<String>();
// operations
Map<String, PathItem> paths = openAPI.getPaths();
Map<String, Schema> schemas = getSchemas(openAPI); Map<String, Schema> schemas = getSchemas(openAPI);
unusedSchemas.addAll(schemas.keySet());
visitOpenAPI(openAPI, (s, t) -> {
if(s.get$ref() != null) {
unusedSchemas.remove(getSimpleRef(s.get$ref()));
}
});
return unusedSchemas;
}
/**
* Return the list of schemas in the 'components/schemas' used only in a 'application/x-www-form-urlencoded' or 'multipart/form-data' mime time
* @param openAPI specification
* @return schemas a list of schemas
*/
public static List<String> getSchemasUsedOnlyInFormParam(OpenAPI openAPI) {
List<String> schemasUsedInFormParam = new ArrayList<String>();
List<String> schemasUsedInOtherCases = new ArrayList<String>();
visitOpenAPI(openAPI, (s, t) -> {
if(s.get$ref() != null) {
String ref = getSimpleRef(s.get$ref());
if ("application/x-www-form-urlencoded".equalsIgnoreCase(t) ||
"multipart/form-data".equalsIgnoreCase(t)) {
schemasUsedInFormParam.add(ref);
} else {
schemasUsedInOtherCases.add(ref);
}
}
});
return schemasUsedInFormParam.stream().filter(n -> !schemasUsedInOtherCases.contains(n)).collect(Collectors.toList());
}
/**
* Private method used by several methods ({@link #getAllUsedSchemas(OpenAPI)},
* {@link #getUnusedSchemas(OpenAPI)},
* {@link #getSchemasUsedOnlyInFormParam(OpenAPI)}, ...) to traverse all paths of an
* OpenAPI instance and call the visitor functional interface when a schema is found.
*
* @param openAPI specification
* @param visitor functional interface (can be defined as a lambda) called each time a schema is found.
*/
private static void visitOpenAPI(OpenAPI openAPI, OpenAPISchemaVisitor visitor) {
Map<String, PathItem> paths = openAPI.getPaths();
if (paths != null) { if (paths != null) {
for (String pathname : paths.keySet()) { for (PathItem path : paths.values()) {
PathItem path = paths.get(pathname); List<Operation> allOperations = path.readOperations();
Map<PathItem.HttpMethod, Operation> operationMap = path.readOperationsMap(); if (allOperations != null) {
if (operationMap != null) { for (Operation operation : allOperations) {
for (PathItem.HttpMethod method : operationMap.keySet()) { //Params:
Operation operation = operationMap.get(method); if(operation.getParameters() != null) {
RequestBody requestBody = operation.getRequestBody(); for (Parameter p : operation.getParameters()) {
Parameter parameter = getReferencedParameter(openAPI, p);
if (requestBody == null) { if (parameter.getSchema() != null) {
continue; visitor.visit(parameter.getSchema(), null);
}
}
} }
//LOGGER.info("debugging resolver: " + requestBody.toString()); //RequestBody:
if (requestBody.getContent() == null) { RequestBody requestBody = getReferencedRequestBody(openAPI, operation.getRequestBody());
continue; if (requestBody != null && requestBody.getContent() != null) {
for (Entry<String, MediaType> e : requestBody.getContent().entrySet()) {
if (e.getValue().getSchema() != null) {
visitor.visit(e.getValue().getSchema(), e.getKey());
}
}
} }
// go through "content" //Responses:
for (String mimeType : requestBody.getContent().keySet()) { if(operation.getResponses() != null) {
if ("application/x-www-form-urlencoded".equalsIgnoreCase(mimeType) || for (ApiResponse r : operation.getResponses().values()) {
"multipart/form-data".equalsIgnoreCase(mimeType)) { ApiResponse apiResponse = getReferencedApiResponse(openAPI, r);
// remove the schema that's automatically created by the parser if (apiResponse != null && apiResponse.getContent() != null) {
MediaType mediaType = requestBody.getContent().get(mimeType); for (Entry<String, MediaType> e : apiResponse.getContent().entrySet()) {
if (mediaType.getSchema().get$ref() != null) { if (e.getValue().getSchema() != null) {
LOGGER.debug("mark schema (form parameters) as unused: " + getSimpleRef(mediaType.getSchema().get$ref())); visitor.visit(e.getValue().getSchema(), e.getKey());
unusedSchemas.add(getSimpleRef(mediaType.getSchema().get$ref())); }
}
} }
} }
} }
@ -126,8 +202,12 @@ public class ModelUtils {
} }
} }
} }
}
return unusedSchemas; @FunctionalInterface
private static interface OpenAPISchemaVisitor {
public void visit(Schema schema, String mimeType);
} }
public static String getSimpleRef(String ref) { public static String getSimpleRef(String ref) {
@ -344,7 +424,7 @@ public class ModelUtils {
/** /**
* If a Schema contains a reference to an other Schema with '$ref', returns the referenced Schema or the actual Schema in the other cases. * If a Schema contains a reference to an other Schema with '$ref', returns the referenced Schema or the actual Schema in the other cases.
* @param openAPI * @param openAPI specification being checked
* @param schema potentially containing a '$ref' * @param schema potentially containing a '$ref'
* @return schema without '$ref' * @return schema without '$ref'
*/ */
@ -373,7 +453,7 @@ public class ModelUtils {
/** /**
* If a RequestBody contains a reference to an other RequestBody with '$ref', returns the referenced RequestBody or the actual RequestBody in the other cases. * If a RequestBody contains a reference to an other RequestBody with '$ref', returns the referenced RequestBody or the actual RequestBody in the other cases.
* @param openAPI * @param openAPI specification being checked
* @param requestBody potentially containing a '$ref' * @param requestBody potentially containing a '$ref'
* @return requestBody without '$ref' * @return requestBody without '$ref'
*/ */
@ -398,7 +478,7 @@ public class ModelUtils {
/** /**
* If a ApiResponse contains a reference to an other ApiResponse with '$ref', returns the referenced ApiResponse or the actual ApiResponse in the other cases. * If a ApiResponse contains a reference to an other ApiResponse with '$ref', returns the referenced ApiResponse or the actual ApiResponse in the other cases.
* @param openAPI * @param openAPI specification being checked
* @param apiResponse potentially containing a '$ref' * @param apiResponse potentially containing a '$ref'
* @return apiResponse without '$ref' * @return apiResponse without '$ref'
*/ */
@ -420,12 +500,46 @@ public class ModelUtils {
} }
return null; return null;
} }
/**
* If a Parameter contains a reference to an other Parameter with '$ref', returns the referenced Parameter or the actual Parameter in the other cases.
* @param openAPI specification being checked
* @param parameter potentially containing a '$ref'
* @return parameter without '$ref'
*/
public static Parameter getReferencedParameter(OpenAPI openAPI, Parameter parameter) {
if (parameter != null && StringUtils.isNotEmpty(parameter.get$ref())) {
String name = getSimpleRef(parameter.get$ref());
return getParameter(openAPI, name);
}
return parameter;
}
public static Parameter getParameter(OpenAPI openAPI, String name) {
if (name == null) {
return null;
}
if (openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getRequestBodies() != null) {
return openAPI.getComponents().getParameters().get(name);
}
return null;
}
/**
* Return the first defined Schema for a RequestBody
* @param requestBody request body of the operation
* @return firstSchema
*/
public static Schema getSchemaFromRequestBody(RequestBody requestBody) { public static Schema getSchemaFromRequestBody(RequestBody requestBody) {
return getSchemaFromContent(requestBody.getContent()); return getSchemaFromContent(requestBody.getContent());
} }
/**
* Return the first defined Schema for a ApiResponse
* @param response api response of the operation
* @return firstSchema
*/
public static Schema getSchemaFromResponse(ApiResponse response) { public static Schema getSchemaFromResponse(ApiResponse response) {
return getSchemaFromContent(response.getContent()); return getSchemaFromContent(response.getContent());
} }
@ -434,6 +548,9 @@ public class ModelUtils {
if (content == null || content.isEmpty()) { if (content == null || content.isEmpty()) {
return null; return null;
} }
if(content.size() > 1) {
LOGGER.warn("Multiple schemas found, returning only the first one");
}
MediaType mediaType = content.values().iterator().next(); MediaType mediaType = content.values().iterator().next();
return mediaType.getSchema(); return mediaType.getSchema();
} }

View File

@ -29,9 +29,64 @@ import java.util.List;
public class ModelUtilsTest { public class ModelUtilsTest {
@Test @Test
public void testEnsureNoDuplicateProduces() { public void testGetAllUsedSchemas() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/ping.yaml", null, new ParseOptions()).getOpenAPI(); final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/unusedSchemas.yaml", null, new ParseOptions()).getOpenAPI();
List<String> allUsedSchemas = ModelUtils.getAllUsedSchemas(openAPI);
Assert.assertEquals(allUsedSchemas.size(), 12);
Assert.assertTrue(allUsedSchemas.contains("SomeObjShared"), "contains 'SomeObjShared'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj1"), "contains 'UnusedObj1'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj2"), "contains 'SomeObj2'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj3"), "contains 'SomeObj3'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj6"), "contains 'SomeObj6'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj7"), "contains 'SomeObj7'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj8"), "contains 'SomeObj8'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj9A"), "contains 'SomeObj9A'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj9B"), "contains 'SomeObj9B'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj10A"), "contains 'SomeObj10A'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj10B"), "contains 'SomeObj10B'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj11"), "contains 'SomeObj11'");
}
@Test
public void testGetUnusedSchemas() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/unusedSchemas.yaml", null, new ParseOptions()).getOpenAPI();
List<String> unusedSchemas = ModelUtils.getUnusedSchemas(openAPI); List<String> unusedSchemas = ModelUtils.getUnusedSchemas(openAPI);
Assert.assertEquals(unusedSchemas.size(), 4);
//UnusedObj is not used at all:
Assert.assertTrue(unusedSchemas.contains("UnusedObj1"), "contains 'UnusedObj1'");
//SomeObjUnused is used in a request body that is not used.
Assert.assertTrue(unusedSchemas.contains("UnusedObj2"), "contains 'UnusedObj2'");
//SomeObjUnused is used in a response that is not used.
Assert.assertTrue(unusedSchemas.contains("UnusedObj3"), "contains 'UnusedObj3'");
//SomeObjUnused is used in a parameter that is not used.
Assert.assertTrue(unusedSchemas.contains("UnusedObj4"), "contains 'UnusedObj4'");
}
@Test
public void testSchemasUsedOnlyInFormParam() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/unusedSchemas.yaml", null, new ParseOptions()).getOpenAPI();
List<String> unusedSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
Assert.assertEquals(unusedSchemas.size(), 3);
//SomeObj2 is only used in a 'application/x-www-form-urlencoded' request
Assert.assertTrue(unusedSchemas.contains("SomeObj2"), "contains 'SomeObj2'");
//SomeObj3 is only used in a 'multipart/form-data' request
Assert.assertTrue(unusedSchemas.contains("SomeObj3"), "contains 'SomeObj3'");
//SomeObj7 is only used in a 'application/x-www-form-urlencoded' request (with referenced request body)
Assert.assertTrue(unusedSchemas.contains("SomeObj7"), "contains 'SomeObj7'");
}
@Test
public void testNoComponentsSection() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/ping.yaml", null, new ParseOptions()).getOpenAPI();
List<String> unusedSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
Assert.assertEquals(unusedSchemas.size(), 0);
}
@Test
public void testGlobalProducesConsumes() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/globalProducesConsumesTest.yaml", null, new ParseOptions()).getOpenAPI();
List<String> unusedSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
Assert.assertEquals(unusedSchemas.size(), 0); Assert.assertEquals(unusedSchemas.size(), 0);
} }
} }

View File

@ -0,0 +1,39 @@
swagger: '2.0'
info:
title: Test
description: Test API
version: 1.0.0
host: some.example.com
basePath: /v1
schemes:
- https
- http
consumes:
- application/json
- application/x-www-form-urlencoded
produces:
- application/json
paths:
/testMe:
post:
tags:
- db
operationId: testMeOp
parameters:
- in: body
name: body
required: false
schema:
$ref: '#/definitions/SomeObject'
responses:
'200':
description: Successful Operation
definitions:
SomeObject:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32

View File

@ -0,0 +1,309 @@
openapi: 3.0.1
info:
title: Test
description: Test API
version: 1.0.0
servers:
- url: http://some.example.com/v1
paths:
/some/p1:
post:
operationId: p1
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/SomeObj1'
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/SomeObj1'
required: false
responses:
200:
description: Successful Operation
content: {}
/some/p2:
post:
operationId: p2
requestBody:
content:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/SomeObj2'
required: false
responses:
200:
description: Successful Operation
content: {}
/some/p3:
post:
operationId: p3
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/SomeObj3'
required: false
responses:
200:
description: Successful Operation
content: {}
/some/p4:
post:
operationId: p4
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/SomeObjShared'
required: false
responses:
200:
description: Successful Operation
content: {}
/some/p5:
post:
operationId: p5
requestBody:
content:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/SomeObjShared'
required: false
responses:
200:
description: Successful Operation
content: {}
/some/p6:
post:
operationId: p6
requestBody:
$ref: '#/components/requestBodies/Op6RequestBody'
responses:
200:
description: Successful Operation
content: {}
/some/p7:
post:
operationId: p7
requestBody:
$ref: '#/components/requestBodies/Op7RequestBody'
responses:
200:
description: Successful Operation
content: {}
/some/p8:
post:
operationId: p8
responses:
200:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/SomeObj8'
/some/p9:
post:
operationId: p9
responses:
200:
$ref: '#/components/responses/Rep9'
/some/p10/{someParam}:
post:
operationId: p10
parameters:
- name: status
in: query
schema:
$ref: '#/components/schemas/SomeObj10A'
- name: someParam
in: path
description: selected value
required: true
schema:
$ref: '#/components/schemas/SomeObj10B'
responses:
200:
description: Successful Operation
content: {}
/some/p11:
post:
operationId: p11
parameters:
- $ref: '#/components/parameters/QueryParam11'
responses:
200:
description: Successful Operation
content: {}
components:
schemas:
UnusedObj1:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
UnusedObj2:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
UnusedObj3:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
UnusedObj4:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
SomeObj1:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
SomeObj2:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
SomeObj3:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
SomeObj6:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
SomeObj7:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
SomeObj8:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
SomeObj9A:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
SomeObj9B:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
SomeObj10A:
type: array
items:
type: string
enum:
- available
- pending
- sold
default: available
SomeObj10B:
type: string
enum:
- v1
- v2
default: v1
SomeObj11:
type: string
enum:
- v1
- v2
default: v1
SomeObjShared:
type: object
properties:
p1:
type: string
p2:
type: integer
format: int32
requestBodies:
UnusedRequestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/UnusedObj2'
required: false
Op6RequestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/SomeObj6'
required: false
Op7RequestBody:
content:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/SomeObj7'
responses:
UnusedResponse:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/UnusedObj3'
Rep9:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/SomeObj9A'
application/xml:
schema:
$ref: '#/components/schemas/SomeObj9B'
parameters:
UnusedParam:
name: id
in: query
schema:
$ref: '#/components/schemas/UnusedObj4'
QueryParam11:
name: id
in: query
schema:
$ref: '#/components/schemas/SomeObj11'