swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.39k stars 2.19k forks source link

OpenAPI 3.1 - SchemaResolution.INLINE not working with Array Type objects #4771

Closed YousefHaggy closed 1 week ago

YousefHaggy commented 1 month ago

Having an issue with ModelResolver when resolving Array types

Say I have CustomObject.class, trying to resolve the schema of it's Array type class results in a Array schema with a reference

ModelResolver resolver = new ModelResolver(mapper).openapi31(true);
resolver.setSchemaResolution(SchemaResolution.INLINE);
context = new ModelConverterContextImpl(resolver);
context.resolve(new AnnotatedType().type(CustomObject[].class));

Resulting schema component:

{
  "items": {
    "$ref": "#/components/schemas/CustomObject"
   },
  type": "array"
}

SchemaResolution.INLINE doesn't inline these types. When I resolve a schema with that option, any properties that are array types on the class, ex: List, CustomObject[], get converted to Array reference schemas.

Is there anyways to generate fully inline Schemas when there are Array types? For SchemaResolution.INLINE, I would expect my above example to resolve as:

{
  "items": {
    "type": "object",
     "properties": {
       ...
      }
   },
  type": "array"
}
YousefHaggy commented 1 month ago

@frantuma Seeing that you worked on SchemaResolution PRs, do you think this change would make sense for SchemaResolution.INLINE? I could make a PR

Thanks!

frantuma commented 1 week ago

As mentioned in wiki SchemaResolution only applies to OAS 3.0 specification, however the behaviour also occurred with OAS 3.0 specs (openapi31 = false) and this is fixed by this #4784 .

4784 also introduces the option to set an ENV or System property apply-schema-resolution=true which causes the SchemaResolution to be partially processed also for 3.1 specs.

Closing ticket, please reopen if you're still experiencing issues

YousefHaggy commented 1 week ago

Thanks @frantuma, I appreciate this. Could applySchemaResolution be moved to a function call in ModelResolver so that we can just override it in our custom resolver? Setting System properties is unideal in our workflow. I can open a PR for that if you think it's reasonable


protected boolean shouldApplySchemaResolution(){
return !openapi31 ||
                        (Boolean.parseBoolean(System.getProperty(Schema.APPLY_SCHEMA_RESOLUTION_PROPERTY, "false")) ||
                                Boolean.parseBoolean(System.getenv(Schema.APPLY_SCHEMA_RESOLUTION_PROPERTY)));
}
frantuma commented 1 week ago

@YousefHaggy the flag has been added as quick solution for such edge use case, what I meant with partially processed also for 3.1 specs. is that to have it fully working more work is needed, both adding the option along all the involved code in different modules (basically similarly to what done for the schemaResolution option, and also ensure that behavior is correct.

That said, a PR to allow the same outcome currently obtained with the system property by updating ModelResolver is welcome!

YousefHaggy commented 1 week ago

@frantuma Made a small PR to obtain the same outcome but move the check to a method to allow clients to override the default behavior https://github.com/swagger-api/swagger-core/pull/4791