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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Trending

  • Fix the Target, Precompute Once: A Backend-Free Word-Ladder Solver With a BFS Distance Field
  • How Kestra Unifies Enterprise Workflow Orchestration and Agentic AI
  • AI Agents and Frontier Models: Insights From The Neuron
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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.

By 
John Thompson
·
Apr. 19, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
22.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook