Skip to content

Commit

Permalink
Refine DefinitionMerger
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Nov 27, 2024
1 parent b80d947 commit ab2458c
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

<!-- generate json format javadoc for openapi -->
<dependency>
<groupId>com.github.therapi</groupId>
<artifactId>therapi-runtime-javadoc-scribe</artifactId>
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,4 @@ public interface GreeterService {
* Sends greetings with bi streaming
*/
StreamObserver<HelloRequest> sayHelloBiStream(StreamObserver<HelloReply> responseObserver);

/**
* Create an animal
* @param cat the cat
* @param dog the dog
* @return the animal
*/
Animal createAnimal(Cat cat, Dog dog);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ public void onCompleted() {
};
}

@Override
public Animal createAnimal(Cat cat, Dog dog) {
return cat;
}

private static HelloReply toReply(String message) {
HelloReply reply = new HelloReply();
reply.setMessage(message);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ dubbo:
openapi:
enabled: true
cache: false
schema-flatten: false
schema-class-excludes:
- a.
- "!b.c."
- "!org.apache.dubbo.springboot.demo."
servlet:
enabled: true
registry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.MethodMeta;
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.ParameterMeta;
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.ServiceMeta;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.Constants;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.OpenAPIDefinitionResolver;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.OpenAPISchemaPredicate;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.OpenAPISchemaResolver;
Expand All @@ -35,15 +36,20 @@
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.OpenAPI;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Operation;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Schema;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Schema.Type;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Tag;

import java.util.Arrays;
import java.util.Map;

import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.media.Schema.AccessMode;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;

import static org.apache.dubbo.rpc.protocol.tri.rest.openapi.Helper.setValue;
import static org.apache.dubbo.rpc.protocol.tri.rest.openapi.Helper.trim;

@Activate(order = 50, onClass = "io.swagger.v3.oas.annotations.OpenAPIDefinition")
Expand Down Expand Up @@ -98,7 +104,14 @@ public OpenAPI resolve(OpenAPI openAPI, ServiceMeta serviceMeta, OpenAPIChain ch

openAPI.setExternalDocs(toExternalDocs(anno.externalDocs()));

openAPI.setExtensions(toProperties(anno.extensions()));
Map<String, String> properties = toProperties(anno.extensions());
if (properties != null) {
String group = properties.remove(Constants.X_API_GROUP);
if (group != null) {
openAPI.setGroup(group);
}
openAPI.setExtensions(properties);
}
return openAPI;
}

Expand Down Expand Up @@ -142,27 +155,74 @@ public Operation resolve(Operation operation, MethodMeta methodMeta, OperationCo
for (String tag : anno.tags()) {
operation.addTag(tag);
}
Map<String, String> properties = toProperties(anno.extensions());
if (properties != null) {
String group = properties.remove(Constants.X_API_GROUP);
if (group != null) {
operation.setGroup(group);
}
String version = properties.remove(Constants.X_API_VERSION);
if (version != null) {
operation.setVersion(version);
}
operation.setExtensions(properties);
}
return operation
.setSummary(trim(anno.summary()))
.setDescription(trim(anno.description()))
.setExternalDocs(toExternalDocs(anno.externalDocs()))
.setOperationId(trim(anno.operationId()))
.setDeprecated(anno.deprecated() ? Boolean.TRUE : null)
.setExtensions(toProperties(anno.extensions()));
.setDeprecated(anno.deprecated() ? Boolean.TRUE : null);
}

@Override
public Schema resolve(ParameterMeta parameter, SchemaContext context, SchemaChain chain) {
AnnotationMeta<io.swagger.v3.oas.annotations.media.Schema> meta =
AnnotationMeta<io.swagger.v3.oas.annotations.media.Schema> annoMeta =
parameter.getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
if (meta == null) {
if (annoMeta == null) {
return chain.resolve(parameter, context);
}
io.swagger.v3.oas.annotations.media.Schema schema = meta.getAnnotation();
if (schema.hidden() || parameter.isHierarchyAnnotated(Hidden.class)) {
io.swagger.v3.oas.annotations.media.Schema anno = annoMeta.getAnnotation();
if (anno.hidden() || parameter.isHierarchyAnnotated(Hidden.class)) {
return null;
}
return chain.resolve(parameter, context);
Schema schema = chain.resolve(parameter, context);
if (schema == null) {
return null;
}

Map<String, String> properties = toProperties(anno.extensions());
if (properties != null) {
String group = properties.remove(Constants.X_API_GROUP);
if (group != null) {
schema.setGroup(group);
}
String version = properties.remove(Constants.X_API_VERSION);
if (version != null) {
schema.setVersion(version);
}
schema.setExtensions(properties);
}

setValue(anno::type, v -> schema.setType(Type.valueOf(v)));
setValue(anno::format, schema::setFormat);
setValue(anno::name, schema::setName);
setValue(anno::title, schema::setTitle);
setValue(anno::description, schema::setDescription);
setValue(anno::defaultValue, schema::setDefaultValue);
setValue(anno::pattern, schema::setPattern);
setValue(anno::example, schema::setExample);
String[] enumItems = trim(anno.allowableValues());
if (enumItems != null) {
schema.setEnumeration(Arrays.asList(enumItems));
}
schema.setRequired(anno.requiredMode() == RequiredMode.REQUIRED ? Boolean.TRUE : null);
schema.setReadOnly(anno.accessMode() == AccessMode.READ_ONLY ? Boolean.TRUE : null);
schema.setWriteOnly(anno.accessMode() == AccessMode.WRITE_ONLY ? Boolean.TRUE : null);
schema.setNullable(anno.nullable() ? Boolean.TRUE : null);
schema.setDeprecated(anno.deprecated() ? Boolean.TRUE : null);

return schema;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
*/
package org.apache.dubbo.rpc.protocol.tri.servlet;

import org.apache.dubbo.remoting.http12.HttpHeaderNames;
import org.apache.dubbo.remoting.http12.HttpHeaders;
import org.apache.dubbo.remoting.http12.h2.Http2Header;

import javax.servlet.http.HttpServletRequest;

import java.util.Enumeration;

import io.netty.handler.codec.http2.Http2Headers.PseudoHeaderName;

public final class HttpMetadataAdapter implements Http2Header {

private final HttpServletRequest request;
Expand Down Expand Up @@ -50,6 +53,10 @@ public HttpHeaders headers() {
headers.add(key, ven.nextElement());
}
}
headers.add(PseudoHeaderName.METHOD.value(), method());
headers.add(PseudoHeaderName.SCHEME.value(), request.getScheme());
headers.add(PseudoHeaderName.AUTHORITY.value(), request.getHeader(HttpHeaderNames.HOST.getName()));
headers.add(PseudoHeaderName.PROTOCOL.value(), request.getProtocol());
this.headers = headers;
}
return headers;
Expand Down
Loading

0 comments on commit ab2458c

Please sign in to comment.