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

feat: add HTTP status code #235

Merged
merged 5 commits into from
Jan 30, 2024

Conversation

jonaslagoni
Copy link
Member

@jonaslagoni jonaslagoni commented Jan 11, 2024

Description
This PR adds the statusCode property to the message object.

The reason for adding it to the message object and not the operation object, is that it feels more natural, as the status code belong to each message being send.

...
channels:
  HttpCustomersChannel:
    address: /customers
    messages:
      HttpCustomersGetMessageResponse:
        description: OK
        payload:
          $ref: '#/components/schemas/CustomerPaginated'
        bindings:
          http:
            statusCode: 200
            bindingVersion: '0.3.0'

      HttpCustomersGetMessageResponse:
        description: Server error, could not complete request
        payload:
          $ref: '#/components/schemas/SomeError'
        bindings:
          http:
            statusCode: 500
            bindingVersion: '0.3.0'

operations:
  GetCustomers:
    action: send
    channel:
       $ref: #/channels/HttpCustomersChannel
    bindings:
      http:
        method: GET
        bindingVersion: '0.3.0'

Related issue(s)
Fixes #234

@smoya
Copy link
Member

smoya commented Jan 12, 2024

The reason for adding it to the message object and not the operation object, is that it feels more

Tell me more, tell me more.

@smoya
Copy link
Member

smoya commented Jan 12, 2024

I understand the status code can't exist in the operation since in one channel can coexist several messages (responses) with a different status code each (200, 202, 500...). You won't be able to do this within a single operation.

smoya
smoya previously approved these changes Jan 12, 2024
Copy link
Member

@smoya smoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀🌔

@smoya
Copy link
Member

smoya commented Jan 12, 2024

Wondering how this will work with Operation Reply objects 🤔

http/README.md Outdated Show resolved Hide resolved
@fmvilas
Copy link
Member

fmvilas commented Jan 12, 2024

Wondering how this will work with Operation Reply objects 🤔

@jonaslagoni I think it's important we clarify this. It would be great to have this info written in the binding document too.

@jonaslagoni
Copy link
Member Author

When you define your application as a REST server i.e. you receive a request where you respond with a reply that has specific status codes it looks like this:

asyncapi: 3.0.0
info:
  title: REST server
  version: 0.0.1
defaultContentType: application/json
servers:
  HttpServer:
    host: 'http://localhost:8080/api'
    protocol: http
    security:
      - type: http
        scheme: basic
      - type: http
        scheme: bearer
        bearerFormat: JWT
channels:
  HttpCustomersChannel:
    address: /customers
    servers:
      - $ref: '#/servers/HttpServer'
    messages:
      HttpCustomersGetMessage:
        payload:
          type: 'null'
      HttpCustomersGetMessageResponse:
        description: OK
        payload:
          $ref: '#/components/schemas/CustomerPaginated'
        bindings:
          http:
            statusCode: 200
operations:
  onListCustomers:
    action: receive
    description: listCustomers
    channel:
      $ref: '#/channels/HttpCustomersChannel'
    messages:
      - $ref: '#/channels/HttpCustomersChannel/messages/HttpCustomersGetMessage'
    reply:
      channel:
        $ref: '#/channels/HttpCustomersChannel'
      messages:
        - $ref: >-
            #/channels/HttpCustomersChannel/messages/HttpCustomersGetMessageResponse
components:
  schemas:
    Customer:
      type: object
    CustomerPaginated:
      type: object

When you define your application as interacting with a REST server as a client where are the one sending the request, nothing changes for the messages nor the status code 🤔

asyncapi: 3.0.0
info:
  title: REST client
  version: 0.0.1
defaultContentType: application/json
servers:
  HttpServer:
    host: 'http://localhost:8080/api'
    protocol: http
    security:
      - type: http
        scheme: basic
      - type: http
        scheme: bearer
        bearerFormat: JWT
channels:
  HttpCustomersChannel:
    address: /customers
    servers:
      - $ref: '#/servers/HttpServer'
    messages:
      HttpCustomersGetMessage:
        payload:
          type: 'null'
      HttpCustomersGetMessageResponse:
        description: OK
        payload:
          $ref: '#/components/schemas/CustomerPaginated'
        bindings:
          http:
            statusCode: 200
operations:
  onListCustomers:
    action: send
    description: listCustomers
    channel:
      $ref: '#/channels/HttpCustomersChannel'
    messages:
      - $ref: '#/channels/HttpCustomersChannel/messages/HttpCustomersGetMessage'
    reply:
      channel:
        $ref: '#/channels/HttpCustomersChannel'
      messages:
        - $ref: >-
            #/channels/HttpCustomersChannel/messages/HttpCustomersGetMessageResponse
components:
  schemas:
    Customer:
      type: object
    CustomerPaginated:
      type: object

In short, it just means that messages referenced in the operation should NOT have statusCode defined, but messages defined in the reply section SHOULD 🤔

How do you folks suggest we clarify the interaction here in a meaningful way 🤔 ?

@fmvilas
Copy link
Member

fmvilas commented Jan 17, 2024

In short, it just means that messages referenced in the operation should NOT have statusCode defined, but messages defined in the reply section SHOULD 🤔

I think we can say that statusCode in the operation messages (not reply) can simply be ignored. We only take it into account inside the reply section. How does that sound?

@jonaslagoni
Copy link
Member Author

@fmvilas added the following as description (feel free to reformat and change it as needed):

The HTTP response status code according to [RFC 9110](https://httpwg.org/specs/rfc9110.html#overview.of.status.codes). `statusCode` is only relevant for messages referenced by the [operation reply object](https://www.asyncapi.com/docs/reference/specification/v3.0.0#operationReplyObject) as they define the status code for the response, in all other cases this can safely be ignored.

Copy link
Member

@fmvilas fmvilas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made one more proposal.

http/README.md Outdated Show resolved Hide resolved
Co-authored-by: Fran Méndez <[email protected]>
@jonaslagoni jonaslagoni requested a review from fmvilas January 18, 2024 22:02
@jonaslagoni
Copy link
Member Author

@smoya anything from your side?

Copy link
Member

@smoya smoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In short, it just means that messages referenced in the operation should NOT have statusCode defined, but messages defined in the reply section SHOULD 🤔

I think we can say that statusCode in the operation messages (not reply) can simply be ignored. We only take it into account inside the reply section. How does that sound?

I don't feel this is very user friendly but I can't find a better alternative considering the status code SHOULD be located in the Message Object because the reasons already mentioned.

So 👍 in my side.

@jonaslagoni
Copy link
Member Author

Added the JSON schema changes here: asyncapi/spec-json-schemas#484

Feel free to merge in any order you see fit 🙂

@jonaslagoni
Copy link
Member Author

/rtm

@asyncapi-bot asyncapi-bot merged commit f3116d7 into asyncapi:master Jan 30, 2024
6 checks passed
@jonaslagoni jonaslagoni deleted the add_status_code_http branch January 30, 2024 04:15
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.

How to define response code for HTTP
4 participants