5

I want to use a class from a library as a type of a property. Say it is foo.lib.Foo. What is the right way to properly define it as a type of the property?

SomeDto:
  type: object
  properties:
     foo:
        $ref: `foo.lib.Foo`??? 
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • An OpenAPI definition doesn't have visibility into external Java/etc. libraries, all schemas (classes) must be defined in your OpenAPI file. See also [How to have a property reference a model in OpenAPI (i.e. nest the models)?](https://stackoverflow.com/a/26410540/113116) – Helen Dec 17 '20 at 09:25

1 Answers1

6

This can be done through defining a type mapping and referring the type explicitly.

  1. Define a type mapping in the plugin configuration
    <configuration>
         ...
         <importMappings>
             <importMapping>Foo=foo.lib.Foo</importMapping>
         </importMappings>
         ...
    </configuration>
  1. Refer the type:
SomeDto:
  type: object
  properties:
     foo:
        $ref: '#/components/schema/Foo' 
  1. Include the dependency of the library in the classpath (the dependencies section).
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • `type: Foo` is not a valid type in an OpenAPI definition, the `type` values are predefined. Do you maybe mean `foo: {$ref: '#/components/schemas/Foo'}` where `Foo` is defined as `type: object` (i.e. free-form object)? – Helen Dec 17 '20 at 14:12
  • 1
    Nope, this is exactly like I wrote and it works perfectly fine: the generated classes contain proper full names of the resolved type (`foo.lib.Foo` in the class code) – Andremoniy Dec 17 '20 at 14:13
  • This does not work. I have tried to import a class that is defined within a maven dependency using the accepted answer and I can confirm it does not work. You basically get the error. My maven pom contains. MyClass=com.org.project.MyClass Errors: -Could not find components/schemas/MyClass in contents of ./ird-db-dao-service.json – juckky Nov 23 '22 at 18:21
  • @juckky can you try with `schemaMappings` ? like this : Foo=foo.lib.Foo I could import the missing type like this. openapi-generator-maven-plugin V. 6.5.0 – François Vanhille Apr 13 '23 at 11:35