Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]generate openapi #14570

Open
wants to merge 6 commits into
base: 3.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.metadata;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.metadata.swagger.model.OpenAPI;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -226,4 +227,6 @@ static boolean isMetadataService(String interfaceName) {
* @since 3.0
*/
String getAndListenInstanceMetadata(String consumerId, InstanceMetadataChangedListener listener);

OpenAPI getOpenAPI();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.metadata;

import org.apache.dubbo.metadata.swagger.model.OpenAPI;
import org.apache.dubbo.metadata.swagger.model.PathItem;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

public class OperationService {
public String getOperationId(String operationId, String oldOperationId, OpenAPI openAPI) {
if (StringUtils.isNotBlank(oldOperationId)) return this.getOperationId(oldOperationId, openAPI);
else return this.getOperationId(operationId, openAPI);
}
/**
* Gets operation id.
*
* @param operationId the operation id
* @param openAPI the open api
* @return the operation id
*/
public String getOperationId(String operationId, OpenAPI openAPI) {
boolean operationIdUsed = existOperationId(operationId, openAPI);
String operationIdToFind = null;
int counter = 0;
while (operationIdUsed) {
operationIdToFind = String.format("%s_%d", operationId, ++counter);
operationIdUsed = existOperationId(operationIdToFind, openAPI);
}
if (operationIdToFind != null) {
operationId = operationIdToFind;
}
return operationId;
}

/**
* Exist operation id boolean.
*
* @param operationId the operation id
* @param openAPI the open api
* @return the boolean
*/
private boolean existOperationId(String operationId, OpenAPI openAPI) {
if (openAPI == null) {
return false;
}
if (openAPI.getPaths() == null || openAPI.getPaths().isEmpty()) {
return false;
}
for (PathItem path : openAPI.getPaths().values()) {
Set<String> pathOperationIds = extractOperationIdFromPathItem(path);
if (pathOperationIds.contains(operationId)) {
return true;
}
}
return false;
}

/**
* Extract operation id from path item set.
*
* @param path the path
* @return the set
*/
private Set<String> extractOperationIdFromPathItem(PathItem path) {
Set<String> ids = new HashSet<>();
if (path.getGet() != null && StringUtils.isNotBlank(path.getGet().getOperationId())) {
ids.add(path.getGet().getOperationId());
}
if (path.getPost() != null && StringUtils.isNotBlank(path.getPost().getOperationId())) {
ids.add(path.getPost().getOperationId());
}
if (path.getPut() != null && StringUtils.isNotBlank(path.getPut().getOperationId())) {
ids.add(path.getPut().getOperationId());
}
if (path.getDelete() != null && StringUtils.isNotBlank(path.getDelete().getOperationId())) {
ids.add(path.getDelete().getOperationId());
}
if (path.getOptions() != null
&& StringUtils.isNotBlank(path.getOptions().getOperationId())) {
ids.add(path.getOptions().getOperationId());
}
if (path.getHead() != null && StringUtils.isNotBlank(path.getHead().getOperationId())) {
ids.add(path.getHead().getOperationId());
}
if (path.getPatch() != null && StringUtils.isNotBlank(path.getPatch().getOperationId())) {
ids.add(path.getPatch().getOperationId());
}
return ids;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.metadata.swagger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.metadata.swagger.model.responses.ApiResponse;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class MethodAttributes {
/**
* The Default consumes media type.
*/
private final String defaultConsumesMediaType;

/**
* The Default produces media type.
*/
private final String defaultProducesMediaType;

/**
* The Method consumes.
*/
private String[] methodProduces = {};

/**
* The Method consumes.
*/
private String[] methodConsumes = {};

/**
* The Generic map response.
*/
private Map<String, ApiResponse> genericMapResponse = new LinkedHashMap<>();

public String getDefaultConsumesMediaType() {
return defaultConsumesMediaType;
}

public String getDefaultProducesMediaType() {
return defaultProducesMediaType;
}

public String[] getMethodProduces() {
return methodProduces;
}

public void setMethodProduces(String[] methodProduces) {
this.methodProduces = methodProduces;
}

public Map<String, ApiResponse> getGenericMapResponse() {
return genericMapResponse;
}

public void setGenericMapResponse(Map<String, ApiResponse> genericMapResponse) {
this.genericMapResponse = genericMapResponse;
}

public MethodAttributes(String defaultConsumesMediaType, String defaultProducesMediaType, String[] methodProduces) {
this.defaultConsumesMediaType = defaultConsumesMediaType;
this.defaultProducesMediaType = defaultProducesMediaType;
this.methodProduces = methodProduces;
}

public void fillMethod() {
String[] produces = methodProduces;
String[] consumes = methodConsumes;
if (ArrayUtils.isNotEmpty(produces)) {
methodProduces = mergeArrays(methodProduces, produces);
} else if (ArrayUtils.isEmpty(methodProduces)) {
methodProduces = new String[] {defaultProducesMediaType};
}

if (ArrayUtils.isNotEmpty(consumes)) {
methodConsumes = mergeArrays(methodConsumes, consumes);
} else if (ArrayUtils.isEmpty(methodConsumes)) {
methodConsumes = new String[] {defaultConsumesMediaType};
}
}

private String[] mergeArrays(String[] array1, String[] array2) {
Set<String> uniqueValues =
array1 == null ? new HashSet<>() : Arrays.stream(array1).collect(Collectors.toSet());
uniqueValues.addAll(Arrays.asList(array2));
return uniqueValues.toArray(new String[0]);
}
}
Loading
Loading