0

Having recently started a new Spring-based project with my team, we have decided to leave our well-known-but-outdated Swagger 2.0 documentation practices in favour of Swagger 3.0 standard following the OpenApi directives.

Not to make the jump too drastic, we are trying to stick to the configuration- and annotation-based style of auto-generated documentation. This, however, does not come without some difficulties.

We would like to define schemas used for some heavily reusable types just once, for the whole project to use. Namely, we are using org.bson.types.ObjectId fields coupled with -to-string and string-to- serializers/deserializers utilising type's hex-string representation. This is just fine for our controllers.

Meanwhile, Swagger UI (v. 1.2.9) insists on representing any ObjectId field and/or parameter as its full object form, i.e.

 "id": {
    "timestamp": 0,
    "counter": 0,
    "time": 0,
    "date": "2020-02-11T11:13:57.130Z",
    "machineIdentifier": 0,
    "processIdentifier": 0,
    "timeSecond": 0
  }

Is there a way for our java code to define schema for the ObjectId type - that is an external dependency - once and for all so that our Swagger UI recognises this type can be represented as a simple string at any encountered instance?

1 Answer 1

0

I got the same problem and I solved it using a static block as my constructor:

static {
  SpringDocUtils.getConfig().replaceWithSchema(ObjectId.class, new StringSchema());
}

And, for sure, I'm using Spring Doc OpenAPI with a Java-based configuration. You can simply adjust as your need. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.