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

C++ output with "https://app.quicktype.io/" is very different from the one generated locally with master repo #2592

Closed
samacumen opened this issue May 12, 2024 · 1 comment
Labels

Comments

@samacumen
Copy link

samacumen commented May 12, 2024

Go to https://app.quicktype.io/

  • select C++,
  • turn off boost option
  • Compare output from web interface Vs locally generated from the master branch for C++. It is widely different.

Issue Type: Bug or variation in code generation. Problem with Input parsing.

Context (Environment, Version, Language)

Ubuntu 22.04 LTS

Input Format: Default JSON Schema
Output Language: C++

CLI, npm, or app.quicktype.io: Used app.quicktype and local npm based code generation
Version: 23.0.0.0 (when using npm)

Description

I am trying to get the correct output meant for C++ for a given schema.

Input Data

INPUT: The JSON schema is:

{
  "id": "http://json-schema.org/geo",
  "$schema": "http://json-schema.org/draft-06/schema#",
  "description": "A geographical coordinate",
  "type": "object",
  "properties": {
    "latitude": {
      "type": "number"
    },
    "longitude": {
      "type": "number"
    }
  }
}

The output from the web interface is:

#pragma once

#include <optional>
#include "json.hpp"

#include <optional>
#include <stdexcept>
#include <regex>

...
    /**
     * A geographical coordinate
     */
    class Coordinate {
        public:
        Coordinate() = default;
        virtual ~Coordinate() = default;

        private:
        std::optional<double> latitude;
        std::optional<double> longitude;

        public:
        std::optional<double> get_latitude() const { return latitude; }
        void set_latitude(std::optional<double> value) { this->latitude = value; }

        std::optional<double> get_longitude() const { return longitude; }
        void set_longitude(std::optional<double> value) { this->longitude = value; }
    };
}

namespace quicktype {
    void from_json(const json & j, Coordinate & x);
    void to_json(json & j, const Coordinate & x);

    inline void from_json(const json & j, Coordinate& x) {
        x.set_latitude(get_stack_optional<double>(j, "latitude"));
        x.set_longitude(get_stack_optional<double>(j, "longitude"));
    }

    inline void to_json(json & j, const Coordinate & x) {
        j = json::object();
        j["latitude"] = x.get_latitude();
        j["longitude"] = x.get_longitude();
    }
}

The output from locally generated C++ code for master branch (v23.0.0.0) of quickType is:

$ cd quicktype/
$ npm install quicktype-core
$ npm install

# For compilation
$ script/quicktype
$ ./script/quicktype -l c++ --no-boost --src Coordinate.json --out Coordinate.cpp
#pragma once

#include "json.hpp"

#include <optional>
#include <stdexcept>
#include <regex>

namespace quicktype {
    using nlohmann::json;

..

    class Itude {
        public:
        Itude() = default;
        virtual ~Itude() = default;

        private:
        std::string type;

        public:
        const std::string & get_type() const { return type; }
        std::string & get_mutable_type() { return type; }
        void set_type(const std::string & value) { this->type = value; }
    };

    class Properties {
        public:
        Properties() = default;
        virtual ~Properties() = default;

        private:
        Itude latitude;
        Itude longitude;

        public:
        const Itude & get_latitude() const { return latitude; }
        Itude & get_mutable_latitude() { return latitude; }
        void set_latitude(const Itude & value) { this->latitude = value; }

        const Itude & get_longitude() const { return longitude; }
        Itude & get_mutable_longitude() { return longitude; }
        void set_longitude(const Itude & value) { this->longitude = value; }
    };

    class Coordinate {
        ..
    };
}

..

Possible Solution

I would like to determine where I am going wrong. Why are the outputs widely different via https://app.quicktype.io/ and local code generation via npm, when every option is exactly the same.

Looking forward to your answer.

Thanks!

@samacumen samacumen added the bug label May 12, 2024
@samacumen samacumen changed the title https://app.quicktype.io/ output is very different from master repo output C++ output with "https://app.quicktype.io/" is very different from the one generated locally with master repo May 12, 2024
@samacumen
Copy link
Author

I missed the -s option (which is generic) "quicktype -s schema". Now, the outputs are the same. Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant