jonaslagoni / asyncapi-quicktype-template

Template for generating typed models from AsyncAPI message payloads.
6 stars 3 forks source link

Why does generator does not generate data type according to the format in YAML file #33

Open junhan87 opened 1 year ago

junhan87 commented 1 year ago

Hi, I am using this template to geenerate C++ code. I am having issue that the generated data type is not the same as YAML file.

YAML file:

triggeredApiData:
    description: 
    type: array
    items:
      type: object
      properties:
              data:
          description: 
          type: integer
          format: uint32
          minimum: 0
          maximum: 4294967295
      required:
        - "apiDataId"

From above snippet, the format is set as uint32 and max is set to 4294967295 for unsigned type.

Then running this command:

ag $file.FullName @lagoni/asyncapi-quicktype-template --param "quicktypeLanguage=cplusplus" -o $OutputPath\$ModuleName\CPP

Generated code:

    class TriggeredApiDatum {
        public:
        TriggeredApiDatum() :
            data_constraint(0, 4294967295, boost::none, boost::none, boost::none)
        {}
        virtual ~TriggeredApiDatum() = default;

        private:
        ApiDataId api_data_id;
        std::shared_ptr<int64_t> data;
        ClassMemberConstraints data_constraint;

        public:
        /**
         * APIデータ / API Data
         */
        const ApiDataId & get_api_data_id() const { return api_data_id; }
        ApiDataId & get_mutable_api_data_id() { return api_data_id; }
        void set_api_data_id(const ApiDataId & value) { this->api_data_id = value; }

        /**
         * データ / Data
         * * "apiDataId"が"rsCalcCurrentTime"以外の場合に必須。 / Required if "apiDataId" is NOT
         * "rsCalcCurrentTime".
         */
        std::shared_ptr<int64_t> get_data() const { return data; }
        void set_data(std::shared_ptr<int64_t> value) { if (value) CheckConstraint("data", data_constraint, *value); this->data = value; }
    };

std::shared_ptr data data is generated as int64_t instead of uint32_t.

data_constraint(0, 4294967295, boost::none, boost::none, boost::none) Due to the maximum value is 4294967295 hence it is -1 if the type is int64_t. Hence any value pass to data, constraint check will throw exception.

Can you let me know where from the generator is generating int64_t instead of uint32_t from the format tag in YAML file?

Appreciate if you can shed some light to point me out how it is worked and where can I modify the tool.

Thanks.

junhan87 commented 1 year ago

@jonaslagoni can you help to take a look on this?

jonaslagoni commented 1 year ago

Hey @junhan87, I am sorry, I could have sworn I had archived this 🤔

I am working (almost every day) on our own model generation tool under the AsyncAPI umbrella called Modelina. We just recently added C++ support, however, all of these different types are not supported, however easily achievable! I am more than happy to help over there, feel free to reach out on the AsyncAPI slack if you find this interesting.