Skip to content

Custom loader and file referenced types fail to qualify name #495

@cpu

Description

@cpu

👋 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions