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 Java OpenAPI generation for REST API #2447

Merged
merged 3 commits into from
Aug 24, 2021

Conversation

longnguyen1997
Copy link
Contributor

@longnguyen1997 longnguyen1997 commented Aug 24, 2021

Description:

This PR modifies the OpenAPI YAML spec file in order to support Java REST client generation when using OpenAPITools/openapi-generator.

Notes for reviewer:

  • The previous way of recording 400 errors was actually wrong: you were actually nesting a schema reference within an existing definition, so you ended up with the following.
400:
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/responses/InvalidParameterError'
              example:
                _status:
                  messages:
                    - message: "Invalid parameter: account.id"

became

400:
          description: Bad Request
          content:
            application/json:
              schema:
                description: Invalid parameter
                content:
                  application/json:
                    schema:
                      $ref: '#/components/schemas/Error'
                    example:
                      _status:
                        messages:
                          - message: "Invalid parameter: account.id"
                          - message: Invalid Transaction id. Please use \shard.realm.num-sss-nnn\ format where sss are seconds and nnn are nanoseconds
              example:
                _status:
                  messages:
                    - message: "Invalid parameter: account.id"

, which, of course, breaks the build when you do try to generate the client files. Doing this change resulted in much more consistent response code blocks (400s and 404s now share the same structure of pointing to a reference).

  • Many of the responses outlined in the spec became InlineResponseXXX objects when generated, which is not good for naming. Screenshot included below. I moved these to their own reference names—this also keeps consistency with the overall structure of writing this spec file.

InlineResponse200 (AccountInfo)

InlineResponses

  • We had to delete additional_properties from the spec for state proofs to get generation to work. According to the current alpha API for state proofs, there is no such property in the response, which might explain why it breaks generation.

Screen Shot 2021-08-24 at 1 19 24 PM

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Signed-off-by: Long Nguyen <longnguyen@circle.com>
…ditional_properties on state proofs

Signed-off-by: Long Nguyen <longnguyen@circle.com>
@codecov
Copy link

codecov bot commented Aug 24, 2021

Codecov Report

Merging #2447 (489cb5a) into main (545e340) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##               main    #2447   +/-   ##
=========================================
  Coverage     90.83%   90.83%           
  Complexity     2432     2432           
=========================================
  Files           420      420           
  Lines         11593    11593           
  Branches       1013     1013           
=========================================
  Hits          10530    10530           
  Misses          732      732           
  Partials        331      331           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 545e340...489cb5a. Read the comment docs.

@steven-sheehy steven-sheehy requested a review from a team August 24, 2021 17:27
@steven-sheehy steven-sheehy added bug Type: Something isn't working P2 rest Area: REST API labels Aug 24, 2021
@steven-sheehy steven-sheehy added this to the Mirror 0.40.0 milestone Aug 24, 2021
Nana-EC
Nana-EC previously approved these changes Aug 24, 2021
Copy link
Contributor

@Nana-EC Nana-EC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@steven-sheehy steven-sheehy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Do you mind adding the execution of the openapi generator so that we don't break you guys in the future? Something like this to hedera-mirror-rest/pom.xml:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>${openapi-generator.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/api/v1/openapi.yml</inputSpec>
                <generatorName>java</generatorName>
            </configuration>
        </execution>
    </executions>
</plugin>

xin-hedera
xin-hedera previously approved these changes Aug 24, 2021
Copy link
Collaborator

@xin-hedera xin-hedera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@longnguyencircle
Copy link
Contributor

longnguyencircle commented Aug 24, 2021

Thank you all so much for the approvals! I'll be adding another commit with the actual generation execution @steven-sheehy mentioned.

Signed-off-by: Long Nguyen <longnguyen@circle.com>
@longnguyencircle
Copy link
Contributor

Looks like the generation succeeded in REST API / test (v1) (pull_request)!

[INFO] writing file /home/runner/work/hedera-mirror-node/hedera-mirror-node/hedera-mirror-rest/target/generated-sources/openapi/src/main/java/org/openapitools/client/model/AccountsResponse.java
. . . . .

Copy link
Member

@steven-sheehy steven-sheehy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@steven-sheehy steven-sheehy merged commit c723185 into hashgraph:main Aug 24, 2021
@longnguyencircle longnguyencircle deleted the fix-java-generation branch August 25, 2021 17:29
longnguyencircle pushed a commit to longnguyencircle/hedera-mirror-node that referenced this pull request Aug 25, 2021
* Fix 400 ref bug in OpenAPI spec
* Add properly named response objects for major API calls and delete additional_properties on state proofs
* Add Java generation of OpenAPI spec for validation

Signed-off-by: Long Nguyen <longnguyen@circle.com>
longnguyencircle pushed a commit to longnguyencircle/hedera-mirror-node that referenced this pull request Aug 25, 2021
* Fix 400 ref bug in OpenAPI spec
* Add properly named response objects for major API calls and delete additional_properties on state proofs
* Add Java generation of OpenAPI spec for validation

Signed-off-by: Long Nguyen <longnguyen@circle.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Type: Something isn't working P2 rest Area: REST API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants