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

[BUG]: Setting leadingComments causes incorrect go code to be emitted #2670

Open
miam-miam opened this issue Dec 11, 2024 · 0 comments
Open
Labels

Comments

@miam-miam
Copy link

Issue Type

The quicktype output when generating go code is incorrect and uncompilable due to it not correctly adding all the imports to the top of the file when setting leadingComments.

Context (Environment, Version, Language)

Input Format: JSON Schema
Output Language: Go

CLI, npm, or app.quicktype.io: npm (quicktype-core)
Version: v23.0.170

Description

I am trying to create a set of types in a single go file by adding multiple JSON schemas together.

Input Data

const geoSchema = {
  id: 'http://json-schema.org/geo',
  $schema: 'http://json-schema.org/draft-06/schema#',
  description: 'A geographical coordinate',
  type: 'object',
  properties: {
    longitude: {
      type: 'number',
    },
  },
};

const blogPostSchema = {
  $id: 'https://example.com/blog-post.schema.json',
  $schema: 'https://json-schema.org/draft/2020-12/schema',
  description: 'A representation of a blog post',
  type: 'object',
  properties: {
    publishedDate: { type: 'string', format: 'date-time' },
  },
};

Expected Behaviour / Output

package templates

import "encoding/json"
import "time"

func UnmarshalGeoCord(data []byte) (GeoCord, error) {
        var r GeoCord
        err := json.Unmarshal(data, &r)
        return r, err
}

func (r *GeoCord) Marshal() ([]byte, error) {
        return json.Marshal(r)
}

func UnmarshalBlogPost(data []byte) (BlogPost, error) {
        var r BlogPost
        err := json.Unmarshal(data, &r)
        return r, err
}

func (r *BlogPost) Marshal() ([]byte, error) {
        return json.Marshal(r)
}

// A geographical coordinate
type GeoCord struct {
        Longitude *float64 `json:"longitude,omitempty"`
}

// A representation of a blog post
type BlogPost struct {
        PublishedDate *time.Time `json:"publishedDate,omitempty"`
}

Current Behaviour / Output

Notice how the import "time" is in the middle of the file which is invalid in go.

package templates

import "encoding/json"

func UnmarshalGeoCord(data []byte) (GeoCord, error) {
        var r GeoCord
        err := json.Unmarshal(data, &r)
        return r, err
}

func (r *GeoCord) Marshal() ([]byte, error) {
        return json.Marshal(r)
}

func UnmarshalBlogPost(data []byte) (BlogPost, error) {
        var r BlogPost
        err := json.Unmarshal(data, &r)
        return r, err
}

func (r *BlogPost) Marshal() ([]byte, error) {
        return json.Marshal(r)
}

// A geographical coordinate
type GeoCord struct {
        Longitude *float64 `json:"longitude,omitempty"`
}

import "time"

// A representation of a blog post
type BlogPost struct {
        PublishedDate *time.Time `json:"publishedDate,omitempty"`
}

Steps to Reproduce

async function main() {
  const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());

  await schemaInput.addSource({
    name: 'geoCord',
    schema: JSON.stringify(geoSchema),
  });

  await schemaInput.addSource({
    name: 'blogPost',
    schema: JSON.stringify(blogPostSchema),
  });

  const inputData = new InputData();
  inputData.addInput(schemaInput);

  const file = await quicktype({
    inputData,
    leadingComments: [],
    lang: 'go',
    rendererOptions: {
      package: 'templates',
    },
  });

  console.log(file.lines.join('\n'));
}

main();

Possible Solution

Looks like potentially this line is wrong

and should instead still collect imports even if leadingComments is not set to undefined

@miam-miam miam-miam added the bug label Dec 11, 2024
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