ScaCap / spring-auto-restdocs

Spring Auto REST Docs is an extension to Spring REST Docs
https://scacap.github.io/spring-auto-restdocs/
Apache License 2.0
310 stars 86 forks source link

Is JsonSubtype supported by SARD? Dead links in SARD docs wrt 'examples' and 'source code examples' #285

Closed ghost closed 5 years ago

ghost commented 5 years ago

Using SARD 1.0.14. Having some problems with getting documentation generated when using JsonSubType. As in the 'type' defined in the abstract class which defines the Jsonsubtype is not shown in the docs.

I know from the examples that you support it. Thought to have a look at the code for the examples. Not being able to find it.

The links in the documentations are not working, the links are: https://github.com/ScaCap/spring-auto-restdocs/tree/master/spring-auto-restdocs-example https://htmlpreview.github.io/?https://github.com/ScaCap/spring-auto-restdocs/blob/master/spring-auto-restdocs-example/generated-docs/index.html. Both result in an 404.

In the past I had mentioned that they were not working, see issues #232. In that ticket you supplied a working link: https://htmlpreview.github.io/?https://github.com/ScaCap/spring-auto-restdocs/blob/1.0.x/spring-auto-restdocs-example/generated-docs/index.html. But I can't figure out how to get to the example code.

Any help is very much appreciated.

jmisur commented 5 years ago

Try this link https://htmlpreview.github.io/?https://github.com/ScaCap/spring-auto-restdocs/blob/v1.0.14/spring-auto-restdocs-example/generated-docs/index.html

jmisur commented 5 years ago

Just change the branch to Tag v1.0.14 and you'll see that version of the entire repository: https://github.com/ScaCap/spring-auto-restdocs/tree/v1.0.14

ghost commented 5 years ago

Thanks for the very fast response. Currently at home and don't have access to Eclipse to load the code in, so I could have a better look at it. Used github to look around a bit in the files. You are doing things quite differently then we do.

We started out with Spring Rest Docs and migrated to Spring Auto Rest Docs. With SRD we were using Jackson 1.9.13. For SARD we needed Jacskon 2.x - 2.9.8 now. With SRD we were using JsonTypeInfo and JsonSubTypes which worked fine. With SARD we can't get this to work. The type and a description like "Tag attribute. Available only if metadata type=1." is not shown.

Basically our setup.

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type") // type not shown in SARD docs.
@JsonSubTypes({
        @Type(value = JsonNoCalculationStrategy.class, name = "NO_CALCULATION_STRATEGY"),
        @Type(value = JsonAverageCalculationStrategy.class, name = "AVERAGE_CALCULATION_STRATEGY"),
        @Type(value = JsonAverageAllPassedCalculationStrategy.class, name = "AVERAGE_ALL_PASSED_CALCULATION_STRATEGY")
})
public abstract class JsonAbstractCalculationStrategy {

        private Boolean m_enabled;
    private Boolean m_default;
    private JsonCaluclationStrategyType m_type;

public abstract <R, A> R accept(JsonCalculationStrategyVisitor<R, A> visitor,
            A argument);
}

public interface JsonCalculationStrategyVisitor<R, A> {

    R visit(JsonNoCalculationStrategy value, A argument);
    R visit(JsonAverageCalculationStrategy value, A argument);
    R visit(JsonAverageAllPassedCalculationStrategy value, A argument);
}

/**
 * JSON calculation strategy.
 */
public enum JsonCaluclationStrategyType {
    NO_CALCULATION_STRATEGY, 
    AVERAGE_CALCULATION_STRATEGY, 
    AVERAGE_ALL_PASSED_CALCULATION_STRATEGY
}

One of the three Sub classes 

public class JsonNoCalculationStrategy extends JsonAbstractCalculationStrategy {

    @Override
    public <R, A> R accept(JsonCalculationStrategyVisitor<R, A> visitor, A argument) {
        return visitor.visit(this, argument);
    }
}

JSON request body:

{
  "type" : "NO_CALCULATION_STRATEGY",
  "enabled" : true,
  "default" : false
}'

It looks as if you are using JavaType and Metadata and JsonGenerator to get things working. Again can't really investigate.

Questions:

  1. Does this mean that using @JsonTypeInfo and JsonSubTypes is not supported by SARD?
  2. Does this mean that we have to rewrite our code to make it compliant with SARD?
  3. The way your code works. Is it possible to change the int (1 and 2 in your examples) and use logical names like NO_CALCULATION_STRATEGY?

Just one working day left and then another dev has to take over. If you are able to answer quickly back again he has enough information.

Update Clarified that following goes wrong: The type field and a description like "Tag attribute. Available only if metadata type=1." is not shown. All other fields are however shown.

ghost commented 5 years ago

Another remark. SARD docs: An example of JsonSubType support. See Jackson type documentation. The ' Jackson type documentation.' link/achor doesn't seem to work. I arrive at the same SARD docs at the beginning. Hence couldn't look the info. Do you perhaps have a working link?

jmisur commented 5 years ago

The string you're looking for is here. It's not dynamic, it's just Javadoc of a field in a subtype. If it's not showing for you, you are running snippet generation without doclet preprocessing before (IDE maybe?). If you do full compile and test with maven (if your project is setup properly), then you'll see the Javadoc appearing in final snippet.

Json subtypes and those annotations should indeed work. 1 and 2 are just dummy names and are strings as any other value.

Also check your objectMapper configuration. If it cannot resolve some fields, it might be that it's not configures as in the example.

ghost commented 5 years ago

Very much relieved that our setup is supported. Not that knee deep into Jackson knowledge tbh and it looked as if you were doing something completely different. Going to pass all the knowledge to the other dev (me on a long holiday) and let him install the code examples in Eclipse so its easier to have a look at all the things.

Again thanks a lot for the help and the good explanation. Very much appreciated.

ghost commented 5 years ago

Renamed title: original title was "Where can I find the examples code for SARD 1.0.14. Also links to documentation not working '404'."