5

I am trying to do something like the following:

In my schema JSON models section:

"MyObject": {
  "type": "object",
  "description": "my description",
  "properties": {
    "type": "string",
    "description": "my property description",
    "customAnnotation": "true"
  }
}

So right out of the gate, I'm trying to extend JSON Schema - likely my first problem. However, I do not know how to do this legitimately, if that is even possible.

Snippet for use case for "customAnnotation" the moustache template (-l spring):

{{#vars}}
{{^customAnnotation}}@CustomAnnotation {{/customAnnotation}}public {{{datatypeWithEnum}}} {{getter}}() {
    return {{name}};
}
{{/vars}}

Can I actually do something like this? clues helpful (yes, I'm a newbie in this area)!

Note: I would also like to use the count of found "customAnnotation" > 0 to annotate a class. Something like:

{{^containsCustomAnnotations}}@ContainsCustomAnnotations {{/hasCustomAnnotation}}public void MyClass {
}

Thanks!

digable1
  • 61
  • 4
  • Check the "Specification Extensions" section here http://swagger.io/specification/ but to get something like `{{/hasCustomAnnotation}}` you might need to modify the code generator itself. – moondaisy Jun 16 '17 at 00:54
  • Plus, in swagger codegen this are calles Vendor Extensions and they are used for some generators (it is explained on the Wiki) . Look for `{{vendorExtensions` in the repository to see how they used them on the templates. I think there might be a `{{hasVendorExtensions}}` which could be useful, but I am not a 100% sure, and if there is but the language you are using uses other extensions it might not have the same value that your `{{hasCustomAnnotation}} ` as it might consider the ones from the language too. – moondaisy Jun 16 '17 at 01:30

1 Answers1

0

For the first part, schema:

"MyObject": {
  "type": "object",
  "description": "my description",
  "properties": {
    "foo": {
      "type": "string",
      "description": "my property description",
      "x-customAnnotation": true
    }
  }
}

and template:

{{#vendorExtensions.x-customAnnotation}}@CustomAnnotation {{/vendorExtensions.x-customAnnotation}}public {{{datatypeWithEnum}}} {{getter}}() {
    return {{name}};
}

cf. Swagger Codegen :- Vendor Extensions are not accessible

ejohnson
  • 655
  • 7
  • 18