FasterXML / jackson-module-jsonSchema

Module for generating JSON Schema (v3) definitions from POJOs
371 stars 135 forks source link

How to specify the "id" URI? #72

Open heruan opened 9 years ago

heruan commented 9 years ago

I have two classes: Article and User and their respective schemas are available at:

Since Article.author is of type User, how can I have the correct $ref in the schema? Now I'm getting this:

{
   "type":"object",
   "properties":{
      "id":{
         "type":"integer"
      },
      "title":{
         "type":"string"
      },
      "author":{
         "type":"object",
         "id":"urn:jsonschema:it:saiv:best:common:entity:User",
         "properties":{
            "id":{
               "type":"integer"
            },
            "cn":{
               "type":"string"
            }
         }
      }
   }
}

but I would expect something like:

{
   "type":"object",
   "properties":{
      "id":{
         "type":"integer"
      },
      "title":{
         "type":"string"
      },
      "author":{
          "$ref":"http://localhost:8080/api/users/$schema"
      }
   }
}

This is the code I'm using to generate the schema:

    @GET
    @Path("/$schema")
    @Produces(MediaType.APPLICATION_JSON)
    public String getSchema() throws IOException {
        HyperSchemaFactoryWrapper visitor = new HyperSchemaFactoryWrapper();
        ObjectMapper mapper = new ObjectMapper();
        mapper.acceptJsonFormatVisitor(Article.class, visitor);
        JsonSchema schema = visitor.finalSchema();
        return mapper.writeValueAsString(schema);
    }
edrik commented 9 years ago

I think you can achieve this by setting a custom VisitorContex on the HyperSchemaFactoryWrapper visitor.setVisitorContext( myCtx )

heruan commented 9 years ago

Thank you @edrik using a custom VisitorContext solved half of a problem: now I can generate an id with the full URI of the schema; but I always get the schema nested and not referenced with $ref.