Git Product home page Git Product logo

json2dto's Introduction

Json 2 DTO

Build Status StyleCI Latest Version on Packagist Total Downloads

Spatie's Data Transfer Object library is awesome, but typing out DTOs can quickly become a chore. Inspired by Json2Typescript style tools, we built one for PHP DTOs.

Web Version

Try it out at https://json2dto.atymic.dev
The web version has all of the same tools as the cli version in an easy to use GUI. When generating nested DTOs, the tool will create a zip file.

CLI Tool

Prefer to use the tool locally? You can install json2dto via composer and generate files directly from json files.

composer global require atymic/json2dto # Install Globally

composer require atymic/json2dto --dev # Install locally in a project

Usage

The tool accepts json input either as a filename (second argument) or via stdin.
You should run the tool in the root of your project (where your composer.json is located) as it will resolve namespaces based on your PSR4 autoloading config. If you aren't using PSR4, your generated folder structure might not match.

Examples

# Generate PHP 7.4 typed DTO
./vendor/bin/json2dto generate "App\DTO" test.json -name "Test" --typed

# Generate PHP 8.0 typed DTO (DTO V3)
./vendor/bin/json2dto generate "App\DTO" test.json -name "Test" --v3

# Generate a flexible DTO (with nested DTOs)
./vendor/bin/json2dto generate "App\DTO" test.json -name "Test" --nested --flexible

# Generate a DTO from stdin
wget http://example.com/cat.json | ./vendor/bin/json2dto generate "App\DTO" -name Cat

Usage

json2dto generate [options] [--] <namespace> [<json>]

Arguments:
  namespace                       Namespace to generate the class(es) in
  json                            File containing the json string

Options:
      --nested                    Generate nested DTOs
      --typed                     Generate PHP >= 7.4 strict typing
      --flexible                  Generate a flexible DTO
      --dry                       Dry run, print generated files
      --v3                        Generate V3 DTO
  -h, --help                      Display this help message

json2dto's People

Contributors

atymic avatar dependabot[bot] avatar hornet-wing avatar mfn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

json2dto's Issues

When keys have a duplicate value, their DTO get overwritten

I was fetching some posts with their corresponding insights data from the Facebook graph API into my Laravel app and wanted to generate their DTO's, but the result is missing some DTO's.

Here is the response for the facebook call

{
  "data": [
    {
      "insights": {
        "data": [
          {
            "name": "post_impressions",
            "period": "lifetime",
            "values": [
              {
                "value": 614
              }
            ],
            "title": "Lifetime Post Total Impressions",
            "description": "Lifetime: The number of times your Page's post entered a person's screen. Posts include statuses, photos, links, videos and more. (Total Count)"
          }
        ],
        "paging": {
          "previous": "xxxxxxxxxxxxxxx",
          "next": "yyyyyyyyyyyyyyy"
        }
      },
      "created_time": "2021-10-13T16:11:55+0000",
      "message": "Very important message"
    }
  ],
  "paging": {
    "cursors": {
      "before": "xxxxxxxxxxxxxxx",
      "after": "yyyyyyyyyyyyyyy"
    },
    "next": "zzzzzzzzzz"
  }
}

As you can see, there is paging for the data and paging for the insights, but the general response contains "data" aswell as the insights. When generating nested DTO's we only get 1 paging and 1 data DTO, while we expect 2.

Maybe we can keep track of the parent key and store children in their own folder/namespace?

Generating nested DTOs

It would be good if the tool could automatically generate nested DTOs. Might be better suited to the CLI version, see #1

Further Test Coverage

There's currently no test coverage, so it would be a good to add some tests :)
I plan to do this soon, but if anyone would like to help I'd love a PR!

Installation failed on Laravel 8

Hey,

unfortunately the cli installation is failing in my project.

composer require atymic/json2dto --dev

$ composer require atymic/json2dto --dev
Using version ^0.2.2 for atymic/json2dto
./composer.json has been updated
Running composer update atymic/json2dto
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires atymic/json2dto ^0.2.2 -> satisfiable by atymic/json2dto[0.2.2].
    - atymic/json2dto 0.2.2 requires illuminate/support ~5.5 || ~6.0 -> found illuminate/support[v5.5.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev] but these were not loaded, likely because it conflicts with another require.


Installation failed, reverting ./composer.json and ./composer.lock to their original content.

composer.json

"require": {
    "php": "^8.0",
    "fideloper/proxy": "^4.4",
    "fruitcake/laravel-cors": "^2.0",
    "guzzlehttp/guzzle": "^7.0.1",
    "laravel/framework": "^8.40",
    "laravel/jetstream": "^2.3",
    "laravel/sanctum": "^2.6",
    "laravel/tinker": "^2.5",
    "livewire/livewire": "^2.0",
    "lorisleiva/laravel-actions": "^2.1",
    "spatie/data-transfer-object": "^3.1",
    "spatie/laravel-enum": "^2.5",
    "wulfheart/pretty_routes": "^0.3.0"
  },
  "require-dev": {
    "barryvdh/laravel-debugbar": "^3.5",
    "barryvdh/laravel-ide-helper": "^2.10",
    "facade/ignition": "^2.5",
    "fakerphp/faker": "^1.9.1",
    "laravel-shift/blueprint": "^1.24",
    "laravel/sail": "^1.0.1",
    "mockery/mockery": "^1.4.2",
    "nunomaduro/collision": "^5.0",
    "phpunit/phpunit": "^9.3.3",
    "styleci/cli": "^1.1",
    "wulfheart/restfulsac": "dev-master"
  }

Nested DTO generation is blocked by CORS

When generating a nested DTO on json2dto.atymic.dev you get a generic error: Failed to generate DTO.

Upon inspecting in the console it seems to be a CORS issue.

Access to XMLHttpRequest at 'https://json2dto-max7gzuovq-uc.a.run.app/' from origin 'https://json2dto.atymic.dev' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The headers are being added in the source so I can't further debug this. Maybe it is some setting on the hosting somehow?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.