asyncapi / spec

The AsyncAPI specification allows you to create machine-readable definitions of your asynchronous APIs.
https://www.asyncapi.com
Apache License 2.0
4.2k stars 268 forks source link

usage of schema vs payload #427

Closed WAvdBeek closed 4 years ago

WAvdBeek commented 4 years ago

Can I get a clarification on the usage of schema vs payload

Just looking at the example about security of the tutorial: https://www.asyncapi.com/docs/getting-started/security/ This example has a payload object hello-message, which contains the schema definition of the json payload but it also references the schema snippet send-at. what is the preferred usage of schemas vs payload?

Note that I have created a few AsyncAPI models without the keyword schema, and tried to generate code. before commencing code generation I have validate the input with spectral lint.

The python code gives the following error: $ ag ../sdfobject-airquality.async.json @asyncapi/python-paho-template -o py3 S sdfobject-airquality.async.zip

omething went wrong: Template render error: (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\@asyncapi\python-paho-template\template\main.py) [Line 7, Column 48] Error: Unable to call the return value of (asyncapi["components"])["schemas"], which is undefined or falsey at Object._prettifyError (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\lib.js:36:11) at C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:561:19 at Template.root [as rootRenderFunc] (eval at _compile (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), :291:3) at Template.render (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:550:10) at Environment.renderString (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:378:17) at Promise (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\lib\generator.js:612:21) at new Promise () at Generator.renderString (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\lib\generator.js:611:12) at Generator.renderFile (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\lib\generator.js:635:17) at

if I run another template like @asyncapi/html-template or @asyncapi/nodejs-template it works fine.

attached the zipped used input file.

github-actions[bot] commented 4 years ago

Welcome to AsyncAPI. Thanks a lot for reporting your first issue.

Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

jonaslagoni commented 4 years ago

what is the preferred usage of schemas vs payload?

@WAvdBeek depends on your preference I guess. schemas are great to have all the schemas in one place easy to locate and reference. payload are used to describe what schema is used for the message, but in itself is a schema i.e. you could do:

components:
  messages:
    hello-msg:
      payload:
         $ref: '#/components/schemas/testPayload'
    goodbye-msg:
      payload:
        type: object
        properties:
          sentAt:
            $ref: '#/components/schemas/sent-at'
  schemas:
    testPayload: 
        type: object
        properties:
          name:
            type: string
          sentAt:
            $ref: '#/components/schemas/sent-at'
    sent-at:
      type: string
      description: The date and time a message was sent.
      format: datetime

Seems like you encountered a problem with the template itself not being able to handle something. Can you add your document in a comment instead of a zip file please 😄 ?

derberg commented 4 years ago

My guess is that the question is motivated by the fact that Python template, like the Java Spring Cloud Stream template, does not support schemas in a payload on a channel level. I saw @damaru-inc following in Java template approach to only grab schemas from components.schemas so this is why I guess the same was done in Python.

@WAvdBeek try moving payload schemas to components.schemas and use $ref in payload to refer the schema from components. And if by doing this it will work, also report an issue in python template to improve this part

WAvdBeek commented 4 years ago

Thanks for the info! I made the change to use the component/schema as definition part and used the $ref to reference it. the file is still valid according spectral, and I can still generate the html & node.js but now the python code fails in a different way. i will report the same as error on the python code generator

hereby all info:

$ spectral lint ./sdfobject-airqualitypublisher.ascyncapi.json AsyncAPI 2.x detected No results with a severity of 'error' or higher found!

{ "asyncapi": "2.0.0", "info": { "title": "Air Quality", "version": "2019-06-13", "license": { "name": "one Data Model License", "url": "https://github.com/one-data-model/oneDM/blob/master/LICENSE", "x-copyright": "copyright 2016-2017, 2019 Open Connectivity Foundation, Inc. All rights reserved." }, "termsOfService": "https://github.com/one-data-model/oneDM/blob/master/LICENSE", "description": " Generated from SDF Air Quality ", "contact": { "name": "API Support", "url": "http://www.example.com/support", "email": "support@example.com" } }, "tags": [ { "name": "SDF" }, { "name": "Generated" } ], "servers": { "mosquitto": { "url": "mqtt://test.mosquitto.org", "protocol": "mqtt" } }, "channels": { "Airquality": { "publish": { "operationId": "publish-airquality", "description": "This Resource describes a qualitative or measured contaminant that can be used to infer Air Quality. The Property 'valueType' indicates a qualitative or measured reading within the contaminantvalue Property. The Property 'contaminantvalue' can contain the actual sensed value with units per contaminant type. Qualitative is a representative value within the range provided where the minimum value is minimum contamination and maximum value is maximum contamination for the specific contaminant. The Property 'contaminantvalue' contains the actual measured or qualitative level. The Property 'range' contains the allowed range for the value that is being reported. If valueType is 'Measured' then the units for the contaminant types are as follows: Methanol (also known as Formaldehyde): CH2O (ug/m^3), Carbon Dioxide: CO2 (ppm), Carbon Monoxide: CO (ppm), Particulate Matter (less than 1 micron in diameter): PM1 (ug/m^3), Particulate Matter (less than 2. 5 microns in diameter): PM2. 5 (ug/m^3), Particulate Matter (less than 10 microns in diameter): PM10 (ug/m^3), Volatile Organic Compounds: VOC (ug/m^3).", "message": { "payload": { "$ref": "#/components/schemas/airquality" } } } } }, "components": { "schemas": { "airquality": { "type": "object", "properties": { "contaminanttype": { "description": "The contaminant being measured.", "enum": [ "CH2O", "CO2", "CO", "PM1", "PM2.5", "PM10", "VOC", "Smoke", "Odor", "AirPollution" ], "type": "string", "readOnly": true }, "valuetype": { "description": "The property that indicates whether the provided value is qualitative or measured.", "enum": [ "Qualitative", "Measured" ], "type": "string", "readOnly": true }, "contaminantvalue": { "description": "The measured or qualitative value for the contaminant.", "type": "integer", "readOnly": true }, "range": { "description": "The valid range for the Property in the Resource as an integer. The first value in the array is the minimum value, the second value in the array is the maximum value.", "items": { "type": "integer" }, "maxItems": 2, "minItems": 2, "type": "array", "readOnly": true } } } } } }

the ag tool is still crashing

$ ag ./sdfobject-airqualitypublisher.ascyncapi.json @asyncapi/python-paho-template -o py5 Something went wrong: Template render error: (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\@asyncapi\python-paho-template\template\$$schema$$.py) TypeError: typeMap.get is not a function at Object._prettifyError (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\lib.js:36:11) at C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:561:19 at eval (eval at _compile (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), :11:11) at C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:611:9 at Template.root [as rootRenderFunc] (eval at _compile (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), :361:3) at Template.getExported (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:609:10) at eval (eval at _compile (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), :10:5) at createTemplate (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:313:9) at handle (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:325:11) at C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:337:9 at next (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\lib.js:326:7) at Object.asyncIter (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\lib.js:332:3) at Environment.getTemplate (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:319:9) at Template.root [as rootRenderFunc] (eval at _compile (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), :8:5) at Template.render (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:550:10) at Environment.renderString (C:\Users\wovander\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:378:17)

$ ag ./sdfobject-airqualitypublisher.ascyncapi.json @asyncapi/html-template -o html2 npm http fetch GET 200 https://registry.npmjs.org/@asyncapi%2fhtml-template 2274ms npm http fetch GET 200 https://registry.npmjs.org/@asyncapi/html-template/-/html-template-0.10.0.tgz 892ms npm http fetch GET 304 https://registry.npmjs.org/fsevents 85ms (from cache) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules\chokidar\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

Done! ✨ Check out your shiny new generated files at C:\Users\wovander\Documents\Wouter\standard-orgs\OIC\github-ocf\async\blah\html2.

$ ag ./sdfobject-airqualitypublisher.ascyncapi.json @asyncapi/nodejs-template -o nodejs1 -p server=mosquitto

Done! ✨ Check out your shiny new generated files at C:\Users\wovander\Documents\Wouter\standard-orgs\OIC\github-ocf\async\blah\nodejs1.

derberg commented 4 years ago

Hey @WAvdBeek I'll close this one as you already reported it here https://github.com/asyncapi/python-paho-template/issues/19 Feel free to reopen in case you have more questions, or just open a new issue