From ed2b397105257761af23ebdccf23a21a05ceab99 Mon Sep 17 00:00:00 2001 From: wubin1989 <328454505@qq.com> Date: Sun, 9 May 2021 00:47:16 +0800 Subject: [PATCH] fix generate openapi3.json redundant tags array bug --- .../backend/docparser/entity/Backend.java | 2 +- .../openapi3/model/paths/Operation.java | 183 +++++++++++++++++- .../generator/openapi3/model/paths/Path.java | 2 +- .../service/docparser/entity/BizServer.java | 5 +- .../service/ServiceFolderGeneratorTest.java | 2 +- .../service/ServiceTsGeneratorTest.java | 2 +- .../service/TypesTsGeneratorTest.java | 2 +- 7 files changed, 190 insertions(+), 8 deletions(-) diff --git a/unionj-generator-backend/src/main/java/cloud/unionj/generator/backend/docparser/entity/Backend.java b/unionj-generator-backend/src/main/java/cloud/unionj/generator/backend/docparser/entity/Backend.java index a8d244cb..4ff5ce47 100644 --- a/unionj-generator-backend/src/main/java/cloud/unionj/generator/backend/docparser/entity/Backend.java +++ b/unionj-generator-backend/src/main/java/cloud/unionj/generator/backend/docparser/entity/Backend.java @@ -122,7 +122,7 @@ public static Backend convert(Openapi3 openAPI) { } PathWrapper wrapper = new PathWrapper(); Path pathEntryValue = pathEntry.getValue(); - List tags = pathEntryValue.getTags(); + List tags = pathEntryValue.returnTags(); String protoName; if (CollectionUtils.isNotEmpty(tags) && tags.size() > 1 && StringUtils.isNotBlank(tags.get(1))) { protoName = StringUtils.capitalize(tags.get(1)) + "Proto"; diff --git a/unionj-generator-openapi/src/main/java/cloud/unionj/generator/openapi3/model/paths/Operation.java b/unionj-generator-openapi/src/main/java/cloud/unionj/generator/openapi3/model/paths/Operation.java index 99954910..f3e308cc 100644 --- a/unionj-generator-openapi/src/main/java/cloud/unionj/generator/openapi3/model/paths/Operation.java +++ b/unionj-generator-openapi/src/main/java/cloud/unionj/generator/openapi3/model/paths/Operation.java @@ -4,7 +4,6 @@ import cloud.unionj.generator.openapi3.model.ExternalDocs; import cloud.unionj.generator.openapi3.model.Security; import cloud.unionj.generator.openapi3.model.servers.Server; -import lombok.Data; import java.util.ArrayList; import java.util.List; @@ -16,7 +15,6 @@ * cloud.unionj.generator.openapi3.model.paths * date 2020/12/15 */ -@Data public class Operation { private List tags = new ArrayList<>(); @@ -40,6 +38,9 @@ public class Operation { // TODO private List servers; + public Operation() { + } + public void tags(String tag) { this.tags.add(tag); } @@ -48,4 +49,182 @@ public void parameters(Parameter parameter) { this.parameters.add(parameter); } + public List getTags() { + return this.tags; + } + + public String getSummary() { + return this.summary; + } + + public String getDescription() { + return this.description; + } + + public String getOperationId() { + return this.operationId; + } + + public List getParameters() { + return this.parameters; + } + + public RequestBody getRequestBody() { + return this.requestBody; + } + + public Responses getResponses() { + return this.responses; + } + + public boolean isDeprecated() { + return this.deprecated; + } + + public ExternalDocs getExternalDocs() { + return this.externalDocs; + } + + public Map getCallbacks() { + return this.callbacks; + } + + public List getSecurity() { + return this.security; + } + + public List getServers() { + return this.servers; + } + + public void setTags(List tags) { + this.tags = tags; + } + + public void setSummary(String summary) { + this.summary = summary; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setOperationId(String operationId) { + this.operationId = operationId; + } + + public void setParameters(List parameters) { + this.parameters = parameters; + } + + public void setRequestBody(RequestBody requestBody) { + this.requestBody = requestBody; + } + + public void setResponses(Responses responses) { + this.responses = responses; + } + + public void setDeprecated(boolean deprecated) { + this.deprecated = deprecated; + } + + public void setExternalDocs(ExternalDocs externalDocs) { + this.externalDocs = externalDocs; + } + + public void setCallbacks(Map callbacks) { + this.callbacks = callbacks; + } + + public void setSecurity(List security) { + this.security = security; + } + + public void setServers(List servers) { + this.servers = servers; + } + + public boolean equals(final Object o) { + if (o == this) return true; + if (!(o instanceof Operation)) return false; + final Operation other = (Operation) o; + if (!other.canEqual((Object) this)) return false; + final Object this$tags = this.getTags(); + final Object other$tags = other.getTags(); + if (this$tags == null ? other$tags != null : !this$tags.equals(other$tags)) return false; + final Object this$summary = this.getSummary(); + final Object other$summary = other.getSummary(); + if (this$summary == null ? other$summary != null : !this$summary.equals(other$summary)) return false; + final Object this$description = this.getDescription(); + final Object other$description = other.getDescription(); + if (this$description == null ? other$description != null : !this$description.equals(other$description)) + return false; + final Object this$operationId = this.getOperationId(); + final Object other$operationId = other.getOperationId(); + if (this$operationId == null ? other$operationId != null : !this$operationId.equals(other$operationId)) + return false; + final Object this$parameters = this.getParameters(); + final Object other$parameters = other.getParameters(); + if (this$parameters == null ? other$parameters != null : !this$parameters.equals(other$parameters)) return false; + final Object this$requestBody = this.getRequestBody(); + final Object other$requestBody = other.getRequestBody(); + if (this$requestBody == null ? other$requestBody != null : !this$requestBody.equals(other$requestBody)) + return false; + final Object this$responses = this.getResponses(); + final Object other$responses = other.getResponses(); + if (this$responses == null ? other$responses != null : !this$responses.equals(other$responses)) return false; + if (this.isDeprecated() != other.isDeprecated()) return false; + final Object this$externalDocs = this.getExternalDocs(); + final Object other$externalDocs = other.getExternalDocs(); + if (this$externalDocs == null ? other$externalDocs != null : !this$externalDocs.equals(other$externalDocs)) + return false; + final Object this$callbacks = this.getCallbacks(); + final Object other$callbacks = other.getCallbacks(); + if (this$callbacks == null ? other$callbacks != null : !this$callbacks.equals(other$callbacks)) return false; + final Object this$security = this.getSecurity(); + final Object other$security = other.getSecurity(); + if (this$security == null ? other$security != null : !this$security.equals(other$security)) return false; + final Object this$servers = this.getServers(); + final Object other$servers = other.getServers(); + if (this$servers == null ? other$servers != null : !this$servers.equals(other$servers)) return false; + return true; + } + + protected boolean canEqual(final Object other) { + return other instanceof Operation; + } + + public int hashCode() { + final int PRIME = 59; + int result = 1; + final Object $tags = this.getTags(); + result = result * PRIME + ($tags == null ? 43 : $tags.hashCode()); + final Object $summary = this.getSummary(); + result = result * PRIME + ($summary == null ? 43 : $summary.hashCode()); + final Object $description = this.getDescription(); + result = result * PRIME + ($description == null ? 43 : $description.hashCode()); + final Object $operationId = this.getOperationId(); + result = result * PRIME + ($operationId == null ? 43 : $operationId.hashCode()); + final Object $parameters = this.getParameters(); + result = result * PRIME + ($parameters == null ? 43 : $parameters.hashCode()); + final Object $requestBody = this.getRequestBody(); + result = result * PRIME + ($requestBody == null ? 43 : $requestBody.hashCode()); + final Object $responses = this.getResponses(); + result = result * PRIME + ($responses == null ? 43 : $responses.hashCode()); + result = result * PRIME + (this.isDeprecated() ? 79 : 97); + final Object $externalDocs = this.getExternalDocs(); + result = result * PRIME + ($externalDocs == null ? 43 : $externalDocs.hashCode()); + final Object $callbacks = this.getCallbacks(); + result = result * PRIME + ($callbacks == null ? 43 : $callbacks.hashCode()); + final Object $security = this.getSecurity(); + result = result * PRIME + ($security == null ? 43 : $security.hashCode()); + final Object $servers = this.getServers(); + result = result * PRIME + ($servers == null ? 43 : $servers.hashCode()); + return result; + } + + public String toString() { + return "Operation(tags=" + this.getTags() + ", summary=" + this.getSummary() + ", description=" + this.getDescription() + ", operationId=" + this.getOperationId() + ", parameters=" + this.getParameters() + ", requestBody=" + this.getRequestBody() + ", responses=" + this.getResponses() + ", deprecated=" + this.isDeprecated() + ", externalDocs=" + this.getExternalDocs() + ", callbacks=" + this.getCallbacks() + ", security=" + this.getSecurity() + ", servers=" + this.getServers() + ")"; + } } diff --git a/unionj-generator-openapi/src/main/java/cloud/unionj/generator/openapi3/model/paths/Path.java b/unionj-generator-openapi/src/main/java/cloud/unionj/generator/openapi3/model/paths/Path.java index 62df8627..43fe7977 100644 --- a/unionj-generator-openapi/src/main/java/cloud/unionj/generator/openapi3/model/paths/Path.java +++ b/unionj-generator-openapi/src/main/java/cloud/unionj/generator/openapi3/model/paths/Path.java @@ -30,7 +30,7 @@ public class Path { // TODO private List parameters; - public List getTags() { + public List returnTags() { List result = new ArrayList<>(); if (CollectionUtils.isEmpty(result)) { if (this.get != null) { diff --git a/unionj-generator-service/src/main/java/cloud/unionj/generator/service/docparser/entity/BizServer.java b/unionj-generator-service/src/main/java/cloud/unionj/generator/service/docparser/entity/BizServer.java index ef5d5eed..c2494708 100644 --- a/unionj-generator-service/src/main/java/cloud/unionj/generator/service/docparser/entity/BizServer.java +++ b/unionj-generator-service/src/main/java/cloud/unionj/generator/service/docparser/entity/BizServer.java @@ -118,9 +118,12 @@ public static BizServer convert(Openapi3 openAPI) { } PathItemWrapper wrapper = new PathItemWrapper(); Path pathEntryValue = pathItemEntry.getValue(); - List tags = pathEntryValue.getTags(); + List tags = pathEntryValue.returnTags(); String serviceName; + /** + * Use second tag as Service class name if it exists, otherwise use first part of the path splitted by back slash. + */ if (CollectionUtils.isNotEmpty(tags) && tags.size() > 1 && StringUtils.isNotBlank(tags.get(1))) { serviceName = StringUtils.capitalize(tags.get(1)) + "Service"; } else { diff --git a/unionj-generator-service/src/test/java/cloud/unionj/generator/service/ServiceFolderGeneratorTest.java b/unionj-generator-service/src/test/java/cloud/unionj/generator/service/ServiceFolderGeneratorTest.java index 903bfe4a..096bef92 100644 --- a/unionj-generator-service/src/test/java/cloud/unionj/generator/service/ServiceFolderGeneratorTest.java +++ b/unionj-generator-service/src/test/java/cloud/unionj/generator/service/ServiceFolderGeneratorTest.java @@ -19,7 +19,7 @@ public class ServiceFolderGeneratorTest { @Test public void generate() throws IOException { - try (BufferedInputStream is = new BufferedInputStream(ClassLoader.getSystemResourceAsStream("test.json"))) { + try (BufferedInputStream is = new BufferedInputStream(ClassLoader.getSystemResourceAsStream("test1.json"))) { BizServer bizServer = ServiceDocParser.parse(is); ServiceFolderGenerator serviceFolderGenerator = new ServiceFolderGenerator.Builder(bizServer).zip(false).build(); String outputFile = serviceFolderGenerator.generate(); diff --git a/unionj-generator-service/src/test/java/cloud/unionj/generator/service/ServiceTsGeneratorTest.java b/unionj-generator-service/src/test/java/cloud/unionj/generator/service/ServiceTsGeneratorTest.java index aa40464f..6c6b8e2e 100644 --- a/unionj-generator-service/src/test/java/cloud/unionj/generator/service/ServiceTsGeneratorTest.java +++ b/unionj-generator-service/src/test/java/cloud/unionj/generator/service/ServiceTsGeneratorTest.java @@ -20,7 +20,7 @@ public class ServiceTsGeneratorTest { @Test public void generate() throws IOException { - try (BufferedInputStream is = new BufferedInputStream(ClassLoader.getSystemResourceAsStream("petstore3.json"))) { + try (BufferedInputStream is = new BufferedInputStream(ClassLoader.getSystemResourceAsStream("test1.json"))) { BizServer bizServer = ServiceDocParser.parse(is); for (BizService bizService : bizServer.getServices()) { ServiceTsGenerator serviceTsGenerator = new ServiceTsGenerator(bizService); diff --git a/unionj-generator-service/src/test/java/cloud/unionj/generator/service/TypesTsGeneratorTest.java b/unionj-generator-service/src/test/java/cloud/unionj/generator/service/TypesTsGeneratorTest.java index 7a88df34..cf370590 100644 --- a/unionj-generator-service/src/test/java/cloud/unionj/generator/service/TypesTsGeneratorTest.java +++ b/unionj-generator-service/src/test/java/cloud/unionj/generator/service/TypesTsGeneratorTest.java @@ -38,7 +38,7 @@ public class TypesTsGeneratorTest { @Test public void generate() throws IOException { - try (BufferedInputStream is = new BufferedInputStream(ClassLoader.getSystemResourceAsStream("test.json"))) { + try (BufferedInputStream is = new BufferedInputStream(ClassLoader.getSystemResourceAsStream("test1.json"))) { BizServer bizServer = ServiceDocParser.parse(is); TypesTsGenerator typesTsGenerator = new TypesTsGenerator(bizServer.getTypes()); String outputFile = typesTsGenerator.generate();