Giter Club home page Giter Club logo

scc-multiapi-converter's Introduction

Codacy Badge Maven Central

SCC Verifier MultiApi Converter

📜 Summary

SCC Verifier MultiApi Converter allows us to use Spring Cloud Contract to generate contracts from an OpenApi yaml document or an AsyncApi document. This plugin will detect automatically what contract needs to be generated

Here is the documentation for these technologies:

🚀 Getting Started

In order to get this plugin working, you need the following things installed in your computer:

  • Java 11 Version
  • Maven

After you have these installed, you need to add the Spring Cloud Contract Maven Plugin in your pom.xml file and extend it with this Converter as a Dependency. Here is an easy example of a basic configuration:

<plugin>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-contract-maven-plugin</artifactId>
  <version>3.0.0</version>
  <extensions>true</extensions>
  <configuration>
    <packageWithBaseClasses>com.sngular.challenge.gameserver.baseclasses</packageWithBaseClasses>
    <contractsDirectory>${project.basedir}/src/main/resources/api</contractsDirectory>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>com.sngular</groupId>
      <artifactId>scc-multiapi-converter</artifactId>
      <version>3.4.1</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</plugin>

🧑🏻‍💻 Usage

Inside the configuration tag, you must declare the contractsDirectory tag. In this tag you must specify the directory that contains the OpenApi/AsyncApi Yamls.

Also, we recommend specifying the baseClassForTests tag. This tag indicates the base class from which the autogenerated test will be extended. This class must be a test class of the producer part in which only a call to the producer method will be made, which is necessary for the test of the producer of the autogenerated class.

Once you specify this in your pom.xml you must use the command: mvn clean install. This will generate the stubs necessary for the contract testing generation under the stubs folder.

It will also generate the ContractVerifierTest class for the producer and consumer tests for your Messaging Api and your Rest Api, in addition to the corresponding yml files with the contracts of both, inside target in the package where your baseClassForTests is located. If you want to change where your contracts will be located you must use the basePackageForTests tag in the pom.xml.

If you need more control over the settings of your project, you can use all the Configuration Options that Spring Cloud Contract Maven Plugin has. These configuration options can be checked in the official documentation webpage under 4.2.7 Section: Spring Cloud Contract Verifier Setup.

✏️ Writing Ymls

This plugin supports most of the OpenApi/Swagger and AsyncApi, but there are a couple of things that must be noted:

  • Using OpenApi/AsyncApi´s example label in the parameters/schemas will check in our contracts that the response is equal to the example value instead of using a Regex.
  • OpenApi: Since 2.4.0 version, we support the definition of parameters in both Path and Operation object, but you can only define it in one of them. ❗❗❗️ We use the Option resolver from OpenApi which will override the Operation parameters if you have a parameter defined in the Path.
  • Please be aware that writing an example must be the same type as indicated in the file, otherwise your contract will break.

This is an easy example of a small YAML for OpenApi that will work with our plugin:

---
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Sngular Challenge Game Server
  description: Test File for SCC MultiApi Plugin.
  contact:
    name: Sngular
    url: sngular.com
    email: [email protected]
  license:
    name: MPL 2.0
servers:
- url: http://localhost:8080/v1
paths:
  /games:
    summary: Hola
    get:
      summary: List all available games
      description: Test File for SCC MultiApi Plugin.
      tags:
      - games
      operationId: listGames
      responses:
        '200':
          description: A paged array of games
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Game"
components:
  schemas:
    Game:
      type: object
      properties:
        player:
          $ref: "#/components/schemas/Player"
    Player:
      type: object
      properties:
        name:
          $ref: "#/components/schemas/Name"
    Name:
      type: object
      properties:
        firstname:
          type: string
          example: John
        lastname:
          type: string
          example: Doe
tags:
- name: games
  description: Test description for SCC MultiApi Plugin.

And here, there is an example of a YAML file for the AsyncApi side:

asyncapi: "2.3.0"
info:
  title: Order Service
  version: 1.0.0
  description: Order management Service
  contact:
    name: Sngular
    url: http://www.asyncapi.org/support
    email: [email protected]
    license:
      name: MPL 2.0
channels:
  orderCreated:
    publish:
      operationId: "publishOperation"
      message:
        $ref: '#/components/messages/OrderCreated'
    description: Operation that will produce an OrderCreated object
  createOrder:
    subscribe:
      operationId: "subscribeOperation"
      message:
        $ref: '#/components/messages/CreateOrder'
    description: Operation that will consume an CreateOrder object
components:
  messages:
    OrderCreated:
      payload:
        $ref: '#/components/schemas/Order'
    CreateOrder:
      payload:
        $ref: '#/components/schemas/Order'
  schemas:
    Order:
      type: object
      properties:
        tableNumber:
          type: integer
          format: int32
          example: 1
        clientRef:
          type: integer
          format: int32
          example: 432

⚠️Current Limitations

Currently, this plugin has some limitations that will be addressed in the future. In order to make it work, we must follow some rules:

OpenApi implementation:

  • Some OpenApi functionalities are not implemented yet, such as creating example objects, instead you must use the example tag in every property of the object.
  • Due to the OpenApi Parser code, when you use a $ref that points to an external file, there are some limitations when using $ref again in that same file.

Async Api implementation:

  • This plugin doesn't allow the use of AllOfs, OneOfs and AnyOfs.
  • Support for generating contracts from avro files.
  • Some AsyncApi functionalities are not implemented yet, such as creating example objects, instead you must use the example tag in every property of the object.

🌐 RoadMap

  • Add implementation to support AllOfs, AnyOfs and OneOfs in AsyncApi.
  • Add support for generating contracts from avro files.
  • Further investigation for OpenApi/AsyncApi and Spring Cloud Contract possibilities.
  • More testing and fixing possible bugs that may occur in the future.
  • Get rid of the OpenApi parser in order to control our own code.

scc-multiapi-converter's People

Contributors

dependabot[bot] avatar dianagomezgar avatar fcampostato avatar isaulloa avatar jemacineiras avatar jmejutovazquez avatar marcingrzejszczak avatar mortizam avatar mslowiak avatar olgacalderon avatar ramilsngular avatar rivarsal avatar torrespro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

scc-multiapi-converter's Issues

[SCC-MultiApi-Plugin][OpenApi] Add support for parameters in Path Object

As of right now, SCC MultiApi Plugin only supports parameters in Operations.

We should develop a way to map the Parameters from the Path Object as well.

Here is an example of a parameter in a Path Object:
image

We should refactor the processRequest method in the OpenApiContractConverter to allow the usage of parameters in the PathObject

image

[SCC-MultiApi-Converter] Add action check to ensure version is updated

As a developer, I want to have a check for the pom.xml version, to avoid errors during deployment stage.

We must analyze how to add a check for the version on PR, maybe including a new job/step on an existent workflow, or maybe adding a new workflow. This must check if the version is updated compared to the main branch, and if it's not, put a message on the PR including a link to the versioning system we are using, SemVer, so the contributor could solve the error.

When this problem is solved, we must spread the solution to another projects with the same problem, SCS Multiapi Plugin and KLoadGen.

[Repo Improvement] Associate GitHub actions to PR tags

When a pull request is merged into main:

  • If it has the documentation tag, the wiki should be updated.
  • If it has the release tag, a new release should be generated.

Adding both tags results in both actions being executed, and it would also be possible to merge without adding any of the two tags.

[SCC-Multiapi-Converter] [OpenApi] Add the possibility of using referenced arrays with $ref Objects inside

Currently, SCC MultiApi does not support the creation of a contract when an array is referenced in a schema and has $ref inside of his items.

This happens inside the processBodyAndMatchers method because we check if the Schema has properties, but an array doesn´t, it has items. As you can see in line 199:

image

Check OpenApi Docs for this issue: https://swagger.io/docs/specification/data-models/data-types/

image

#3 Add License and create github actions for releases

In this issue we have to prepare everything related to release the plugin into Maven and have the correct github actions ready for the github release as well.

We need the following:

  • Add the MPL header in every java file.
  • Add the github actions folder and the necessary files for the release

Failed to execute goal 'generateTests' while trying to generate contracts from an OpenApi yaml document

Hello,

I would like to understand how this library works. So I created a Spring-Boot project based on Spring-Boot 2.7.3 and Spring-Cloud 2021.0.3. As build tool I use Maven 3.8 and to configure the project properly I followed the instructions on your website. As API definition I used the OpenApi example from your website.

When I run the build I get the following error message:

Failed to execute goal org.springframework.cloud:spring-cloud-contract-maven-plugin:3.1.3:generateTests (default-generateTests) on project openapi-scc: Execution default-generateTests of goal org. springframework.cloud:spring-cloud-contract-maven-plugin:3.1.3:generateTests failed: Failed to convert file [C:\opt\ws\work\_openapi-scc\src\main\resources\api\openapi.yml]: Cannot invoke "io.swagger.v3.oas.models.media.Schema.getExample()" because "schema" is null -> [Help 1].

Am I doing something wrong? Is the example missing some things? Is the library having a bug?

The project where I had the problem can be found here:
https://github.com/ingogriebsch/_openapi-scc

[SCC-MultiApi-Converter] Update actions to avoid errors with forks

As a developer, I want the actions that are defined for the project to work with the forked repositories as well, so people can contribute with the project and don't receive an error from the actions.

Now, maven-pr action searchs for the HEAD commit reference inside the same repository and branch, and executes the jobs on that. This makes impossible the use of forked repos, so we need to address it.

Also, if this is possible, we must remove the execution of this action on push to main branch.

When this problem is solved, we must spread the solution to another projects with the same problem.

[SCC-MultiApi-Converter][OpenApi] Rewrite the logic for AnyOfs

As of right now, the logic behind the AnyOfs will create a N number of contracts that will come from the AnyOf´s list.

This creates a problem since the N number and the contract can changed based on the execution of the plugin, making it a lottery for a user to get the right contract at the right time.

As a team, we must review the logic behind this and change it to a behaviour that is manageable for a user.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.