Skip to content

Commit

Permalink
Merge b188800 into fa55b53
Browse files Browse the repository at this point in the history
  • Loading branch information
kakulisen committed Oct 29, 2019
2 parents fa55b53 + b188800 commit 41b7a47
Show file tree
Hide file tree
Showing 70 changed files with 3,913 additions and 55 deletions.
30 changes: 0 additions & 30 deletions common/pom.xml
Expand Up @@ -54,36 +54,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>swagger-generator-springmvc</artifactId>
<version>1.2.0</version>
</dependency>

<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>swagger-generator-jaxrs</artifactId>
<version>1.2.0</version>
</dependency>

<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-rest-common</artifactId>
<version>1.2.0</version>
</dependency>

<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-pojo</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
15 changes: 15 additions & 0 deletions contractgen/pom.xml
Expand Up @@ -34,6 +34,21 @@
<artifactId>common</artifactId>
</dependency>

<dependency>
<groupId>org.apache.servicecomb.toolkit</groupId>
<artifactId>oas-generator-jaxrs</artifactId>
</dependency>

<dependency>
<groupId>org.apache.servicecomb.toolkit</groupId>
<artifactId>oas-generator-spring</artifactId>
</dependency>

<dependency>
<groupId>org.apache.servicecomb.toolkit</groupId>
<artifactId>oas-generator-servicecomb</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Expand Up @@ -30,29 +30,26 @@
import java.util.Map;
import java.util.Vector;

import org.apache.commons.lang3.StringUtils;
import javax.ws.rs.Path;

import org.apache.servicecomb.provider.pojo.RpcSchema;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.swagger.SwaggerUtils;
import org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext;
import org.apache.servicecomb.swagger.generator.core.SwaggerGenerator;
import org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext;
import org.apache.servicecomb.toolkit.ContractsGenerator;
import org.apache.servicecomb.toolkit.common.ContractFileType;
import org.apache.servicecomb.toolkit.common.ImmediateClassLoader;
import org.apache.servicecomb.toolkit.generator.OasGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.Path;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.OpenAPI;

public class DefaultContractsGenerator implements ContractsGenerator {

private static Logger LOGGER = LoggerFactory.getLogger(DefaultContractsGenerator.class);

private static CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();

private Map<String, Object> config;

private List<String> classpathUrls;
Expand Down Expand Up @@ -125,12 +122,14 @@ public void generate() throws RuntimeException {
continue;
}

SwaggerGeneratorContext generatorContext =
compositeSwaggerGeneratorContext.selectContext(loadClass);
OasGenerator oasGenerator = new OasGenerator();
OpenAPI oas = oasGenerator.generate(loadClass);

SwaggerGenerator generator = new SwaggerGenerator(generatorContext, loadClass);
if (oas == null) {
continue;
}

String swaggerString = SwaggerUtils.swaggerToString(generator.generate());
String oasPretty = Yaml.pretty(oas);

File outputFile = new File(
outputDir + File.separator + loadClass.getSimpleName() + contractfileType
Expand All @@ -143,7 +142,7 @@ public void generate() throws RuntimeException {
outputFile.createNewFile();
}

Files.write(Paths.get(outputFile.toURI()), swaggerString.getBytes());
Files.write(Paths.get(outputFile.toURI()), oasPretty.getBytes());
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Expand Up @@ -32,8 +32,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.swagger.models.Swagger;
import io.swagger.util.Json;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.OpenAPI;

public class ContractsSwaggerUIGenerator implements DocGenerator {

Expand All @@ -43,7 +43,7 @@ public class ContractsSwaggerUIGenerator implements DocGenerator {

private final static String DOC_SUFFIX = ".html";

private Swagger contractContent;
private OpenAPI contractContent;

private String outputPath = ".";

Expand All @@ -55,7 +55,7 @@ public boolean canProcess(String type) {

@Override
public void configure(Map<String, Object> config) {
this.contractContent = (Swagger) config.get("contractContent");
this.contractContent = (OpenAPI) config.get("contractContent");
this.outputPath = (String) config.get("outputPath");
}

Expand Down
Expand Up @@ -33,16 +33,14 @@
import org.junit.Assert;
import org.junit.Test;

import io.swagger.models.Swagger;
import io.swagger.parser.Swagger20Parser;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.parser.core.models.SwaggerParseResult;

public class ContractsSwaggerUIGeneratorTest {

@Test
public void testContractTransferToSwaggerUI() throws IOException {

Swagger20Parser swagger20Parser = new Swagger20Parser();

InputStream in = ContractsSwaggerUIGeneratorTest.class.getClassLoader().getResourceAsStream("HelloEndPoint.yaml");

StringBuilder sb = new StringBuilder();
Expand All @@ -52,14 +50,15 @@ public void testContractTransferToSwaggerUI() throws IOException {
sb.append(new String(bytes, 0, len));
}

Swagger swagger = swagger20Parser.parse(sb.toString());
OpenAPIParser openAPIParser = new OpenAPIParser();
SwaggerParseResult swaggerParseResult = openAPIParser.readContents(sb.toString(), null, null);

Path tempDir = Files.createTempDirectory(null);
Path outputPath = Paths.get(tempDir.toFile().getAbsolutePath()
+ File.separator + "swagger-ui.html");
DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, "default");
Map<String, Object> docGeneratorConfig = new HashMap<>();
docGeneratorConfig.put("contractContent", swagger);
docGeneratorConfig.put("contractContent", swaggerParseResult.getOpenAPI());
docGeneratorConfig.put("outputPath", outputPath.toFile().getCanonicalPath());
Objects.requireNonNull(docGenerator).configure(docGeneratorConfig);
docGenerator.generate();
Expand Down
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 comments on commit 41b7a47

Please sign in to comment.