Skip to content

additionalProperties does not have to be "either a Boolean or a Schema instance" #3850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MosheElisha opened this issue Jan 6, 2021 · 8 comments

Comments

@MosheElisha
Copy link

Currently, the code enforces that "additionalProperties must be either a Boolean or a Schema instance".

According to https://swagger.io/docs/specification/data-models/dictionaries/ and OAI/OpenAPI-Specification#668 (comment), the following structure is also valid:

type: object
additionalProperties:
  type: string

This is the openapi that represents (and is generated) when the model is a Map of string and string (Map<String, String>).

@frantuma
Copy link
Member

frantuma commented Jan 6, 2021

The enforced behaviour/structure does not seem contradictory to me:

 additionalProperties:
  type: string

is implemented/deserialized in swagger-core with additionalProperties being a Schema with type = "string" (StringSchema)

@MosheElisha
Copy link
Author

Thanks, @frantuma .

Perhaps you are right but when I try to load a valid openapi.json.txt into a io.swagger.v3.oas.models.OpenAPI - I get the below exception.

We are reading the json into OpenAPI in order to test and validate the auto generated openapi.json.

Running with a debugger, we can see that the additionalProperties is a instanceof LinkedHashMap and not a instanceof Boolean or instanceof Schema.

image

...
Caused by: com.fasterxml.jackson.databind.JsonMappingException: additionalProperties must be either a Boolean or a Schema instance
 at [Source: (PushbackInputStream); line: 1245, column: 13] (through reference chain: io.swagger.v3.oas.models.OpenAPI["components"]->io.swagger.v3.oas.models.Components["schemas"]->java.util.LinkedHashMap["CreateDynamicOrderRequest"]->io.swagger.v3.oas.models.media.Schema["properties"]->java.util.LinkedHashMap["customAttributes"]->io.swagger.v3.oas.models.media.Schema["additionalProperties"])
	at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:274)
	at com.fasterxml.jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:623)
	at com.fasterxml.jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:611)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:143)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:402)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:609)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:437)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:32)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:402)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:609)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:437)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:32)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:402)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:402)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4591)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3599)
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:274)
	... 73 more
Caused by: java.lang.IllegalArgumentException: additionalProperties must be either a Boolean or a Schema instance
	at io.swagger.v3.oas.models.media.Schema.setAdditionalProperties(Schema.java:553)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:141)
	... 94 more

@frantuma
Copy link
Member

frantuma commented Jan 6, 2021

I am not sure how you are deserializing and in which scenario, but from your logs it looks like you're using some possibly not configured ObjectMapper; swagger-core provides helper class Json (and similar Yaml) giving access to a correctly configured mapper which - depending on scenario - you would use e.g. like:

Json.mapper().readValue(openApiAsString, OpenAPI.class);

Please close ticket if this answers your question

@MosheElisha
Copy link
Author

@frantuma Perfect. Thank you. I was using a default ObjectMapper. Using io.swagger.v3.core.util.Json it worked properly.

@michaelwangwangwang
Copy link

good

@GeniusDP
Copy link

Works

@mynameiswangyang
Copy link

nice

@aditambi
Copy link

working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants