question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle Map<String,Object> in the response

See original GitHub issue

I have an endpoint which returns Map<Locale,Product>. I am not sure how annotate it correctly. My Swagger UI shows empty in the response.

enum class Locale(en_US, en_CA, fr_CA) data class Product(name:String, description:String)

Also, where ever I have Map object, it always shows as following in the swagger

“additionalProp1”: “string”, “additionalProp2”: “string”, “additionalProp3”: “string”

With swagger 2 i was able to manually fix the spec by doing something like following description: type: object properties: en_US: type: string example: 'Open' fr_CA: type: string example: 'OUVERT' en_CA: type: string example: 'Open'

or using this

details: description: This map represents locale specific map for various product names type: object properties: en_US: $ref: '#/components/schemas/ProductNameDetails' fr_CA: $ref: '#/components/schemas/ProductNameDetails' en_CA: $ref: '#/components/schemas/ProductNameDetails'

I also found this stackoverflow but not sure how to do this with springdoc https://stackoverflow.com/questions/46552863/how-to-write-openapi-3-swagger-specification-for-property-name-in-map-object

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
florin05commented, Nov 28, 2021

Great, that is exactly what I was looking for, io.swagger.v3.core.converter.ResolvedSchema class! Thank you @bnasslahsen!

1reaction
bnasslahsencommented, Apr 26, 2020

@nehalshah50,

Your question is not directly related to springdoc-openapi, but to the usage of OpenAPI 3. Anyway, here is a sample working code that you can customize:

@RestController
public class TestController {

	@ApiResponse(ref ="#/components/responses/myCustomResponse" )
	@GetMapping(value = "/search", produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getAll() {
		return null;
	}

	@Bean
	public OpenAPI customOpenAPI() {
		return new OpenAPI()
		.components(new Components()
			.addResponses("myCustomResponse", new io.swagger.v3.oas.models.responses.ApiResponse().description("This map represents locale specific map for various product names type")
				.content(new Content().addMediaType(MediaType.APPLICATION_JSON_VALUE,
					new io.swagger.v3.oas.models.media.MediaType().schema(new MapSchema()
					.addProperties("en_US", new StringSchema())
					.addProperties("fr_CA", new StringSchema()))))));
	}

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

how to map response to Map<String, Object> in flutter
You can manually handle collections inside JSON and format it to a object that you need.
Read more >
Avoiding String.ValueOf on Map<String, Object> from JSON
I'm trying insert the new campaigns without having to do a lot of String.ValueOf(m.get(value)); syntax. Its not a huge deal to do String.valueOf ......
Read more >
Map<String, Object> results - Trailhead - Salesforce
Map results = (Map ) JSON.deserializeUntyped(response.getBody()); Want detail meaning of this line ?
Read more >
Convert JSON to a Map Using Gson - Baeldung
Now, if we construct our Map type as Map<String, Object>, then the parser will still default as we saw in the previous section....
Read more >
Java | Examples - Arvados
Examples ; String apiName = · arvados ; Map<String, String> collection = new HashMap<String, String>(); collection.put( · name ; Map<String, Object> params =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found