mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-04 06:30:52 +00:00
Refactor InlineModelResolver (#1788)
* Extract a method "flattenPaths" to reduce the scope of method * Tweak * Rename parameter name * Extract a method "flattenModels" to reduce the scope of method * Rename parameter name * Rename: models -> components * Delete comment * Extract a method "flattenRequestBody" to reduce the scope of method * Extract a method "flattenParameters" to reduce the scope of method * Extract a method "flattenResponses" to reduce the scope of method * Tweak types * Reduce indentation
This commit is contained in:
parent
9334dd391a
commit
af6757ccde
@ -17,18 +17,16 @@
|
|||||||
|
|
||||||
package org.openapitools.codegen;
|
package org.openapitools.codegen;
|
||||||
|
|
||||||
import io.swagger.v3.oas.models.Components;
|
import io.swagger.v3.oas.models.*;
|
||||||
import io.swagger.v3.oas.models.OpenAPI;
|
|
||||||
import io.swagger.v3.oas.models.media.Schema;
|
import io.swagger.v3.oas.models.media.Schema;
|
||||||
import io.swagger.v3.oas.models.media.ArraySchema;
|
import io.swagger.v3.oas.models.media.ArraySchema;
|
||||||
import io.swagger.v3.oas.models.media.ObjectSchema;
|
import io.swagger.v3.oas.models.media.ObjectSchema;
|
||||||
import io.swagger.v3.oas.models.media.*;
|
import io.swagger.v3.oas.models.media.*;
|
||||||
import io.swagger.v3.oas.models.responses.ApiResponse;
|
import io.swagger.v3.oas.models.responses.ApiResponse;
|
||||||
import io.swagger.v3.oas.models.Operation;
|
|
||||||
import io.swagger.v3.oas.models.PathItem;
|
|
||||||
import io.swagger.v3.oas.models.parameters.Parameter;
|
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.core.util.Json;
|
import io.swagger.v3.core.util.Json;
|
||||||
|
import io.swagger.v3.oas.models.responses.ApiResponses;
|
||||||
import org.openapitools.codegen.utils.ModelUtils;
|
import org.openapitools.codegen.utils.ModelUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -56,15 +54,45 @@ public class InlineModelResolver {
|
|||||||
if (openapi.getComponents().getSchemas() == null) {
|
if (openapi.getComponents().getSchemas() == null) {
|
||||||
openapi.getComponents().setSchemas(new HashMap<String, Schema>());
|
openapi.getComponents().setSchemas(new HashMap<String, Schema>());
|
||||||
}
|
}
|
||||||
// operations
|
|
||||||
Map<String, PathItem> paths = openapi.getPaths();
|
flattenPaths(openapi);
|
||||||
Map<String, Schema> models = openapi.getComponents().getSchemas();
|
flattenComponents(openapi);
|
||||||
if (paths != null) {
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flatten inline models in Paths
|
||||||
|
*
|
||||||
|
* @param openAPI target spec
|
||||||
|
*/
|
||||||
|
private void flattenPaths(OpenAPI openAPI) {
|
||||||
|
Paths paths = openAPI.getPaths();
|
||||||
|
if (paths == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (String pathname : paths.keySet()) {
|
for (String pathname : paths.keySet()) {
|
||||||
PathItem path = paths.get(pathname);
|
PathItem path = paths.get(pathname);
|
||||||
for (Operation operation : path.readOperations()) {
|
for (Operation operation : path.readOperations()) {
|
||||||
|
flattenRequestBody(openAPI, pathname, operation);
|
||||||
|
flattenParameters(openAPI, pathname, operation);
|
||||||
|
flattenResponses(openAPI, pathname, operation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flatten inline models in RequestBody
|
||||||
|
*
|
||||||
|
* @param openAPI target spec
|
||||||
|
* @param pathname target pathname
|
||||||
|
* @param operation target operation
|
||||||
|
*/
|
||||||
|
private void flattenRequestBody(OpenAPI openAPI, String pathname, Operation operation) {
|
||||||
RequestBody requestBody = operation.getRequestBody();
|
RequestBody requestBody = operation.getRequestBody();
|
||||||
if (requestBody != null) {
|
if (requestBody == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Schema model = ModelUtils.getSchemaFromRequestBody(requestBody);
|
Schema model = ModelUtils.getSchemaFromRequestBody(requestBody);
|
||||||
if (model instanceof ObjectSchema) {
|
if (model instanceof ObjectSchema) {
|
||||||
Schema obj = (Schema) model;
|
Schema obj = (Schema) model;
|
||||||
@ -74,7 +102,7 @@ public class InlineModelResolver {
|
|||||||
// for model name, use "title" if defined, otherwise default to 'inline_object'
|
// for model name, use "title" if defined, otherwise default to 'inline_object'
|
||||||
String modelName = resolveModelName(obj.getTitle(), "inline_object");
|
String modelName = resolveModelName(obj.getTitle(), "inline_object");
|
||||||
addGenerated(modelName, model);
|
addGenerated(modelName, model);
|
||||||
openapi.getComponents().addSchemas(modelName, model);
|
openAPI.getComponents().addSchemas(modelName, model);
|
||||||
|
|
||||||
// create request body
|
// create request body
|
||||||
RequestBody rb = new RequestBody();
|
RequestBody rb = new RequestBody();
|
||||||
@ -101,12 +129,12 @@ public class InlineModelResolver {
|
|||||||
rb.setContent(content);
|
rb.setContent(content);
|
||||||
|
|
||||||
// add to openapi "components"
|
// add to openapi "components"
|
||||||
if (openapi.getComponents().getRequestBodies() == null) {
|
if (openAPI.getComponents().getRequestBodies() == null) {
|
||||||
Map<String, RequestBody> requestBodies = new HashMap<String, RequestBody>();
|
Map<String, RequestBody> requestBodies = new HashMap<String, RequestBody>();
|
||||||
requestBodies.put(modelName, rb);
|
requestBodies.put(modelName, rb);
|
||||||
openapi.getComponents().setRequestBodies(requestBodies);
|
openAPI.getComponents().setRequestBodies(requestBodies);
|
||||||
} else {
|
} else {
|
||||||
openapi.getComponents().getRequestBodies().put(modelName, rb);
|
openAPI.getComponents().getRequestBodies().put(modelName, rb);
|
||||||
}
|
}
|
||||||
|
|
||||||
// update requestBody to use $ref instead of inline def
|
// update requestBody to use $ref instead of inline def
|
||||||
@ -133,17 +161,31 @@ public class InlineModelResolver {
|
|||||||
schema.setRequired(op.getRequired());
|
schema.setRequired(op.getRequired());
|
||||||
am.setItems(schema);
|
am.setItems(schema);
|
||||||
addGenerated(modelName, innerModel);
|
addGenerated(modelName, innerModel);
|
||||||
openapi.getComponents().addSchemas(modelName, innerModel);
|
openAPI.getComponents().addSchemas(modelName, innerModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flatten inline models in parameters
|
||||||
|
*
|
||||||
|
* @param openAPI target spec
|
||||||
|
* @param pathname target pathname
|
||||||
|
* @param operation target operation
|
||||||
|
*/
|
||||||
|
private void flattenParameters(OpenAPI openAPI, String pathname, Operation operation) {
|
||||||
List<Parameter> parameters = operation.getParameters();
|
List<Parameter> parameters = operation.getParameters();
|
||||||
if (parameters != null) {
|
if (parameters == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (Parameter parameter : parameters) {
|
for (Parameter parameter : parameters) {
|
||||||
if (parameter.getSchema() != null) {
|
if (parameter.getSchema() == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Schema model = parameter.getSchema();
|
Schema model = parameter.getSchema();
|
||||||
if (model instanceof ObjectSchema) {
|
if (model instanceof ObjectSchema) {
|
||||||
Schema obj = (Schema) model;
|
Schema obj = (Schema) model;
|
||||||
@ -154,7 +196,7 @@ public class InlineModelResolver {
|
|||||||
|
|
||||||
parameter.$ref(modelName);
|
parameter.$ref(modelName);
|
||||||
addGenerated(modelName, model);
|
addGenerated(modelName, model);
|
||||||
openapi.getComponents().addSchemas(modelName, model);
|
openAPI.getComponents().addSchemas(modelName, model);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (model instanceof ArraySchema) {
|
} else if (model instanceof ArraySchema) {
|
||||||
@ -176,19 +218,33 @@ public class InlineModelResolver {
|
|||||||
schema.setRequired(op.getRequired());
|
schema.setRequired(op.getRequired());
|
||||||
am.setItems(schema);
|
am.setItems(schema);
|
||||||
addGenerated(modelName, innerModel);
|
addGenerated(modelName, innerModel);
|
||||||
openapi.getComponents().addSchemas(modelName, innerModel);
|
openAPI.getComponents().addSchemas(modelName, innerModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flatten inline models in ApiResponses
|
||||||
|
*
|
||||||
|
* @param openAPI target spec
|
||||||
|
* @param pathname target pathname
|
||||||
|
* @param operation target operation
|
||||||
|
*/
|
||||||
|
private void flattenResponses(OpenAPI openAPI, String pathname, Operation operation) {
|
||||||
|
ApiResponses responses = operation.getResponses();
|
||||||
|
if (responses == null) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
Map<String, ApiResponse> responses = operation.getResponses();
|
|
||||||
if (responses != null) {
|
|
||||||
for (String key : responses.keySet()) {
|
for (String key : responses.keySet()) {
|
||||||
ApiResponse response = responses.get(key);
|
ApiResponse response = responses.get(key);
|
||||||
if (ModelUtils.getSchemaFromResponse(response) != null) {
|
if (ModelUtils.getSchemaFromResponse(response) == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Schema property = ModelUtils.getSchemaFromResponse(response);
|
Schema property = ModelUtils.getSchemaFromResponse(response);
|
||||||
if (property instanceof ObjectSchema) {
|
if (property instanceof ObjectSchema) {
|
||||||
ObjectSchema op = (ObjectSchema) property;
|
ObjectSchema op = (ObjectSchema) property;
|
||||||
@ -207,7 +263,7 @@ public class InlineModelResolver {
|
|||||||
schema.setRequired(op.getRequired());
|
schema.setRequired(op.getRequired());
|
||||||
mediaType.setSchema(schema);
|
mediaType.setSchema(schema);
|
||||||
addGenerated(modelName, model);
|
addGenerated(modelName, model);
|
||||||
openapi.getComponents().addSchemas(modelName, model);
|
openAPI.getComponents().addSchemas(modelName, model);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -231,7 +287,7 @@ public class InlineModelResolver {
|
|||||||
schema.setRequired(op.getRequired());
|
schema.setRequired(op.getRequired());
|
||||||
ap.setItems(schema);
|
ap.setItems(schema);
|
||||||
addGenerated(modelName, innerModel);
|
addGenerated(modelName, innerModel);
|
||||||
openapi.getComponents().addSchemas(modelName, innerModel);
|
openAPI.getComponents().addSchemas(modelName, innerModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -255,19 +311,25 @@ public class InlineModelResolver {
|
|||||||
schema.setRequired(op.getRequired());
|
schema.setRequired(op.getRequired());
|
||||||
mp.setAdditionalProperties(schema);
|
mp.setAdditionalProperties(schema);
|
||||||
addGenerated(modelName, innerModel);
|
addGenerated(modelName, innerModel);
|
||||||
openapi.getComponents().addSchemas(modelName, innerModel);
|
openAPI.getComponents().addSchemas(modelName, innerModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flatten inline models in components
|
||||||
|
*
|
||||||
|
* @param openAPI target spec
|
||||||
|
*/
|
||||||
|
private void flattenComponents(OpenAPI openAPI) {
|
||||||
|
Map<String, Schema> models = openAPI.getComponents().getSchemas();
|
||||||
|
if (models == null) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// definitions
|
|
||||||
if (models != null) {
|
|
||||||
List<String> modelNames = new ArrayList<String>(models.keySet());
|
List<String> modelNames = new ArrayList<String>(models.keySet());
|
||||||
for (String modelName : modelNames) {
|
for (String modelName : modelNames) {
|
||||||
Schema model = models.get(modelName);
|
Schema model = models.get(modelName);
|
||||||
@ -286,7 +348,7 @@ public class InlineModelResolver {
|
|||||||
Schema innerModel = modelFromProperty(op, innerModelName);
|
Schema innerModel = modelFromProperty(op, innerModelName);
|
||||||
String existing = matchGenerated(innerModel);
|
String existing = matchGenerated(innerModel);
|
||||||
if (existing == null) {
|
if (existing == null) {
|
||||||
openapi.getComponents().addSchemas(innerModelName, innerModel);
|
openAPI.getComponents().addSchemas(innerModelName, innerModel);
|
||||||
addGenerated(innerModelName, innerModel);
|
addGenerated(innerModelName, innerModel);
|
||||||
Schema schema = new Schema().$ref(innerModelName);
|
Schema schema = new Schema().$ref(innerModelName);
|
||||||
schema.setRequired(op.getRequired());
|
schema.setRequired(op.getRequired());
|
||||||
@ -315,7 +377,6 @@ public class InlineModelResolver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function fix models that are string (mostly enum). Before this fix, the
|
* This function fix models that are string (mostly enum). Before this fix, the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user