glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
11.76k stars 1.04k forks source link

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

Closed samacumen closed 1 month ago

samacumen commented 1 month ago

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

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 commented 1 month ago

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