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

Adds metadata to contracts #1466

Merged
merged 2 commits into from Aug 4, 2020
Merged

Adds metadata to contracts #1466

merged 2 commits into from Aug 4, 2020

Conversation

marcingrzejszczak
Copy link
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?

@marcingrzejszczak marcingrzejszczak added this to the 3.0.0-M4 milestone Aug 3, 2020
@marcingrzejszczak marcingrzejszczak added this to In progress in 2020.0.0-M4 via automation Aug 3, 2020
@marcingrzejszczak marcingrzejszczak merged commit 242c820 into master Aug 4, 2020
2 of 4 checks passed
2020.0.0-M4 automation moved this from In progress to Done Aug 4, 2020
@artemptushkin
Copy link
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
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
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
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
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
No open projects
2020.0.0-M4
  
Done
2 participants