DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Contracts for Microservices With OpenAPI and Spring Cloud Contract

Contracts for Microservices With OpenAPI and Spring Cloud Contract

In this post, I'll explain how to define contracts at the service level using OpenAPI and Spring Cloud Contract.

John Thompson user avatar by
John Thompson
·
Apr. 19, 19 · Tutorial
Like (4)
Save
Tweet
Share
18.07K Views

Join the DZone community and get the full member experience.

Join For Free

I've previously posted about defining Consumer Driven Contracts using the OpenAPI specification and Spring Cloud Contract. This approach works well because you can tie your contracts to your API specification, and also wire in a request validator into your contract tests.

But one problem that remained is typically your OpenAPI specification will span multiple services. Thus, when generating contract tests, you'd get tests generated for all services and not just a specific service.

This leads to headaches in your CI/CD process. If you ran the tests at the service level, contract tests for other services would, of course, fail.

Ideally you'd want contract testing at the service level, and not downstream at the scope of the OpenAPI specification.

To address this problem, I've made an enhancement to my OpenAPI SCC contract parser to allow contract generation at the service level.

This enhancement is to allow contract generation at the microservice level from a OpenAPI specification which is typically from the enterprise level.

In this post, I'll explain how to define contracts at the service level using OpenAPI and Spring Cloud Contract.

This feature is available in release 2.1.1.2 of Spring Cloud Contract OpenAPI and higher.

In the x-contracts extension on the OpenAPI Operations object, declare the service name as follows:

x-contracts:
      -   contractId: 1
          name: Test Get Payor
          contractPath: "/v1/payors/0a818933-087d-47f2-ad83-2f986ed087eb"
          serviceName: payor-service

At this point, if you do nothing, all contracts in the spec will still be parsed.

To filter on the service name, the parser will read a system environment variable called scc.enabled.servicenames.

Spring Cloud Contacts are typically generated via the SCC Maven or Gradle plugins. The plugin configuration is upstream of the parser, thus a system environment variable was the best configuration solution.

For use with Maven, setting the system environment variable is simple. You can use a Maven plugin as shown below.

<!--  Set env prop for contract tests - filters to just this service-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>set-system-properties</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <property>
                                    <name>scc.enabled.servicenames</name>
                                    <value>${artifactId}</value>
                                </property>
                            </properties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Here, I'm setting the environment variable to the artifact id. Thus my 'serviceName' is the same as the Maven artifactId. A good idea from my buddy Mike R.

The value can be any string value. Or any comma separated list of string values if you wish to activate multiple serviceNames.

One side note - if a contact does not have a serviceName value set, the parser will still pick it up.

One other note, if your configuration does not produce any contracts, the SCC Maven plugin will throw an exception. Currently, the SCC YamlContract parser is picking up the OpenAPI YAML document and throwing an exception when trying to parse it. This should be fixed in the next release of Spring Cloud Contract. See this issue for details. At the time of writing, Spring Cloud Contract is on version 2.1.1.RELEASE. I expect the fix will make the next release of Spring Cloud Contract.

Through this configuration, you can have one OpenAPI specification for your APIs, and then re-use the specification to generate contracts at the individual service level.

This post was specifically about using the new service name feature. If you'd like to learn more about defining consumer driven contracts in OpenAPI for Spring Cloud Contract, please see the home page of my project repository.

Spring Cloud Spring Framework microservice OpenAPI Specification

Published at DZone with permission of John Thompson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • Remote Debugging Dangers and Pitfalls
  • Event Driven 2.0
  • The 12 Biggest Android App Development Trends in 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: