Skip to content

Adds metadata to contracts#1466

Merged
marcingrzejszczak merged 2 commits intomasterfrom
issues_#1465_metadata
Aug 4, 2020
Merged

Adds metadata to contracts#1466
marcingrzejszczak merged 2 commits intomasterfrom
issues_#1465_metadata

Conversation

@marcingrzejszczak
Copy link
Copy Markdown
Contributor

fixes #1340

  • you can add the following entry in the wiremock metadata map
Contract.make {
		request {
			method GET()
			url '/drunks'
		}
		response {
			status OK()
			body([
				count: 100
			])
			headers {
				contentType("application/json")
			}
		}
		metadata([wiremock: '''\
	{
		"response" : {
			"delayDistribution": {
                    "type": "lognormal",
                    "median": 80,
                    "sigma": 0.4
            }
		}
	}
'''
		])

fixes #1078 , #1406

  • you can add the following entry in the wiremock metadata map (unfortunately webhooks won't work due to a mismatch of versions between WireMock 2.21 (used by extension) and 2.27 (used by Spring Cloud Contract).
Contract.make {
		request {
			method GET()
			url '/drunks'
		}
		response {
			status OK()
			body([
				count: 100
			])
			headers {
				contentType("application/json")
			}
		}
		metadata([wiremock: '''\
	{
		"postServeActions" : {
    "webhook" : {
      "headers" : {
        "Content-Type" : "application/json"
      },
      "method" : "POST",
      "body" : "{ \"result\": \"SUCCESS\" }",
      "url" : "http://localhost:56299/callback"
    }
  }
	}
'''
		])

as for #1084 we could add a metadata entry that would be of restdocs key and there we could apply additional work related to restdocs (wdyt @wkorando @artemptushkin )

cc @Buzzardo - there is documentation changed here so once this gets merged could you please do a editing pass?

@artemptushkin
Copy link
Copy Markdown
Contributor

@marcingrzejszczak hi, sorry what does mean restdocs key? Do you have code sample/example of usage? Metadata looks okay, depends on restdocs view of it

@marcingrzejszczak
Copy link
Copy Markdown
Contributor Author

marcingrzejszczak commented Aug 4, 2020

So in the metadata(...) section you could set e.g. metadata("restdocs",...) or metadata("description",...) in a way similar to this one

name: add-produce
description: Operation for adding a new produce item
request:
  method: POST
  url: /api/v1/produce
  headers:
    Content-Type: application/json;charset=UTF-8
  body:
    name: "Kiwi"
    subName: ""
    quantity: 75
  matchers:
    headers:
    - key: Content-Type
      regex: "application/json.*"
    body:
      - path: $.quantity
        type: by_regex
        value: "[0-9]+"
      - path: $.name
        type: by_regex
        predefined: only_alpha_unicode
  metadata:
   restdocs:
    headers:
    - key: Content-Type
      description: "Produce endpoint expects JSON encoded request bodies"
    body:
      - path: $.name
        description: "Name of new produce item"
      - path: $.subName
        description: "Sub-name of new produce item providing a more specific name for the produce item"
      - path: $.quantity
        description: "Number of produce item currently in stock"
response:
  status: 200
  body:
    id: 10
    name: "Kiwi"
    subName: ""
    quantity: 75
  headers:
    Content-Type: application/json;charset=UTF-8
  matchers:
    body:
      - path: $.id
        type: by_regex
        value: "[0-9]+"
  metadata:
   restdocs:
    headers:
    - key: Content-Type
      description: "Produce endpoint provides responses in JSON"
    body:
      - path: $.id
        description: "Unique id of new produce item"
      - path: $.name
        description: "Name of new produce item"
      - path: $.subName
        description: "Sub-name of new produce item providing a more specific name for the produce item"
      - path: $.quantity
        description: "Number of produce item currently in stock"

That way we'd put it into the metadata map with a restdocs key.

@artemptushkin
Copy link
Copy Markdown
Contributor

It is possible, it is better to add clean documentation for it with a good example, cause we have strange String / yaml / json conversions here.
As I see usage sample on contract side:

metadata("""
    restdocs:
         headers:
             - key: Content-Type
               description: "Produce endpoint provides responses in JSON"
""")

Thanks :)

@marcingrzejszczak
Copy link
Copy Markdown
Contributor Author

There's no example atm that's why we're prototyping. I've reformatted the example

name: add-produce
description: Operation for adding a new produce item
request:
  method: POST
  url: /api/v1/produce
  headers:
    Content-Type: application/json;charset=UTF-8
  body:
    name: "Kiwi"
    subName: ""
    quantity: 75
  matchers:
    headers:
      - key: Content-Type
        regex: "application/json.*"
    body:
      - path: $.quantity
        type: by_regex
        value: "[0-9]+"
      - path: $.name
        type: by_regex
        predefined: only_alpha_unicode
  metadata:
    restdocs:
      headers:
        - key: Content-Type
          description: "Produce endpoint expects JSON encoded request bodies"
      body:
        - path: $.name
          description: "Name of new produce item"
        - path: $.subName
          description: "Sub-name of new produce item providing a more specific name for the produce item"
        - path: $.quantity
          description: "Number of produce item currently in stock"
response:
  status: 200
  body:
    id: 10
    name: "Kiwi"
    subName: ""
    quantity: 75
  headers:
    Content-Type: application/json;charset=UTF-8
  matchers:
    body:
      - path: $.id
        type: by_regex
        value: "[0-9]+"
  metadata:
    restdocs:
      headers:
        - key: Content-Type
          description: "Produce endpoint provides responses in JSON"
      body:
        - path: $.id
          description: "Unique id of new produce item"
        - path: $.name
          description: "Name of new produce item"
        - path: $.subName
          description: "Sub-name of new produce item providing a more specific name for the produce item"
        - path: $.quantity
          description: "Number of produce item currently in stock"

For Groovy it would be either a similar map or just calling rest docs methods

metadata("restdocs": RestDocsStaticMethods.foo().bar().baz())

@artemptushkin
Copy link
Copy Markdown
Contributor

Ah, I get it, it was YAML contract. I'm used to groovy :)

Yes, it is nice so far!

@marcingrzejszczak marcingrzejszczak deleted the issues_#1465_metadata branch January 11, 2021 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow to extend contracts & stubs Need support for webhooks / callback URI Contract stub with non fixed delay Support for postServeAction

2 participants