Skip to content

Commit

Permalink
Merge bea5d18 into fa55b53
Browse files Browse the repository at this point in the history
  • Loading branch information
kakulisen committed Oct 26, 2019
2 parents fa55b53 + bea5d18 commit f7bdbfc
Show file tree
Hide file tree
Showing 27 changed files with 2,038 additions and 0 deletions.
68 changes: 68 additions & 0 deletions oas-generator/oas-generator-core/pom.xml
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>oas-generator</artifactId>
<groupId>org.apache.servicecomb.toolkit</groupId>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>oas-generator-core</artifactId>

<properties>
<swagger.version>2.0.9</swagger.version>
</properties>

<dependencies>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger.version}</version>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.version}</version>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId>
<version>${swagger.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>7.2</version>
</dependency>

</dependencies>


</project>
@@ -0,0 +1,24 @@
/*
* 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.servicecomb.toolkit.generator;

public class HttpStatus {

public static String OK = "200";

}
@@ -0,0 +1,54 @@
/*
* 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.servicecomb.toolkit.generator;

/**
* Common media type constants
*
* @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1 section 3.7</a>
*/
public class MediaTypeConst {

public final static String WILDCARD = "*/*";

public final static String APPLICATION_XML = "application/xml";

public final static String APPLICATION_ATOM_XML = "application/atom+xml";

public final static String APPLICATION_XHTML_XML = "application/xhtml+xml";

public final static String APPLICATION_SVG_XML = "application/svg+xml";

public final static String APPLICATION_JSON = "application/json";

public final static String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";

public final static String MULTIPART_FORM_DATA = "multipart/form-data";

public final static String APPLICATION_OCTET_STREAM = "application/octet-stream";

public final static String TEXT_PLAIN = "text/plain";

public final static String TEXT_XML = "text/xml";

public final static String TEXT_HTML = "text/html";

public final static String SERVER_SENT_EVENTS = "text/event-stream";

public final static String APPLICATION_JSON_PATCH_JSON = "application/json-patch+json";
}
@@ -0,0 +1,151 @@
/*
* 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.servicecomb.toolkit.generator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.toolkit.generator.parser.api.OpenApiAnnotationParser;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.PathItem.HttpMethod;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.servers.Server;

public class OasContext {

private OpenAPI openAPI;

private String basePath;

private Class<?> cls;

private List<OperationContext> operationList = new ArrayList<>();

private OpenApiAnnotationParser parser;

public OasContext(OpenApiAnnotationParser parser) {
this(new OpenAPI(), parser);
}

public OasContext(OpenAPI openAPI, OpenApiAnnotationParser parser) {
this.openAPI = openAPI;
this.parser = parser;
}

public OpenAPI toOpenAPI() {
ensurePaths();
for (OperationContext operationCtx : operationList) {
if (!operationCtx.hasOperation()) {
continue;
}

if (openAPI.getPaths() == null) {
openAPI.setPaths(new Paths());
}

PathItem pathItem = openAPI.getPaths().get(operationCtx.getPath());
if (pathItem == null) {
pathItem = new PathItem();
openAPI.path(operationCtx.getPath(), pathItem);
}
pathItem.operation(HttpMethod.valueOf(operationCtx.getHttpMethod()), operationCtx.toOperation());
}

// 如果没有restful资源则返回null
if (openAPI.getPaths() == null || openAPI.getPaths().size() == 0) {
return null;
}

openAPI.info(new Info().title("gen").version("1.0.0"));

correctBasepath();
correctComponents();

openAPI.servers(Collections.singletonList(new Server().url(basePath)));

return openAPI;
}

private void correctComponents() {
Components nullComponents = new Components();
if (nullComponents.equals(getComponents())) {
openAPI.setComponents(null);
}
}

private void correctBasepath() {
if (StringUtils.isEmpty(basePath)) {
basePath = "/";
}

if (!basePath.startsWith("/")) {
basePath = "/" + basePath;
}
}

public Components getComponents() {
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
return openAPI.getComponents();
}

private void ensurePaths() {
if (openAPI.getPaths() == null) {
openAPI.setPaths(new Paths());
}
}

public OpenApiAnnotationParser getParser() {
return parser;
}

public void setParser(OpenApiAnnotationParser parser) {
this.parser = parser;
}

public OpenAPI getOpenAPI() {
return openAPI;
}

public String getBasePath() {
return basePath;
}

public Class<?> getCls() {
return cls;
}

public void setCls(Class<?> cls) {
this.cls = cls;
}

public void setBasePath(String basePath) {
this.basePath = basePath;
}

public void addOperation(OperationContext operation) {
operationList.add(operation);
}
}
@@ -0,0 +1,63 @@
/*
* 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.servicecomb.toolkit.generator;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;

import org.apache.servicecomb.toolkit.generator.parser.api.OpenApiAnnotationParser;

import io.swagger.v3.oas.models.OpenAPI;

public class OasGenerator {

private static List<OpenApiAnnotationParser> parserList = new ArrayList<>();

static {
ServiceLoader.load(OpenApiAnnotationParser.class).forEach(parserList::add);
}

public OpenAPI generate(Class<?> cls) {

Optional<OpenApiAnnotationParser> parserOptional = parserList.stream().filter(parser -> parser.canProcess(cls))
.findFirst();

if (!parserOptional.isPresent()) {
return null;
}
OasContext context = new OasContext(parserOptional.get());
parserOptional.get().parser(cls, context);
return context.toOpenAPI();
}

public List<OpenAPI> generate(Set<Class> classes) {

List<OpenAPI> openApiList = new ArrayList<>();
for (Class cls : classes) {
OpenAPI openAPI = generate(cls);
if (openAPI != null) {
openApiList.add(openAPI);
}
}

return openApiList;
}
}

0 comments on commit f7bdbfc

Please sign in to comment.