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

fix(interactive): Support generating valid spring code with Interactive OpenAPI spec #4415

Closed
wants to merge 5 commits into from
Closed
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
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ flex/interactive/sdk/python/gs_interactive/rest.py
!flex/interactive/sdk/python/gs_interactive/models/long_text.py


interactive_engine/groot-http/.openapi-generator/
interactive_engine/groot-http/src/test/
interactive_engine/groot-http/src/main/resources/
interactive_engine/groot-http/src/main/java/org/
interactive_engine/groot-http/src/main/java/com/alibaba/graphscope/groot/service/api/
interactive_engine/groot-http/src/main/java/com/alibaba/graphscope/groot/service/models/
interactive_engine/groot-http/src/main/java/com/alibaba/graphscope/groot/*.java
!interactive_engine/groot-http/src/main/java/com/alibaba/graphscope/groot/service/models/GSDataType.java
!interactive_engine/groot-http/src/main/java/com/alibaba/graphscope/groot/service/models/StringTypeString.java
!interactive_engine/groot-http/src/main/java/com/alibaba/graphscope/groot/service/models/TemporalTypeTemporal.java

**/.cache/

**/*.ci-friendly-pom.xml
Expand Down
18 changes: 18 additions & 0 deletions flex/interactive/sdk/generate_sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ function do_gen_python() {
eval $cmd
}


function do_gen_spring() {
echo "Generating Spring API"
OUTPUT_PATH="${CUR_DIR}/../../../interactive_engine/groot-http"
GROOT_PACKAGE_NAME="com.alibaba.graphscope.groot"
GROOT_ARTIFACT_ID="groot-http"
additional_properties="apiPackage=${GROOT_PACKAGE_NAME}.service.api,modelPackage=${GROOT_PACKAGE_NAME}.service.models,artifactId=${GROOT_ARTIFACT_ID},groupId=${GROUP_ID},invokerPackage=${GROOT_PACKAGE_NAME}"
export JAVA_OPTS="-Dlog.level=${LOG_LEVEL}"
cmd="openapi-generator-cli generate -i ${OPENAPI_SPEC_PATH} -g spring -o ${OUTPUT_PATH}"
cmd=" ${cmd} --additional-properties=${additional_properties}"
echo "Running command: ${cmd}"

eval $cmd
}

function do_gen() {
# expect only one argument
if [ $# -ne 1 ]; then
Expand All @@ -97,6 +112,9 @@ function do_gen() {
python)
do_gen_python
;;
spring)
do_gen_spring
;;
*)
err "Unsupported language: $lang"
usage
Expand Down
3 changes: 2 additions & 1 deletion flex/openapi/openapi_interactive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,7 @@ components:
properties:
timestamp:
type: string
nullable: true
DateType:
x-body-name: date_type
type: object
Expand All @@ -1395,6 +1396,7 @@ components:
properties:
date32:
type: string
nullable: true
TemporalType:
x-body-name: temporal_type
type: object
Expand Down Expand Up @@ -2111,4 +2113,3 @@ components:
type: array
items:
$ref: '#/components/schemas/EdgeRequest'

31 changes: 31 additions & 0 deletions interactive_engine/groot-http/.openapi-generator-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md

src/main/java/com/alibaba/graphscope/groot/service/models/GSDataType.java
src/main/java/com/alibaba/graphscope/groot/service/models/StringTypeString.java
src/main/java/com/alibaba/graphscope/groot/service/models/TemporalTypeTemporal.java

README.md

pom.xml
21 changes: 21 additions & 0 deletions interactive_engine/groot-http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# OpenAPI generated server

Spring Boot Server

## Overview
This server was generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
By using the [OpenAPI-Spec](https://openapis.org), you can easily generate a server stub.
This is an example of building a OpenAPI-enabled server in Java using the SpringBoot framework.


The underlying library integrating OpenAPI to Spring Boot is [springdoc](https://springdoc.org).
Springdoc will generate an OpenAPI v3 specification based on the generated Controller and Model classes.
The specification is available to download using the following url:
http://localhost:8080/v3/api-docs/

Start your server as a simple java application

You can view the api documentation in swagger-ui by pointing to
http://localhost:8080/swagger-ui.html

Change default port value in application.properties
82 changes: 82 additions & 0 deletions interactive_engine/groot-http/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.graphscope</groupId>
<artifactId>groot-http</artifactId>
<packaging>jar</packaging>
<name>groot-http</name>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springdoc.version>1.6.14</springdoc.version>
<swagger-ui.version>5.3.1</swagger-ui.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<!--SpringDoc dependencies -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.6</version>
</dependency>
<!-- Bean Validation API support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2022 Alibaba Group Holding Limited.
*
* Licensed 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 com.alibaba.graphscope.groot.service.deserializer;

import com.alibaba.graphscope.groot.service.models.GSDataType;
import com.alibaba.graphscope.groot.service.models.PrimitiveType;
import com.alibaba.graphscope.groot.service.models.StringType;
import com.alibaba.graphscope.groot.service.models.StringTypeString;
import com.alibaba.graphscope.groot.service.models.TemporalType;
import com.alibaba.graphscope.groot.service.models.TemporalTypeTemporal;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class GSDataTypeDeserializer extends JsonDeserializer<GSDataType> {
public GSDataTypeDeserializer() {}

@Override
public GSDataType deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
System.out.println("GSDataTypeDeserializer");
System.out.println(node);

if (node.has("string")) {
StringType stringType = new StringType();
stringType.setString(mapper.treeToValue(node.get("string"), StringTypeString.class));
return stringType;
} else if (node.has("temporal")) {
// ObjectMapper objectMapper = new ObjectMapper();
TemporalType temporalType = new TemporalType();
temporalType.setTemporal(
mapper.treeToValue(node.get("temporal"), TemporalTypeTemporal.class));
return temporalType;
} else if (node.has("primitive_type")) {
return new PrimitiveType(
PrimitiveType.PrimitiveTypeEnum.fromValue(
jp.getCodec().treeToValue(node.get("primitive_type"), String.class)));
} else {
throw new IOException("Unknown variant for GSDataType");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 Alibaba Group Holding Limited.
*
* Licensed 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 com.alibaba.graphscope.groot.service.deserializer;

import com.alibaba.graphscope.groot.service.models.FixedChar;
import com.alibaba.graphscope.groot.service.models.FixedCharChar;
import com.alibaba.graphscope.groot.service.models.LongText;
import com.alibaba.graphscope.groot.service.models.StringTypeString;
import com.alibaba.graphscope.groot.service.models.VarChar;
import com.alibaba.graphscope.groot.service.models.VarCharVarChar;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;

public class StringTypeStringDeserializer extends JsonDeserializer<StringTypeString> {
public StringTypeStringDeserializer() {}

@Override
public StringTypeString deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
System.out.println("StringTypeStringDeserializer");
System.out.println(node);

if (node.has("var_char")) {
System.out.println("var_char");
VarCharVarChar varChar = new VarCharVarChar();
if (node.get("var_char").has("max_length")) {
// return new VarChar(new
// VarCharVarChar(jp.getCodec().treeToValue(node.get("var_char").get("max_length"),
// String.class)));
varChar.setMaxLength(node.get("var_char").get("max_length").asInt());
} else {
throw new IOException("max_length not found in var_char");
}
return new VarChar(varChar);
} else if (node.has("long_text")) {
return new LongText(jp.getCodec().treeToValue(node.get("long_text"), String.class));
} else if (node.has("fixed_char")) {
return new FixedChar(
jp.getCodec().treeToValue(node.get("fixed_char"), FixedCharChar.class));
} else {
throw new IOException("Unknown variant for GSDataType");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2022 Alibaba Group Holding Limited.
*
* Licensed 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 com.alibaba.graphscope.groot.service.deserializer;

import com.alibaba.graphscope.groot.service.models.DateType;
import com.alibaba.graphscope.groot.service.models.TemporalTypeTemporal;
import com.alibaba.graphscope.groot.service.models.TimeStampType;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;

public class TemporalTypeTemporalDeserializer extends JsonDeserializer<TemporalTypeTemporal> {
public TemporalTypeTemporalDeserializer() {}

@Override
public TemporalTypeTemporal deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);

if (node.has("date32")) {
return new DateType();
} else if (node.has("timestamp")) {
return new TimeStampType();
} else {
throw new IOException("Unknown variant for GSDataType");
}
}
}
Loading
Loading