springfox / springfox-grails-integration

SpringFox integration with Grails 3.x
Apache License 2.0
19 stars 12 forks source link

Question : How to hook into schema object generation #20

Open snimavat opened 6 years ago

snimavat commented 6 years ago

I couldnt find any docs so have to ask the question here.

What I am looking for, is to hook into schema object generation. https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject

So that we can generate the schema object for domain classes based on mappings and constraints etc. And can specify what fields are required, pattern, description, default value, min max and other constraits etc.

Thanks

snimavat commented 6 years ago

I guess what i am looking for is probably ModelBuilderPlugin or may be OperationModelsProvider but dont know where to start.. Any help would be appreciated. @dilipkrish

dilipkrish commented 6 years ago

There is already support for certain annotations that influence model generation and properties. All swagger annotations are supported. E.g. ApiModelProperty. In addition where applicable Jackson annotations are supported. Cannot really say without a specific example.

Could you perhaps describe what you’re ttrying to do

snimavat commented 6 years ago

@dilipkrish - What I am looking for is, to hook grails constraints into springfox

Given the Grails domain class (Model)

class Book {
   String name
   BigDecimal price
   Author author

   static constraints = {
    author nullable:false
    name nullable:false, default: "xxx"
   price nullable: false, max: 1000.00, min: 50.00, default:"xxx"
 }
}

From this, I can see, the model has all the information it needs regarding its properties. And I dont want to use ApiModelProperty annotation. (Currently Adding ApiModelProperty(required=true) to title, for example, doesnt make the title required anyway)

I am looking for integration point in springfox, that will let me include the above information in model schema. Eg make a field required, default value, min and max values etc.

Also, looking for a way to make the author a $ref instead of authorId (Which is what it does currently)

I know the grails side, but dont know springfox enough to combine this two things togather.

Grails constraints: http://docs.grails.org/3.1.1/ref/Constraints/Usage.html Thanks

dilipkrish commented 6 years ago

So your need to implement the ModelPropertyBuilderPlugin and hook in to the GrailsDomainClass to infer the constraints. Sorry that I’m not intimately familiar with grails but I know it’s accesible if you pull in the right beans. You can dig in further if you’d like and let me know if you have any questions.

snimavat commented 6 years ago

Okay, will try with ModelPropertyBuilder. I am quite familiar with Grails.. dont know springfox api enough to hook into

Given the way grails support is currently implemented, I see that Grails domains are configured with Alternate type rules. Would i need to change any thing there ? Or just ModelPropertyBuilder should be enough.

dilipkrish commented 6 years ago

Oh that’s right. It is still possible to do it with the plugin I believe but You may need to enhance the rule/convention to support for what your needing to do. In which case u may need to incorporate it as a PR. Im on vacation and don’t have the code in front of me, sorry. Let me know if have any questions I’ll be gals to help.

snimavat commented 6 years ago

@dilipkrish When using AlternateTypePropertyBuilder and AlternateTypeBuilder How do we specify some properties as required, min, max etc ? ModelPropertyAnnotation on original type doesnt seem to have any effect.

dilipkrish commented 6 years ago

I think that is an enhancement that is in the works in the main springfox library. ModelProperty/ModelPropertyBuilder is missing those properties

ibmsoft commented 6 years ago

@snimavat When using AlternateTypePropertyBuilder and AlternateTypeBuilder How do we specify some properties as required, min, max etc ?

dilipkrish commented 6 years ago

You can add a Size annotation by using the withAnnotations

ibmsoft commented 6 years ago

@dilipkrish thanks for your quick replay i find the withAnnotations method ,but i does not find the correct way ,would you like show me the code(i just want add some description to the model)

dilipkrish commented 6 years ago

I'll have to research this. Do you perhaps have a breaking test?