👋 Hi there,
Summary
I'm trying to use go-jsonschema as a library to generate Go code from JSON schemas exposed via a dependency that offers a standard library embed.FS with the schema content. I thought this would be workable by implementing my own schemas.Loader, but in practice this doesn't seem to work. References produce errors of the form:
could not generate type for field "source": could not resolve qualified file name for common.json: cannot resolve schema "common.json"`
I've created a stand-alone reproduction here: https://github.com/cpu/schema-embed
Analysis
Looking at schemaGenerator.generateReferencedType, we can see it's calling schemas.QualifiedFileName to determine the qualified name of a referenced file:
|
qualified, qerr := schemas.QualifiedFileName(fileName, g.schemaFileName, g.config.ResolveExtensions) |
|
if qerr != nil { |
|
return nil, fmt.Errorf("could not resolve qualified file name for %s: %w", fileName, qerr) |
|
} |
That function in turn unconditionally uses the native filesystem via os.Stat to determine if the fully qualified schema file exists:
|
if !fileExists(qualified) { |
|
continue |
|
} |
|
func fileExists(fileName string) bool { |
|
_, err := os.Stat(fileName) |
|
|
|
return err == nil || !os.IsNotExist(err) |
|
} |
This feels like an instance of logic that could be deferred to the Loader instance, perhaps either having schemas.Loader.Load() also return the fully qualified schema name alongside the *schemas.Schema, or by introducing a new func to the Loader interface for this purpose.
Is this something the project is interested in addressing? I could probably implement the PR myself with some guidance on the preferred solution shape. In the meantime I've reverted to expanding the embed.FS into a tempdir on the native filesystem and using the stock FileLoader. I've also confirmed that hacking out the schemas.QualifiedFileName() call in schema_generator.go and using the fileName as the qualified name "fixes" the issue for my use-case, though in the crudest way possible.
Thanks!
👋 Hi there,
Summary
I'm trying to use
go-jsonschemaas a library to generate Go code from JSON schemas exposed via a dependency that offers a standard libraryembed.FSwith the schema content. I thought this would be workable by implementing my ownschemas.Loader, but in practice this doesn't seem to work. References produce errors of the form:I've created a stand-alone reproduction here: https://github.com/cpu/schema-embed
Analysis
Looking at
schemaGenerator.generateReferencedType, we can see it's callingschemas.QualifiedFileNameto determine the qualified name of a referenced file:go-jsonschema/pkg/generator/schema_generator.go
Lines 113 to 116 in 2837f3b
That function in turn unconditionally uses the native filesystem via
os.Statto determine if the fully qualified schema file exists:go-jsonschema/pkg/schemas/loaders.go
Lines 203 to 205 in 2837f3b
go-jsonschema/pkg/schemas/loaders.go
Lines 220 to 224 in 2837f3b
This feels like an instance of logic that could be deferred to the
Loaderinstance, perhaps either havingschemas.Loader.Load()also return the fully qualified schema name alongside the*schemas.Schema, or by introducing a newfuncto theLoaderinterface for this purpose.Is this something the project is interested in addressing? I could probably implement the PR myself with some guidance on the preferred solution shape. In the meantime I've reverted to expanding the
embed.FSinto a tempdir on the native filesystem and using the stockFileLoader. I've also confirmed that hacking out theschemas.QualifiedFileName()call inschema_generator.goand using thefileNameas thequalifiedname "fixes" the issue for my use-case, though in the crudest way possible.Thanks!