private void addSchemas()

in dubbo-plugin/dubbo-rest-openapi/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/openapi/DefinitionMerger.java [420:514]


    private void addSchemas(OpenAPI target, String version, String group) {
        Map<String, PathItem> paths = target.getPaths();
        if (paths == null) {
            return;
        }
        Map<Schema, Schema> schemas = new IdentityHashMap<>();
        for (PathItem pathItem : paths.values()) {
            Map<HttpMethods, Operation> operations = pathItem.getOperations();
            if (operations == null) {
                continue;
            }
            for (Operation operation : operations.values()) {
                List<Parameter> parameters = operation.getParameters();
                if (parameters != null) {
                    for (Parameter parameter : parameters) {
                        addSchema(parameter.getSchema(), schemas, group, version);
                        Map<String, MediaType> contents = parameter.getContents();
                        if (contents == null) {
                            continue;
                        }
                        for (MediaType content : contents.values()) {
                            addSchema(content.getSchema(), schemas, group, version);
                        }
                    }
                }
                RequestBody requestBody = operation.getRequestBody();
                if (requestBody != null) {
                    Map<String, MediaType> contents = requestBody.getContents();
                    if (contents == null) {
                        continue;
                    }
                    for (MediaType content : contents.values()) {
                        addSchema(content.getSchema(), schemas, group, version);
                    }
                }
                Map<String, ApiResponse> responses = operation.getResponses();
                if (responses != null) {
                    for (ApiResponse response : responses.values()) {
                        Map<String, Header> headers = response.getHeaders();
                        if (headers != null) {
                            for (Header header : headers.values()) {
                                addSchema(header.getSchema(), schemas, group, version);
                            }
                        }

                        Map<String, MediaType> contents = response.getContents();
                        if (contents == null) {
                            continue;
                        }
                        for (MediaType content : contents.values()) {
                            addSchema(content.getSchema(), schemas, group, version);
                        }
                    }
                }
            }
        }

        Components components = target.getComponents();
        if (components == null) {
            target.setComponents(components = new Components());
        }

        Set<String> names = CollectionUtils.newHashSet(schemas.size());
        for (Schema schema : schemas.keySet()) {
            String name = schema.getName();
            if (name != null) {
                names.add(name);
            }
        }

        OpenAPINamingStrategy strategy = getNamingStrategy();
        for (Schema schema : schemas.values()) {
            String name = schema.getName();
            if (name == null) {
                Class<?> clazz = schema.getJavaType();
                name = strategy.generateSchemaName(clazz, target);
                for (int i = 1; i < 100; i++) {
                    if (names.contains(name)) {
                        name = strategy.resolveSchemaNameConflict(i, name, clazz, target);
                    } else {
                        names.add(name);
                        break;
                    }
                }
                schema.setName(name);
            }

            for (Schema sourceSchema : schema.getSourceSchemas()) {
                sourceSchema.setTargetSchema(schema);
                sourceSchema.setRef("#/components/schemas/" + name);
            }
            schema.setSourceSchemas(null);
            components.addSchema(name, schema);
        }
    }