quicktype
quicktype copied to clipboard
[BUG]: Setting leadingComments causes incorrect go code to be emitted
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 https://github.com/glideapps/quicktype/blob/29bb8160660a7daa2a837ac0cb1e92f99729ad8e/packages/quicktype-core/src/language/Golang/GolangRenderer.ts#L525 and should instead still collect imports even if leadingComments is not set to undefined