Skip to content

Commit

Permalink
Regenerate request/response for the info operation (opensearch-proj…
Browse files Browse the repository at this point in the history
…ect#1026)

* Tweak formatting rules used by codegen to allow reformatting javadoc

Signed-off-by: Thomas Farr <[email protected]>

* Support codegen for request shapes that may be singleton

Signed-off-by: Thomas Farr <[email protected]>

* Generate info operation shapes

Signed-off-by: Thomas Farr <[email protected]>

---------

Signed-off-by: Thomas Farr <[email protected]>
(cherry picked from commit dea7e72)
  • Loading branch information
Xtansia committed Aug 30, 2024
1 parent 80b2e29 commit 499efde
Show file tree
Hide file tree
Showing 13 changed files with 48,344 additions and 34 deletions.
362 changes: 362 additions & 0 deletions buildSrc/formatterConfig-generated.xml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions java-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ application {
tasks.named<JavaExec>("run") {
args = listOf(
"--input", "$projectDir/opensearch-openapi.yaml",
"--eclipse-config", "$rootDir/buildSrc/formatterConfig.xml",
"--eclipse-config", "$rootDir/buildSrc/formatterConfig-generated.xml",
"--output", "${project(":java-client").projectDir}/src/generated/java/"
)
}
Expand Down Expand Up @@ -255,9 +255,9 @@ spotless {
importOrder()
removeUnusedImports()

eclipse().configFile("../buildSrc/formatterConfig.xml")
eclipse().configFile("../buildSrc/formatterConfig-generated.xml")

trimTrailingWhitespace()
endWithNewline()
}
}
}
47,935 changes: 47,935 additions & 0 deletions java-codegen/opensearch-openapi.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public String getResponseType() {
return responseClassName(operationGroup);
}

public boolean canBeSingleton() {
return !hasRequestBody() && !hasQueryParams() && hasSinglePath() && !getFirstPath().hasParams();
}

public boolean hasRequestBody() {
return !getBodyFields().isEmpty();
}
Expand All @@ -109,7 +113,7 @@ public Collection<HttpPath> getHttpPaths() {
return httpPaths;
}

public HttpPath getFirstHttpPath() {
public HttpPath getFirstPath() {
return httpPaths.get(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.NotImplementedException;
Expand Down Expand Up @@ -89,7 +90,7 @@ public void visit(@Nonnull OpenApiSpecification spec) {
private void visit(@Nonnull OperationGroup group, @Nonnull List<OpenApiOperation> variants) {
LOGGER.info("Visiting Operation Group: {} [{} variants]", group, variants.size());

var parent = root.child(group.getNamespace().orElse(null));
var parent = root.child(group.getNamespace().orElse("core"));

var requestShape = visit(parent, group, variants);
parent.addOperation(requestShape);
Expand Down Expand Up @@ -349,21 +350,16 @@ private Type mapTypeInner(OpenApiSchema schema) {
}

private Type mapOneOf(List<OpenApiSchema> oneOf) {
if (oneOf.size() == 2) {
var first = oneOf.get(0);
var second = oneOf.get(1);
if (isOneOfSingleAndArray(oneOf)) {
return mapType(oneOf.get(1));
}

if (isOneOfSingleAndArray(oneOf)) {
return mapType(second);
}
var types = oneOf.stream().map(OpenApiSchema::determineTypes).flatMap(Set::stream).collect(Collectors.toSet());

if ((first.isString() && (second.isString() || second.isNumber())) || (first.isNumber() && second.isString())) {
return Types.Java.Lang.String;
}

if (first.isBoolean() && second.isString()) {
return Types.Primitive.Boolean;
}
if (types.size() == 2
&& types.contains(OpenApiSchemaType.String)
&& (types.contains(OpenApiSchemaType.Boolean) || types.contains(OpenApiSchemaType.Number))) {
return Types.Java.Lang.String;
}

throw new UnsupportedOperationException("Can not get type name for oneOf: " + oneOf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

public enum HttpStatusCode {
Ok("200"),
Created("201"),
BadRequest("400"),
Forbidden("403"),
NotFound("404"),
InternalServerError("500"),
NotImplemented("501");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
public static class Builder extends {{TYPES.Client.Util.ObjectBuilderBase}} implements {{TYPES.Client.Util.ObjectBuilder}}<{{className}}> {
{{#fields}}
@{{TYPES.Javax.Annotation.Nullable}} private {{type.boxed}} {{name}};
{{^required}}@{{TYPES.Javax.Annotation.Nullable}} {{/required}}private {{type.boxed}} {{name}};
{{/fields}}

{{#fields}}
Expand Down Expand Up @@ -74,4 +74,4 @@
return new {{className}}(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/**

{{>ObjectShape/FieldDoc/BasicInner}}

*/
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{{#description}}
* <p>{{#required}}Required - {{/required}}{{.}}</p>
{{/description}}

* <p>API name: {@code {{wireName}}}</p>
* {{#required}}Required - {{/required}}{{#description}}{{.}}<br/>{{/description}}API name: {@code {{wireName}}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
{{>ObjectShape/FieldDoc/BasicInner}}

* <p>Adds one or more values to <code>{{name}}</code>.</p>
*/
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
{{>ObjectShape/FieldDoc/BasicInner}}

* <p>Adds all elements of <code>list</code> to <code>{{name}}</code>.</p>
*/
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
{{>ObjectShape/FieldDoc/BasicInner}}

* <p>Adds a value to <code>{{name}}</code> using a builder lambda.</p>
*/
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{{>ObjectShape/ClassDeclaration}} {
{{#canBeSingleton}}
public {{className}}() {}

/**
* Singleton instance for {@link {{className}}}.
*/
public static final {{className}} _INSTANCE = new {{className}}();
{{/canBeSingleton}}
{{^canBeSingleton}}
{{>ObjectShape/Fields}}

// ---------------------------------------------------------------------------------------------
Expand All @@ -19,6 +28,7 @@
// ---------------------------------------------------------------------------------------------

{{>ObjectShape/Deserialize}}{{/hasRequestBody}}
{{/canBeSingleton}}

// ---------------------------------------------------------------------------------------------

Expand All @@ -29,11 +39,21 @@
// Request method
request -> {{#quoted}}{{httpMethod}}{{/quoted}},
// Request path
request -> {
request ->
{{#hasSinglePath}}
{{#firstPath}}{{>RequestShape/HttpPathBuilder}}{{/firstPath}}
{{#firstPath}}
{{#hasParams}}
{
{{>RequestShape/HttpPathBuilder}}
}
{{/hasParams}}
{{^hasParams}}
{{#quoted}}{{this}}{{/quoted}}
{{/hasParams}}
{{/firstPath}}
{{/hasSinglePath}}
{{^hasSinglePath}}
{
{{#indexedPathParams}}
final int _{{left}} = 1 << {{right}};
{{/indexedPathParams}}
Expand All @@ -51,8 +71,8 @@
{{/httpPaths}}

throw {{TYPES.Client.Transport.Endpoints.SimpleEndpoint}}.noPathTemplateFound("path");
{{/hasSinglePath}}
},
}
{{/hasSinglePath}},
// Request parameters
{{#hasQueryParams}}
request -> {
Expand Down

0 comments on commit 499efde

Please sign in to comment.